DEV Community

nRouter
nRouter

Posted on

One LLM Object: putting a CrewAI crew behind a gateway

CrewAI crews have a property that cuts both ways: a single run makes a lot of model calls. Researcher thinks, writer drafts, the writer revises, someone summarises. Each call is small. The run is not. And because the calls fan out from agent definitions scattered through the codebase, the operational questions — what did that run cost, what happens when a model errors mid-task, what stops a loop — tend to have no single place to live.

Except they do. Agents in CrewAI don't construct their own clients; they receive an LLM object. That object is the seam.

The one definition

pip install crewai nrouter-sdk
Enter fullscreen mode Exit fullscreen mode
import os
from crewai import LLM

llm = LLM(
    model="openai/claude-sonnet-4-5-20250929",
    base_url="https://api.nrouter.ai/v1",
    api_key=os.environ["NROUTER_API_KEY"],
    temperature=0.2,
    timeout=60.0,
    extra_body={
        "nrouter_cache": True,
        "nrouter_fallbacks": ["claude-haiku-4-5-20251001"],
    },
)
Enter fullscreen mode Exit fullscreen mode

The model string carries the openai/ prefix because the gateway is reached through CrewAI's OpenAI-compatible path. Hand this llm to every agent and the crew's traffic shares one configuration.

What doesn't change

The crew itself. Agent still takes a role, a goal, a backstory and the llm. Task still chains through context. Crew(agents=[...], tasks=[...], process=Process.sequential) still ends in crew.kickoff(). Crew design is CrewAI's job and it stays CrewAI's.

What the gateway carries

  • Fallbacks in extra_body. nrouter_fallbacks names the chain on the object — fallbacks work through that field, not through anything written into the model string. Or use nrouter/auto with nrouter_optimize set to quality, balanced or cost (plus nrouter_cost_band under cost) and let routing decide per call.
  • A ceiling a loop can hit. Spend limits sit on the virtual key, checked before a call runs. A crew that reaches its ceiling gets HTTP 402 with insufficient_credits — a failure you can catch, log and act on, instead of a quiet weekend of iterating.
  • A record per call. Responses carry x-nr-request-id, x-nr-model, x-nr-cost-status and x-nr-request-cost. Across a run, that's attribution: which model served which task, at what settled cost.
  • Errors that say what happened. Gateway errors map to HTTP status codes. A guardrail block names itself in the error; the ceiling arrives as 402. The try/except around kickoff() can tell them apart.

A crew is a workload with a shape — bursty, multi-step, occasionally loopy. Governing it at the one object every agent shares is simply meeting the workload where it already gathers.

Full walkthrough: CrewAI guide

Top comments (0)