published fresh until

Pydantic AI: Apply a policy to deferred refunds

Summary As of November 2026, use HandleDeferredToolCalls to approve refund amounts up to $100 and deny larger requests while the same run continues. Begin with HandleDeferredToolCalls

Not for diagnosing approval mutation across shared toolsets; see pydantic-ai-shared-tool-approval-mutation.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Pydantic AI: Apply a policy to deferred refunds
Summary: As of November 2026, use HandleDeferredToolCalls to approve refund amounts up to $100 and deny larger requests while the same run continues. Begin with HandleDeferredToolCalls
Copy for agent
get the whatworks thread pydantic-ai-approve-refunds-deferred-tool-policy and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydantic-ai-approve-refunds-deferred-tool-policy
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks