Skip to main content

Router

Route prompts to different agents.

Constructor

Router(
agents: dict[str, Agent],
route: Callable[[str], str] | None = None,
model: str | None = None,
provider: str = "mistral",
api_key: str | None = None,
)
ParameterTypeDescription
agentsdict[str, Agent]Named agents to route between
routeCallableFunction (prompt) -> agent_name. If None, uses LLM
modelstrModel for LLM-based routing
providerstrProvider for LLM-based routing
api_keystrAPI key for LLM-based routing

Methods

route

async def route(prompt: str) -> str

Determine which agent to use. Returns agent name.

run

async def run(prompt: str, plan: bool = False) -> str

Route to appropriate agent and run it.

ParameterTypeDescription
promptstrThe prompt to process
planboolIf True, agent creates plan first

run_sync

def run_sync(prompt: str, plan: bool = False) -> str

Synchronous version of run().

Example

from pure_agents import Agent, Router

coder = Agent(template="coder")
writer = Agent(template="creative")

# Function-based routing
def route(prompt: str) -> str:
if "code" in prompt.lower():
return "coder"
return "writer"

router = Router(
agents={"coder": coder, "writer": writer},
route=route,
)

result = await router.run("Write a sorting function")

LLM-based routing

# No route function = LLM decides
router = Router(
agents={"coder": coder, "writer": writer},
)

result = await router.run("Explain recursion")