Sub topic 5
The orchestrator pattern
An orchestrator is a manager agent that keeps control of the conversation and calls specialist agents as ordinary tools. This chapter assumes you know Python, JSON, and basic LLM tool calling, and that you have seen the "agents as tools" idea from the previous chapter.
The working model
The user talks to one agent only. That agent splits the request into subtasks, calls a specialist for each one, reads the returned strings, and writes the final answer. The specialists never speak to the user and never take over the conversation.
Building it
Each specialist is wrapped in a function. The function takes a task string, runs the specialist on a fresh history, and returns the final text. This code reuses Agent and run from the handoff example.
def as_tool(worker: Agent, call_model):
def tool(task: str) -> str:
# The worker starts with a fresh history containing only the task.
_, text = run(worker, [{"role": "user", "content": task}], call_model)
return text
return tool
def build_orchestrator(researcher: Agent, writer: Agent, call_model) -> Agent:
return Agent(
name="orchestrator",
instructions="Split the request into subtasks, call the specialists, and combine their answers.",
tools={
"research": as_tool(researcher, call_model),
"write": as_tool(writer, call_model),
},
)Two details matter here.
- The worker sees only the
tasktext. It does not see the user's messages or the other specialists' work. If the writer needs the researcher's findings, the orchestrator must copy them into the writer's task. - The orchestrator's
runloop treats a specialist like any other tool. The result is appended as atoolmessage, and the orchestrator decides what to do next.
What the orchestrator is responsible for
The orchestrator owns four jobs:
- Decomposing the request into subtasks that one specialist can finish alone.
- Choosing which specialist gets each subtask.
- Passing the right context in each task string.
- Merging the results into one coherent answer.
Because the orchestrator holds the whole picture, it can check results, retry a weak one, or skip a specialist that is not needed.
Common errors
- Vague task strings. "Write the section" fails because the writer has no context. Include the goal, the facts, and the expected format.
- Overlapping specialists. If two specialists could handle the same task, the orchestrator picks inconsistently. Give each one a distinct description.
- Passing results unchecked. A specialist can return an error message or an empty string as a normal result. The orchestrator should validate before merging.
- Everything through the orchestrator. Its context grows with every result. Long runs can crowd out the instructions, so ask specialists for short summaries.
Compared with a handoff
In a handoff, control moves to another agent and the conversation continues there. With an orchestrator, control stays put and the specialists are hidden behind tool calls. Use the orchestrator when you need to combine several outputs or enforce one consistent voice.
Exercise
In as_tool, the writer specialist needs the researcher's findings to draft a section. How does the writer receive them, and why?
Show answer
The orchestrator must include the findings in the task string it passes to the write tool. The writer starts with a fresh history containing only that task, so it cannot see the researcher's output or the original conversation otherwise.