Router¶
Agent router for multi-agent routing and fallback.
- class agent.router.AgentRouter(agents, strategy=RoutingStrategy.FALLBACK, custom_router=None)[source]¶
Bases:
objectRoutes 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:
strategy (
RoutingStrategy|str)custom_router (
Callable[[AgentRequest,list[Agent]],RouteResult] |None)
- 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:
- 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:
- 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:
- 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:
- Returns:
AsyncStreamResponse from the selected agent