Sub topic 4
Agents as tools
This chapter assumes you know the basic agent loop and tool calling from the earlier chapters. You should be comfortable reading Python and passing JSON-like dictionaries.
The idea
A tool is a function the model can call by name. A function can do anything, including running another agent. If you wrap a specialist agent in a plain function that takes a string and returns a string, the calling agent cannot tell it apart from any other tool.
This is the "agent as tool" pattern. The calling agent keeps control of the conversation. The specialist does one job and returns.
Wrapping an agent
This code uses the Agent dataclass and run function from the handoff chapter.
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),
},
)as_tool is a closure. It captures the worker and the model client, and it returns a function with a simple signature. Each call to run inside the tool uses a new message list, so the worker never sees the orchestrator's history.
Running it with a stub model
A scripted model lets you run the pattern without an API key.
def fake_model(agent, messages):
if agent.name == "orchestrator":
if not any(m["role"] == "tool" for m in messages):
return {"type": "tool", "name": "research",
"args": {"task": "Find facts about tides"}}
return {"type": "final", "text": "Summary: " + messages[-1]["content"]}
return {"type": "final", "text": f"[{agent.name}] facts about tides"}
researcher = Agent("researcher", "Find facts.")
writer = Agent("writer", "Write clearly.")
boss = build_orchestrator(researcher, writer, fake_model)
print(run(boss, [{"role": "user", "content": "Explain tides"}], fake_model))The output is ('orchestrator', 'Summary: [researcher] facts about tides'). The final answer comes from the orchestrator, not the researcher.
What this gives you
- Control stays in one place. The orchestrator decides what runs next and when to stop.
- Isolated context. Each worker sees a short, focused history, which keeps prompts small and reduces distraction.
- Reusable workers. The same specialist can serve several orchestrators.
- Simple testing. You can test a worker by calling its tool function directly.
Common errors
- Starving the worker. The worker sees only the
taskstring. If the orchestrator writes "Summarize it", the worker has no idea what "it" is. Tell the orchestrator in its instructions to write self-contained tasks. - Passing everything. Copying the full history into every task removes the isolation benefit and raises token cost.
- Unbounded nesting. A worker that has its own agent tools can spawn further calls. Each inner
runhas its ownmax_turns, but the total work is the product of those limits. Cap the depth. - Unhandled worker failure. If the worker raises an exception, the orchestrator's
runcrashes. Catch errors insidetooland return a readable error string, or let the orchestrator decide how to recover.
Exercise
An orchestrator calls a "write" tool with the task "Rewrite the draft above". The writer produces a confused answer. What is the cause, and how do you fix it?
Show answer
The writer starts with a fresh history containing only the task string, so "the draft above" refers to text it never saw. Fix it by making the orchestrator include the draft text in the task, or by passing it as a separate argument to the tool.