> ## Documentation Index
> Fetch the complete documentation index at: https://docs.swarms.world/llms.txt
> Use this file to discover all available pages before exploring further.

# MultiAgentRouter Guide

> Let a boss model read each task, pick the best specialist agent, and run it

A `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](https://docs.swarms.world/api/multi-agent-router.md).

## How a task is routed

1. The task is added to the router's conversation as `user`.
2. The boss model replies with JSON: a list of handoffs, each with `reasoning`, `agent_name` and an optional rewritten `task`.
3. The router runs the chosen agent on the rewritten task, or on the original task when the boss gave none.
4. 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's `agent_name` and `agent_description`, so make the descriptions say plainly what each agent is for.

```python theme={null}
from swarms import Agent, MultiAgentRouter

agents = [
    Agent(
        agent_name="Code-Expert",
        agent_description="Writes, reviews and explains Python code",
        system_prompt="You write short, correct Python code with a one-line explanation.",
        model_name="gpt-5.4-mini",
        max_loops=1,
        output_type="final",
        print_on=False,
    ),
    Agent(
        agent_name="Researcher",
        agent_description="Explains topics with facts and context",
        system_prompt="You explain topics clearly and factually in a short paragraph.",
        model_name="gpt-5.4-mini",
        max_loops=1,
        output_type="final",
        print_on=False,
    ),
    Agent(
        agent_name="Copywriter",
        agent_description="Writes short marketing copy and taglines",
        system_prompt="You write punchy marketing copy.",
        model_name="gpt-5.4-mini",
        max_loops=1,
        output_type="final",
        print_on=False,
    ),
]

router = MultiAgentRouter(
    name="Help-Desk",
    agents=agents,
    model="gpt-5.4-mini",
)

result = router.run(
    "Write a Python function that checks whether a string is a palindrome."
)

for message in result:
    print(f"--- {message['role']}")
    print(message["content"])
```

`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`:

| `output_type` | Returns |
| - | - |
| `"dict"` (default) | The list of messages: the task as `user`, then the chosen agent's answer under its name |
| `"final"` | Only the last message's content, which is the agent's answer |
| `"str"` | The whole conversation as one string |

The boss's reasoning is never added to the conversation. To see it, keep `print_on=True`.

> [!NOTE]
>
> 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](https://docs.swarms.world/examples/concurrent-workflow-example.md) 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.

```python theme={null}
router = MultiAgentRouter(
    name="Help-Desk",
    agents=agents,
    model="gpt-5.4-mini",
    output_type="final",
    print_on=False,
)

tasks = [
    "Give me three taglines for a reusable water bottle.",
    "Why is the sky blue?",
]
answers = router.batch_run(tasks)

for task, answer in zip(tasks, answers):
    print(f"=== {task}\n{answer}\n")

# The router keeps one conversation across every call.
print(len(router.conversation.conversation_history), "messages so far")
router.conversation.clear()
print(len(router.conversation.conversation_history), "messages after clear()")
```

This prints `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.

```python theme={null}
router = MultiAgentRouter(
    name="Code-First-Desk",
    agents=agents,
    model="gpt-5.4-mini",
    system_prompt=(
        "Send anything that mentions code, scripts or programming to "
        "Code-Expert, even when it also asks for an explanation.\n"
    ),
    output_type="final",
)

answer = router.run("Explain what a Python decorator is.")
print(answer)
```

The printed reasoning cites the rule and selects Code-Expert. Other boss settings: `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.

## Next steps

* [MultiAgentRouter reference](https://docs.swarms.world/api/multi-agent-router.md)
* [SwarmRouter guide](https://docs.swarms.world/examples/swarm-router-example.md)
* [Runnable scripts](https://github.com/kyegomez/swarms/tree/master/examples/multi_agent/mar)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.