Browse documentation
FrameworksCrewAI

CrewAI

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

Last updated

CrewAI speaks the OpenAI REST shape, so every CrewAI agent automatically supports nRouter. Set environment variables and CrewAI routes everything through the gateway — guardrails, caching, and rate-limits auto-apply per-agent.

Installation

pip install crewai

Setup

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

llm = LLM(
    model="openai/claude-sonnet-4-5-20250929",
    base_url="https://api.nrouter.ai/v1",
    api_key=os.environ["NROUTER_API_KEY"],
)

Build a Crew

researcher = Agent(
    role="Senior Researcher",
    goal="Uncover cutting-edge developments in AI",
    backstory="An expert at finding patterns in technical literature.",
    llm=llm,
    verbose=True,
)

writer = Agent(
    role="Tech Writer",
    goal="Translate research into clear prose",
    backstory="Known for explaining complex topics simply.",
    llm=llm,
    verbose=True,
)

research_task = Task(
    description="Research the latest LLM gateway architectures.",
    expected_output="A 5-bullet summary of key findings.",
    agent=researcher,
)

write_task = Task(
    description="Turn the research into a 1-paragraph blog intro.",
    expected_output="A polished opening paragraph.",
    agent=writer,
    context=[research_task],
)

crew = Crew(agents=[researcher, writer], tasks=[research_task, write_task])
result = crew.kickoff()
print(result)

Per-Request Overrides

Pass nrouter_* fields through extra_body on the LLM configuration:

llm = LLM(
    model="openai/gpt-5.5",
    base_url="https://api.nrouter.ai/v1",
    api_key=os.environ["NROUTER_API_KEY"],
    extra_body={
        "nrouter_prompt_template_id": "your-summarizer-id",
        "nrouter_prompt_variables": {"language": "Spanish"},
        "nrouter_cache": False,
    },
)

Guardrails are not passed here. You assign them in the dashboard at key, team, or organization scope — the narrowest scope that mentions a guardrail wins — and they run automatically on every agent turn that scope covers.

Multi-Model Crews

Different agents can use different models — nRouter unifies billing and observability across them:

fast_llm = LLM(model="openai/gpt-5.4-mini", base_url="https://api.nrouter.ai/v1",
               api_key=os.environ["NROUTER_API_KEY"])
smart_llm = LLM(model="openai/claude-sonnet-4-5-20250929", base_url="https://api.nrouter.ai/v1",
                api_key=os.environ["NROUTER_API_KEY"])

researcher = Agent(role="Researcher", llm=smart_llm, ...)
classifier = Agent(role="Classifier", llm=fast_llm, ...)

Next Steps

Was this page helpful?