What this thread does
Do first: from pydantic_ai.capabilities import HandleDeferredToolCalls.
When you need it
Use a deferred-tool handler when a policy can decide approvals inside the process. The page's refund example approves amounts up to $100, denies larger amounts, and lets the same run continue.
Steps
1. Import the policy's types and capability:
from pydantic_ai import Agent, DeferredToolRequests, DeferredToolResults, RunContext
from pydantic_ai.capabilities import HandleDeferredToolCalls
2. Build per-call decisions from validated arguments and record the policy label:
def refund_policy(ctx: RunContext, requests: DeferredToolRequests) -> DeferredToolResults:
return requests.build_results(
approvals={
call.tool_call_id: requests.validated_args[call.tool_call_id]['amount'] <= 100
for call in requests.approvals
},
answered_by={call.tool_call_id: 'refund-policy' for call in requests.approvals},
)
3. Attach the capability and mark the example tool as requiring approval:
agent = Agent('openai:gpt-6.1-sol', capabilities=[HandleDeferredToolCalls(refund_policy)])
@agent.tool_plain(requires_approval=True)
def issue_refund(order_id: str, amount: int) -> str:
return f'Refunded ${amount} for order {order_id}'
4. Use the page's example request:
result = agent.run_sync('Refund order 1234, it arrived broken.')
print(result.output)
Check it worked
The page's example output says it refunded $50 for order 1234. The example tool only returns a message. Approved calls execute, denied calls are reported to the model, and the same run continues. The policy label appears in DeferredToolResultsEvent.
Limits
For this capability, the page does not name a version. Pydantic AI reports answered_by without checking it. A handler without a token runs again during durable replay; the page says to add a required token parameter if the engine should record and reuse its answer. run_stream() ends on approval-required or external tools without running deferred handlers; use run(), iter(), or run_stream_events() for those tools.
Not for: explaining why a shared toolset acquired approval requirements; see pydantic-ai-shared-tool-approval-mutation.
Sources
- Handle Deferred Tool Calls, Pydantic AI documentation, read 2026-11-24: "This handler approves refunds up to $100 itself and denies the rest"
Checked
2026-11-24; read <https://pydantic.dev/docs/ai/capabilities/handle-deferred-tool-calls/index.md>; no command was run.
Started from a post by @Pydantic, packaged by the editor. Original post