Pydantic AI · Chapter 12 of 23

A human's approval

By the end of this chapter you can make a tool wait for a person's approval, read the call that is waiting, resume the run with an approval, a rejection or changed arguments, ask for approval only when a rule says so, save a waiting run and resume it in another program, and explain why the run must stop rather than wait. 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 crop cover agent can now file claims, and Sneha has read rule 3.4 of the policy out loud: "A claim above ₹50,000 needs the regional manager's approval." KhetBazaar's cover pays ₹24,000 an acre, so any loss above about two acres is the regional manager's call. Until now, when the model asked for file_claim, Pydantic AI ran it: one farmer's message, and ₹72,000 of claim was filed before anyone looked.

The regional manager is in the field all day. He answers from his phone in an hour, or the next morning. A Python program cannot sit for a night holding a farmer's run, and a server restarts. So the run has to stop at the call, say what is waiting, be saved, and continue later, maybe in another program, with his answer. Pydantic AI calls these deferred tools.

What you build: 4 tasks

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

  1. Task 1

    approval.py: approval needed

    Make an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's crop cover assistant. Today is 2026-10-23. Use the tools. and one tool, file_claim, taking farmer_id, crop, cause, acres (a float) and loss_date, described as reporting a crop loss for the crop cover, that calls khetbazaar.file_claim. The tool needs approval: the agent may ask for it, but never runs it alone, and the run's output is either text or the calls that wait.

    Run the agent on Hail destroyed 3 acres of my grapes on 22 Oct. I am F1014. and print two lines: Waiting for approval: and the waiting call's tool name, then the call's arguments as JSON text, as the model sent them.

  2. Task 2

    decide.py: approve and reject

    Use task 1's agent and tool. For each message, run the agent, then resume the stopped run with the regional manager's decision for the waiting call, sending no new prompt.

    Hail destroyed 3 acres of my grapes on 22 Oct. I am F1014.: approve it. Unseasonal rain damaged 4 acres of my onion on 21 Oct. I am F1019.: reject it with the message The regional manager rejected this claim: send photos of the field first (policy 3.2).

    Print Approved: and the first resumed run's output, then Rejected: and the second resumed run's output.

  3. Task 3

    edit.py: approval only above Rs 50,000, and an edit

    Use task 1's agent, but file_claim decides for itself: when acres times 24000 is above 50000 and the call has not been approved yet, it asks for approval; otherwise it files the claim at once.

    Run the agent on Hail damaged 1.5 acres of my onion on 22 Oct. I am F1018. and print Filed: and its output.

    Then run it on Hail destroyed 4 acres of my grapes on 22 Oct. I am F1008. The inspection found only 2.5 acres damaged: approve the waiting call with the model's arguments but acres 2.5, and print Approved for 2.5 acres: and the resumed run's output.

  4. Task 4 · interview question

    Interview: overnight.py, why the run must stop

    Use task 1's agent and tool. The program takes one word on the command line. With start, it runs the agent on Pest attack destroyed 3 acres of my pomegranate on 21 Oct. I am F1016., saves the stopped run to pending.json in this folder (the run's messages and the waiting call's id), and prints Saved: waiting for approval of and the tool's name.

    With approve, it loads pending.json, approves the waiting call, resumes the run with the saved messages, and prints the resumed run's output. The check runs the two as separate programs.

    The interviewer will ask: why not wait with input() inside the tool? What is in pending.json, and what is not?

What you will learn

  • requires_approval: a tool the agent may ask for but never runs alone.
  • DeferredToolRequests: how a run ends when a call waits.
  • DeferredToolResults: resuming with approve, reject or edit.
  • ApprovalRequired: asking for approval only when a rule says so.
  • Saving a waiting run and resuming it in another program.

More in Real-world agents

Build "A human's approval" 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.