MultiAgentRouter puts a boss model in front of a team of specialist agents. For each task, the boss reads the name and agent_description of every agent, picks the agent that fits, and may rewrite the task for it. The router then runs that agent and returns the conversation. Use it as a help desk or front door: many kinds of request come in, and each should reach the one agent built for it.
Reference: MultiAgentRouter.
How a task is routed
- The task is added to the router’s conversation as
user. - The boss model replies with JSON: a list of handoffs, each with
reasoning,agent_nameand an optional rewrittentask. - The router runs the chosen agent on the rewritten task, or on the original task when the boss gave none.
- The agent’s answer is added to the conversation under the agent’s name, and the conversation is returned in the format set by
output_type.
1. Route a task
The boss only sees each agent’sagent_name and agent_description, so make the descriptions say plainly what each agent is for.
model is the boss model; each agent keeps its own model_name. With the default print_on=True, the router prints the boss’s decision as a panel, including its reasoning. Here the boss picks Code-Expert and usually rewrites the task, for example to ask for a short explanation as well.
The workers use output_type="final" so that each run returns only the agent’s latest reply. An Agent keeps its memory between runs, and its default output type returns its whole history, so a reused agent would otherwise repeat its earlier answers.
2. Read the result
run(), route_task() and calling the router directly, router(task), all do the same thing. What they return depends on output_type:
The boss’s reasoning is never added to the conversation. To see it, keep
print_on=True.
When the boss splits a task across several agents, the router runs them all at the same time but records only the first agent’s answer. The other answers are dropped. Write descriptions that make one agent the clear choice, or use a ConcurrentWorkflow when you want every agent’s answer.
3. Route several tasks
batch_run() routes tasks one after another. With output_type="final", each result is that task’s answer.
4 messages so far, then 0 messages after clear(). The router never resets its conversation, so with output_type="dict" or "str" each result also holds every earlier task. Call router.conversation.clear() between tasks, or use a new router, when each result must stand alone.
A task that fails in batch_run() is logged and left out of the results, so the list can be shorter than tasks. concurrent_batch_run() routes the tasks in threads; all of them write to the one shared conversation.
4. Steer the boss
system_prompt is placed in front of the built-in routing prompt, so use it for house rules. Here every coding question goes to Code-Expert, even one that only asks for an explanation.
temperature (left out of the request when unset), and any extra keyword argument, which is passed on to the LiteLLM client behind the boss (router.function_caller).
Errors
run() raises ValueError when the boss’s reply is not JSON (the message names the router, the boss model and the start of the reply), when the boss selects no agent, or when it names an agent the router does not have. With skip_null_tasks=True (the default), a handoff whose task resolves to empty is skipped instead of run.