Pydantic AI · Chapter 3 of 23
Tools
By the end of this chapter you can turn a Python function into a tool an agent can use, explain what the model really receives about it, follow the run loop (request, tool call, tool return, answer) in the run's messages, give one agent several tools, and say who actually runs a tool. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Your first 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
In chapter 1 a farmer asked "Is there rain coming in Niphad this week?" and the agent said "No rain is expected in Nashik district this week." KhetBazaar's forecast has 34 mm in Niphad on the 25th. Ganesh has seen what happens next: a farmer leaves his harvested onion in the field.
The model was not lying; it had never seen the forecast. KhetBazaar has the forecast, today's mandi prices and every farmer's account, in khetbazaar.py's plain functions. Chapter 2 got typed answers out of the model. This chapter gets KhetBazaar's facts in: the agent asks your code to look things up, and answers from what comes back.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
price_tool.py: a price tool
Give an agent for openai-chat:lab-chat, with the instructions You are KhetBazaar's farmer assistant., a tool named mandi_price. It takes a crop and a mandi (both str, with type hints), has a docstring that says what it returns, and returns khetbazaar.mandi_price for them.
Run the agent on the farmer's question exactly:
What is today's onion rate in Pimpalgaon?
Print the output, then a second line: Requests: and the number of requests the run made, from its usage.
- Task 2
two_tools.py: two tools, the right one each time
Give one agent for openai-chat:lab-chat, with the instructions You are KhetBazaar's farmer assistant., two tools: mandi_price (crop and mandi, returning khetbazaar.mandi_price) and weather_forecast (taluka, returning khetbazaar.weather_forecast), each with a docstring.
Run it on two questions, in this order, and print each run's output on its own line:
tamatar ka bhav kitna chal raha hai Nashik mandi mein
Will it rain in Sinnar this week?
- Task 3
run_messages.py: read the run's messages
Give an agent for openai-chat:lab-chat, with no instructions, a tool named price_trend that takes a crop and a mandi and returns khetbazaar.price_trend for them. Run it on:
Has the onion rate in Pimpalgaon gone up since last week?
Then go through the run's messages, all of them, and print one line per part: the message's class name, a space and the part's kind; after a tool-call or tool-return part, also a space and the tool's name (for example: ModelResponse tool-call weather_forecast).
- Task 4 · interview question
Interview: never_runs.py, who runs the tool?
Give an agent for openai-chat:lab-chat, with the instructions You are KhetBazaar's farmer assistant., a weather_forecast tool (taluka, returning khetbazaar.weather_forecast). Inside the tool, before returning, print: Tool ran: weather_forecast(taluka), with the taluka it was given (for example: Tool ran: weather_forecast(Niphad)).
Run the agent on the farmer's question garpit hoil ka Yeola madhe? and print the output after the run.
The interviewer will ask who runs a tool, the model or your program, and how you know.
What you will learn
- A tool: a function with type hints and a docstring.
- @agent.tool_plain: registering it on an agent.
- The run loop: request, tool call, tool return, answer, read from all_messages().
- Several tools: the model picks by name and description.
More in Your first agents
Build "Tools" 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.