Pydantic AI · Chapter 11 of 23
Several agents
By the end of this chapter you can let one agent's tool run another agent and count both in one usage, hand work from one agent to the next in your own code, route a farmer's message to a specialist agent with a router, and say when a team of small agents beats one agent with every tool. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world agents
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.
Why you need this
The farmer assistant has grown. It looks up prices and trends, reads the forecast, files claims, checks credit, books collections: ten tools. Ganesh forwards three hundred messages a day, and Kedar has noticed two things in the request log. Every request carries all ten tools' names, descriptions and parameters, so even "kanda bhav?" costs nine hundred input tokens before the farmer's words. And one set of instructions has to cover the market, the weather and the crop cover at once.
Sneha wants claims read the same way every time, by an agent that does nothing else. Pradnya wants price questions answered with a proper look at the trend, by a cheaper model if it does the job. The answer is not one bigger agent but several small ones. Pydantic AI has no special class for this: an agent is an object you can run from anywhere, including from another agent's tool, so a team of agents is ordinary Python.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
delegate.py: an agent behind a tool
Make a market analyst: an agent for openai-chat:lab-chat-mini with the instructions You are KhetBazaar's market analyst. Use the tools. and the tools khetbazaar.mandi_price and khetbazaar.price_trend.
Make the farmer assistant: an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's farmer assistant. Use the tools. and one tool, market_report, taking crop and mandi, whose docstring says it asks the market analyst about a crop's price and its trend at a mandi. The tool runs the analyst on Is the {crop} price at {mandi} going up since last week? (the crop and mandi filled in), counting the analyst's requests in the assistant's run, and returns the analyst's output.
Run the assistant on the farmer's message Pimpalgaon la tomato price kay ahe? Vadhla ka? and print its output, then a second line, Requests: and the number of requests the run's usage counted (every request, the analyst's included).
- Task 2
handoff.py: a hand-off in your code
Make a Pydantic model ClaimReport with farmer_id, crop, cause (text), acres (a float) and loss_date (text). The intake agent: openai-chat:lab-chat, output type ClaimReport, instructions Read the farmer's crop loss message. Today is 2026-10-23. The crop cover agent: openai-chat:lab-chat, the tool khetbazaar.file_claim, instructions You are KhetBazaar's crop cover specialist. Use the tools.
For each message, in order, run the intake agent. If the report's cause is not in khetbazaar.COVERED, print Not covered: and the cause. Otherwise run the crop cover agent on a prompt made from the report, such as File a claim for farmer F1016: 2.0 acres of tomato, unseasonal rain, on 2026-10-22. and print Filed: and its output.
The messages: Unseasonal rain spoilt 2 acres of my tomato on 22 Oct. I am F1016. and Drought dried 1.5 acres of my grapes on 21 Oct. I am F1001.
Last, print Requests: and the number of requests every run made together, counted in one usage object that each run is given.
- Task 3
router.py: a router and its specialists
Make a router: an agent for openai-chat:lab-chat whose output type is a Literal of price, weather and claim, with the instructions Route the farmer's message to one specialist: price, weather or claim.
Make three specialists for openai-chat:lab-chat, each with the instructions You are KhetBazaar's NAME specialist. Today is 2026-10-23. Use your tools. (NAME is price, weather or claim) and only its tools: price has khetbazaar.mandi_price and khetbazaar.price_trend; weather has khetbazaar.weather_forecast; claim has khetbazaar.file_claim and khetbazaar.claim_status.
For each message, in order, run the router, then run the specialist it chose on the farmer's own message, and print the label, a colon, a space and the specialist's output. The messages: Lasalgaon la dalimb bhav kay ahe?, Paus yeil ka? Mi Yeola madhe ahe. and What is the status of my claim KC4007?
- Task 4 · interview question
Interview: one_or_many.py, one agent or several?
Send the message Garpit hoil ka? Mi Niphad madhe ahe. to one agent for openai-chat:lab-chat with all ten functions of khetbazaar.FUNCTIONS as tools and the instructions You are KhetBazaar's farmer assistant. Today is 2026-10-23. Use your tools. Then send it to task 3's router and to task 3's weather specialist (same instructions and tools).
Print two lines: One agent, 10 tools: N input tokens in R requests (that run's usage), then Router and specialist: N input tokens in R requests (the two runs added together).
The interviewer will ask: one agent or several? What does a team cost, what does it buy, and what goes wrong when the router picks the wrong specialist?
What you will learn
- Delegation: an agent runs another agent inside one of its tools.
- Usage across agents: one run's count that includes both.
- Hand-off: your code runs one agent, then the next.
- A router: an agent whose output is a label that picks a specialist.
- One agent or several: what a team costs and what it buys.
More in Real-world agents
Build "Several agents" yourself
KhetBazaar, a Nashik agritech startup, answers farmers on WhatsApp from a bike. You build its farmer assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.