Browse documentation

AutoGen

Orchestrate Microsoft AutoGen multi-agent workflows with nRouter. Connect agents via OpenAI-compatible endpoints with intelligent routing and budget controls.

Orchestrate Microsoft AutoGen multi-agent workflows through nRouter to safeguard conversational swarms with budget ceilings, server-side prompt injection defense, and reliable model fallbacks. Pointing OpenAIChatCompletionClient to https://api.nrouter.ai/v1 brings centralized governance and exact cost attribution to high-iteration agent conversations.

For framework details, see the official AutoGen documentation.

Installation & Setup

Install AutoGen and export your virtual API key:

pip install autogen-agentchat autogen-ext[openai] nrouter-sdk
export NROUTER_API_KEY="sk-nrouter-xxxxxxxxxxxx"

Initialize OpenAIChatCompletionClient pointing to nRouter:

import os
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(
    model="claude-sonnet-4-5-20250929",
    api_key=os.environ["NROUTER_API_KEY"],
    base_url="https://api.nrouter.ai/v1",
    timeout=60.0,
    max_retries=2,
    extra_create_args={"extra_body": {"nrouter_cache": True}},
)

Multi-Agent Group Chat

Construct an agent team with round-robin turns and turn-level limits:

import asyncio
import os
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_ext.models.openai import OpenAIChatCompletionClient

async def run_team():
    client = OpenAIChatCompletionClient(
        model="gpt-5.4-mini",
        api_key=os.environ["NROUTER_API_KEY"],
        base_url="https://api.nrouter.ai/v1",
    )
    researcher = AssistantAgent(name="researcher", model_client=client)
    critic = AssistantAgent(name="critic", model_client=client)
    team = RoundRobinGroupChat([researcher, critic], termination_condition=MaxMessageTermination(max_messages=4))
    result = await team.run(task="Evaluate zero-markup gateway pricing.")
    for msg in result.messages:
        print(f"[{msg.source}]: {msg.content}")

asyncio.run(run_team())

Routing & Observability

Configure model selection, fallbacks, and cost tracking:

  • Routing & fallbacks: Target specific models directly or use nrouter/auto (see Router Settings). For per-call fallbacks, pass nrouter_fallbacks inside extra_create_args={"extra_body": ...} (see Per-Request Options).
  • Budget enforcement: Set hard USD spend ceilings via budget controls to prevent runaway agent loops.
  • FinOps telemetry: Inspect response headers: 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):
import openai

try:
    asyncio.run(run_team())
except openai.BadRequestError as e:
    print(f"Blocked by guardrail or invalid parameters: {e}")
except openai.RateLimitError:
    print("Rate limit reached. Backing off before retry.")

Next Steps

Was this page helpful?