Skip to main content

Overview

The MultiAgentRouter uses a boss agent powered by LLMs to intelligently route tasks to the most appropriate specialized agent(s). The boss agent analyzes task requirements and agent capabilities to make routing decisions, supporting both single and multiple agent assignments.

Installation

Attributes

Optional[str]
default:"None"
Unique identifier for the router instance. When left as None, resolves to generate_id("multi-agent-router")
str
default:"swarm-router"
The name of the router
str
A description of the router’s purpose
List[Callable]
default:"None"
A list of agents to be managed by the router
str
default:"gpt-5.4"
The model to use for the boss agent
float
default:"0.1"
The temperature for the boss agent’s model
callable
default:"None"
Stored on the router as shared_memory_system. Nothing in the router reads it.
OutputType
default:"dict"
The type of output expected from the agents
bool
default:"True"
Whether to print the boss agent’s decision
str
default:"None"
Custom system prompt for the router
bool
default:"True"
Whether to skip executing agents when their assigned task is null or None
Any extra positional or keyword arguments are forwarded to the LiteLLM instance that backs the boss agent (router.function_caller).

Methods

route_task()

Routes a task to the appropriate agent(s) and returns their response.
Parameters:
  • task (str): The task to be routed
Returns: The formatted conversation history (shaped by output_type), containing the user task and the response(s) that were appended to it.
When the boss selects multiple agents, handle_multiple_handoffs() runs all of them concurrently but only appends the first selected agent’s response to the conversation — every other agent’s output is computed and then discarded. The boss’s reasoning is never added to the conversation either (it is only printed to the console when print_on=True), so it is not present in the returned result.

run()

Alias for route_task(). Each call is recorded as a MultiAgentRouter.run trace span when telemetry is enabled. Calling the router directly, router(task), does the same thing.

batch_run()

Batch route tasks to the appropriate agents sequentially.
Parameters:
  • tasks (List[str]): List of tasks to route
Returns: Routing results for the tasks that succeeded, in input order. A task that raises is logged and left out of the list; the batch keeps going.

concurrent_batch_run()

Concurrently route tasks to the appropriate agents.
Parameters:
  • tasks (List[str]): List of tasks to route
Returns: Routing results for the tasks that succeeded, in input order. A task that raises is logged and left out of the list.
Every task shares the router’s single Conversation, so their histories interleave. Use batch_run() when each result’s history matters.

Usage Examples

Basic Routing

Custom System Prompt

Batch Processing

Multiple Agent Assignment

Skip Null Tasks

Response Format

The boss agent returns routing decisions in this JSON format:
For multiple agents:

Features

  • Intelligent Routing: Boss agent analyzes task requirements and agent capabilities
  • Single or Multiple Agents: Automatically determines if task requires one or multiple agents
  • Custom Routing Logic: Override system prompt to customize routing behavior
  • Batch Processing: Process multiple tasks sequentially or concurrently
  • Flexible Output: Support for various output formats (dict, string, json, etc.)
  • Null Task Handling: Option to skip agents with null/empty task assignments