Browse documentation

CrewAI

Connect CrewAI multi-agent systems to nRouter using OpenAI-compatible endpoints. Enable resilient model routing, configured fallbacks, and usage monitoring.

Connect CrewAI multi-agent swarms to nRouter (https://api.nrouter.ai/v1) to govern autonomous role-playing agents with hard budget ceilings, turn-by-turn guardrails, and automated provider fallbacks. By pointing CrewAI's LLM class to nRouter, multi-agent crews collaborate with zero-markup list pricing and real-time cost attribution at the gateway.

For framework details, see the official CrewAI documentation.

Installation & Setup

Install CrewAI and export your virtual API key:

pip install crewai nrouter-sdk
export NROUTER_API_KEY="sk-nrouter-xxxxxxxxxxxx"

Instantiate CrewAI's LLM pointing to nRouter:

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"],
    },
)

Collaborative Multi-Agent Crew

Build an autonomous research and writing workflow:

import os
from crewai import Agent, Task, Crew, Process, LLM

llm = LLM(
    model="openai/gpt-5.4-mini",
    base_url="https://api.nrouter.ai/v1",
    api_key=os.environ["NROUTER_API_KEY"],
)

researcher = Agent(role="Researcher", goal="Analyze gateway patterns", backstory="Systems specialist.", llm=llm)
writer = Agent(role="Writer", goal="Summarize findings", backstory="Technical editor.", llm=llm)

task1 = Task(description="Summarize zero-markup gateway pricing benefits.", expected_output="3 bullets.", agent=researcher)
task2 = Task(description="Draft executive summary.", expected_output="1 paragraph.", agent=writer, context=[task1])

crew = Crew(agents=[researcher, writer], tasks=[task1, task2], process=Process.sequential)
print(crew.kickoff())

Routing & Observability

Configure model tiers, spend controls, and error handling:

  • Routing & fallbacks: Target specific models or use nrouter/auto (see Router Settings). For per-call fallbacks, pass nrouter_fallbacks in extra_body (see Per-Request Options).
  • Budget ceilings: Set USD limits on virtual keys via budget controls to prevent runaway agent loops.
  • FinOps telemetry: Headers include x-nr-request-id, x-nr-model, x-nr-cost-status, and x-nr-request-cost.
  • Error handling: Gateway errors map to HTTP status codes (see Errors & Rate Limits):
try:
    result = crew.kickoff()
except Exception as e:
    if "402" in str(e) or "insufficient_credits" in str(e):
        print("Budget ceiling reached for this virtual key.")
    elif "guardrail" in str(e).lower():
        print("Blocked by gateway safety guardrails.")

Next Steps

Was this page helpful?