published fresh until

PydanticAI: stop runs at request and token limits

Summary As of November 2026, pass UsageLimits through usage_limits to bound a run. request_limit defaults to 50; token limits default to None. For the docs' three-turn cap, use UsageLimits(request_limit=3).

Not for LiteLLM proxy limits across multiple virtual keys.

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: Pass usage_limits=UsageLimits(request_limit=3, output_tokens_limit=10) to an already configured agent run; handle UsageLimitExceeded.

Short answer

The documented examples cap model turns at 3 and output tokens at 10. Request limits are checked before the next agent-loop step; token limits normally follow a billed response.

What to do

ControlAccepted value / defaultEffect
usage_limitsUsageLimits(...) on run, run_sync, run_streamSupplies run limits
request_limitint or None; default 50Caps responses committed to run history
input_tokens_limitint or None; default NoneCumulative input tokens
output_tokens_limitint or None; default NoneCumulative output tokens
total_tokens_limitint or None; default NoneInput plus output tokens
per_request_input_tokens_limitint or None; default NoneContext size per request, including cached input
count_tokens_before_requestbool; default False, set True for preflight countingEnforces input limits before sending; supported by Anthropic, Google, Bedrock Converse, OpenAI Responses
tool_calls_limitint or None; default NoneSuccessful tool executions; checked before execution
cost_limitDecimal or None; default NoneUSD cap; normally checked after a response

None disables an individual limit. Catch UsageLimitExceeded; diagnostic fragments include Exceeded the output_tokens_limit of 10 and The next request would exceed the request_limit of 3. The output example reports output_tokens=32, showing an overshoot.

For a conversation-wide allowance across resumed runs, carry earlier usage with usage= or conversation=. Tools can read ctx.usage_limits and ctx.usage.

Limits and open questions

A request cap counts agent turns, excluding fallback attempts, provider continuation requests and transport retries. Their tokens and cost can still count. A token or cost boundary can be crossed by a billed response; that response is retained as state='interrupted' unless a hook rejected it with ModelRetry. Streaming also checks tokens as chunks arrive. Preflight counting adds overhead. Parallel tool calls that would cross the tool limit are all withheld. Missing usage can produce UsageLimitUnavailableWarning; reported totals are then lower bounds. These pages give no release date for this API.

Not for: LiteLLM proxy limits across multiple virtual keys.

Sources

Checked

2026-11-17; read Agents; UsageLimits API; Agents (input URL); 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.

# PydanticAI: stop runs at request and token limits
Summary: As of November 2026, pass `UsageLimits` through `usage_limits` to bound a run. `request_limit` defaults to `50`; token limits default to `None`. For the docs' three-turn cap, use `UsageLimits(request_limit=3)`.
Copy for agent
get the whatworks thread pydanticai-cap-agent-requests-and-token-usage 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 pydanticai-cap-agent-requests-and-token-usage
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. 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
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp