Pydantic AI · Chapter 16 of 23

Toolsets

By the end of this chapter you can group tools into a toolset and give it to any agent, show a farmer only the tools that fit them, combine toolsets and shape them as one, and say why an agent with fewer tools works better. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Graphs
  • 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 to ten tools: prices, trends, the forecast, inputs, orders, credit, collections and claims. Every agent so far has had its tools one by one, written on the agent itself. Now three teams want agents of their own. Ganesh's field app needs prices and the forecast. Sneha wants claims shown only to farmers who have crop cover, and no ordering for a farmer who has used up their credit. The website wants an assistant that can look things up and never change anything.

Copying tools between agents does not scale, and neither does one agent with everything. Kedar wants tools in named groups that he can hand to an agent, narrow per farmer, and put together like bricks. In Pydantic AI such a group is a toolset. (You met one already: chapter 15's MCPToolset is a toolset whose tools live in a server.)

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    toolset.py: a toolset of prices

    Make a FunctionToolset called prices whose tools are two of khetbazaar's functions, mandi_price and price_trend. Make an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's farmer assistant. and that toolset (no tools written on the agent itself), run it on the farmer's question exactly:

    Pimpalgaon la kanda bhav last week pasun vadhla ka?

    and print the run's output.

  2. Task 2

    per_farmer.py: tools per farmer

    Make an agent for openai-chat:lab-chat with deps of a Farmer dataclass (one field, farmer_id), the instructions You are KhetBazaar's farmer assistant. Today is 2026-10-23. and two toolsets:

    everyday: khetbazaar's mandi_price and weather_forecast, for every farmer;

    claims: file_claim(crop, cause, acres, loss_date) and claim_status(claim_id), which take the farmer from the run's deps and call khetbazaar, shown only to a farmer whose profile from khetbazaar.get_farmer has crop_cover.

    Run it for F1014, then F1012, on the message Hail destroyed my grapes on 22 Oct, 2 acres. Claim karaycha ahe. and print one line per farmer: the farmer id, a colon and a space, then the output.

  3. Task 3

    combined.py: one toolset for the website

    Make three FunctionToolsets of khetbazaar's functions: prices (mandi_price, price_trend), inputs (find_inputs, place_order) and claims (claim_status, file_claim). Combine them into one with CombinedToolset, and filter that once so place_order and file_claim, the tools that change something, are hidden. Give it to an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's website assistant., run it on KC4015 claim cha status kay ahe? and print the run's output.

  4. Task 4 · interview question

    Interview: fewer_tools.py, why fewer tools help

    Make an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's farmer assistant. and run it twice on Pimpalgaon la tamatar bhav kay ahe?: first with a toolset of all ten functions in khetbazaar.FUNCTIONS, then with a toolset of only mandi_price and price_trend. Print one line per run, from its usage: 10 tools: N input tokens, then 2 tools: N input tokens.

    The interviewer will ask: the answer was the same, so why do fewer tools help, and how would you choose the tools for each message?

What you will learn

  • FunctionToolset: a group of plain functions as tools.
  • Toolsets per agent, and per run.
  • filtered and prepared: deciding which tools a run sees.
  • CombinedToolset, prefixed and renamed: many toolsets as one.
  • Why fewer tools help.

More in Graphs

Build "Toolsets" 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.