Router

Agent router for multi-agent routing and fallback.

class agent.router.AgentRouter(agents, strategy=RoutingStrategy.FALLBACK, custom_router=None)[source]

Bases: object

Routes requests across multiple agents with fallback support.

The router provides strategies for: - Automatic failover when providers fail - Load balancing across providers - Capability-based routing - Cost optimization

Example

```python router = AgentRouter(

agents=[

Agent(provider=”anthropic”, model=”claude-sonnet”), Agent(provider=”openai”, model=”gpt-4o”),

], strategy=”fallback”,

)

# Automatically falls back if first provider fails response = router.run(“Hello!”) ```

Parameters:
run(input=None, *, messages=None, system=None, temperature=None, max_tokens=None, stop=None, metadata=None)[source]

Execute a request using the routing strategy.

Parameters:
Return type:

AgentResponse

Returns:

AgentResponse from the selected agent

Raises:

RoutingError – If all agents fail

async run_async(input=None, *, messages=None, system=None, temperature=None, max_tokens=None, stop=None, metadata=None)[source]

Execute a request asynchronously using the routing strategy.

Parameters:
Return type:

AgentResponse

Returns:

AgentResponse from the selected agent

stream(input=None, *, messages=None, system=None, temperature=None, max_tokens=None, metadata=None)[source]

Stream a response using the routing strategy.

For fallback strategy, only tries next agent if initial connection fails.

Parameters:
Return type:

StreamResponse

Returns:

StreamResponse from the selected agent

async stream_async(input=None, *, messages=None, system=None, temperature=None, max_tokens=None, metadata=None)[source]

Stream a response asynchronously using the routing strategy.

Parameters:
Return type:

AsyncStreamResponse

Returns:

AsyncStreamResponse from the selected agent

json(input=None, *, schema, messages=None, system=None, temperature=None, max_tokens=None, metadata=None)[source]

Execute a structured output request with routing.

Parameters:
Return type:

AgentResponse

Returns:

AgentResponse with parsed output