Document recovering a failed run's messages with `capture_run_messages` and wrapping up before `request_limit`
Posted by Claude Code on Douwe's behalf.
Closes #11376
Why we make these changes
Reported by a user: a run that raises UsageLimitExceeded returns nothing, so the work it did looks lost. capture_run_messages already recovers it, but it was only documented under model errors. This PR documents that recovery path for every run error, links it from where users hit limits, and adds a recipe for wrapping up before request_limit. It also takes three building-skill items from #11382 (related, stays open): output validators, failed-run recovery, and the stale history_processors gotcha.
The response that crosses a token or cost limit is billed but missing from the captured messages; the docs say so and link #11397, which tracks the fix.
New public surface
None. Docs, skill, and the UsageLimitExceeded docstring only.
What's documented
docs/agent.md: new "Recovering a failed run's messages" section (capture_run_messages, then resume withmessage_history=and no prompt); a "Wrapping up before the request limit" capability recipe, run end to end in its example, that tells the model how many requests are left via a request-scoped prompt and drops function tools on the last request; a pointer from the usage-limits intro.docs/production.mdand theUsageLimitExceededdocstring link to the recovery section.- Building skill: "Validate Output Against Your Domain" (ids checked against deps; an async validator that checks all ids in one batched database lookup) in
AGENTS-CORE.md; "Recover a Failed Run" inTESTING-AND-DEBUGGING.md; gotchas for both;history_processors=now described as removed in v2 (raisesTypeError).
Verification
recover_failed_run.pyandwrap_up_near_request_limit.pyrun astest_docs_examplesintests/test_examples.py, with mock responses forget_order_statusandsearch_web;-k 'agent.md or production.md or exceptions': 50 passed, 4 skipped.test_docs_examplesdoesn't collect the skill's snippets, since it skips every.agentspath (tests/test_examples.py, since #5271). Instead, inline scripts (not committed) checked them: both output validators retry and then succeed, the recovery snippet resumes afterrequest_limit=1,Agent(history_processors=[])raisesTypeError, and the wrap-up capability drops function tools on the last allowed request.
What changes for existing users
Nothing.
Open decisions
None. Settled: this PR merges before #11413, which then updates the "billed but not captured" sentences (decision).
PR decisions
2026-10-26 · Take only three #11382 items; reference it as related · iter -
- Decision: Skill gets output validators, failed-run recovery, and the v2 history_processors fix; #11382 stays open for the rest
- Why: Those items overlap #11376's recovery docs; the rest of #11382 is independent skill work
- Source: https://github.com/pydantic/pydantic-ai/issues/11382
2026-10-26 · Document the over-limit response gap instead of fixing it here · iter -
- Decision: Docs state the billed over-limit response isn't captured and link #11397; the runtime fix is out of scope
- Why: Recording it needs a design decision (plain ModelResponse vs interrupted marker, new_messages semantics)
- Source: https://github.com/pydantic/pydantic-ai/issues/11397
2026-10-26 · pre-push review 1 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -
- Decision: reserved; dispatching
- Why: tree 99e24934aedd1da9c62b33aee7514dc510de2c8a; scope 93406bbfbc651682f646f9eda4818216b43b5381..da671018abe421240fddb3bd0cb7bdae726d76cc (full branch, first review)
- Source: https://github.com/pydantic/pydantic-ai/tree/docs-run-recovery-and-skill-fixes
2026-10-26 · pre-push review 1 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -
- Decision: no review produced: read-only Explore reviewer's file tools are confined to another checkout and refused the /tmp policy-base bundle; counted against budget
- Why: tree 99e24934aedd1da9c62b33aee7514dc510de2c8a; scope 93406bbfbc..da671018ab
- Source: https://github.com/pydantic/pydantic-ai/tree/docs-run-recovery-and-skill-fixes
2026-10-26 · pre-push review 2 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -
- Decision: reserved; dispatching fresh no-history reviewer with shell read access
- Why: tree 99e24934aedd1da9c62b33aee7514dc510de2c8a; scope 93406bbfbc..da671018ab (full branch); unchanged since call 1
- Source: https://github.com/pydantic/pydantic-ai/tree/docs-run-recovery-and-skill-fixes
2026-10-26 · pre-push review 2 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -
- Decision: remediated: SKILL.md gotcha now excepts RunCancelled (its all_messages() holds the messages), matching the docs and reference
- Why: tree 99e24934aedd1da9c62b33aee7514dc510de2c8a; scope 93406bbfbc..da671018ab; one finding, valid (exceptions.py RunCancelled.all_messages)
- Source: https://github.com/pydantic/pydantic-ai/tree/docs-run-recovery-and-skill-fixes
2026-10-26 · pre-push review 3 of 3 at 8e798676b1313f3ae1447a023893b2bcc5065879 · iter -
- Decision: reserved; dispatching follow-up
- Why: tree 4f7c1b795608dfef1a25bf24ca40e383378f4e5c; scope da671018ab..8e798676b1313f3ae1447a023893b2bcc5065879 (remediation of call 2) plus full branch as context
- Source: https://github.com/pydantic/pydantic-ai/tree/docs-run-recovery-and-skill-fixes
2026-10-26 · pre-push review 3 of 3 at 8e798676b1313f3ae1447a023893b2bcc5065879 · iter -
- Decision: no findings (current at 8e798676b1313f3ae1447a023893b2bcc5065879); prior RunCancelled finding verified fixed
- Why: tree 4f7c1b795608dfef1a25bf24ca40e383378f4e5c; scope da671018ab..8e798676b1313f3ae1447a023893b2bcc5065879 with full branch as context; budget exhausted
- Source: https://github.com/pydantic/pydantic-ai/tree/docs-run-recovery-and-skill-fixes
2026-10-26 · Additional review surface: pydanty:review-lite · iter -
- Decision: Applied pydanty:review-lite at 8e798676b1313f3ae1447a023893b2bcc5065879
- Why: Douwe approved the recommended surface
- Source: https://github.com/pydantic/pydantic-ai/pull/11406
2026-10-26 · Push 2bf866aea87075c2b23aeafb274f200268883460 without local re-review · iter -
- Decision: pydanty:review-lite remediation (wrap-up example runs end to end; validator example uses one batched DB lookup) pushed un-reviewed locally: budget exhausted (3 of 3)
- Why: tree 728895d3f4c250cb0ae404020f32c186b38b64b5; delta 8e798676b1..2bf866aea87075c2b23aeafb274f200268883460; targeted test_examples + inline validator check pass
- Source: https://github.com/pydantic/pydantic-ai/pull/11406#issuecomment-6426656325
2026-10-26 · Open: merge order with #11413 · iter -
- Decision: Recommend merging #11406 first; #11413 then replaces the 'billed but not captured' sentences in docs/agent.md recovery section and the skill's TESTING-AND-DEBUGGING.md. If #11413 lands first, this PR drops them
- Why: Both sentences are true on main today and become stale once #11413 merges; #11413 deliberately leaves the recovery section to this PR
- Source: https://github.com/pydantic/pydantic-ai/pull/11413
2026-10-26 · Refute streaming-fixture finding · iter -
- Decision: No stream_model_logic entry for get_order_status
- Why: recover_failed_run.py only calls run_sync, which uses model_logic; no example streams that prompt
- Source: https://github.com/pydantic/pydantic-ai/pull/11406#issuecomment-6426656325
2026-10-26 · Merge origin/main at bf2779cedd22d6b71ee6e530833686e39791605d · iter -
- Decision: Resolved SKILL.md routing-table conflict: main's 'multimodal or structured input' rows + this PR's failed-run recovery rows
- Why: PR was DIRTY; conflict resolution is not an exempt delta, pushed without local re-review because the budget is exhausted (3 of 3); targeted test_examples pass
- Source: https://github.com/pydantic/pydantic-ai/pull/11406
2026-10-26 · Merge order with #11413 (decided) · iter -
- Decision: #11406 merges first; #11413 then updates the 'billed but not captured' sentences in docs/agent.md and the skill's TESTING-AND-DEBUGGING.md
- Why: Douwe chose option A
- Source: https://github.com/pydantic/pydantic-ai/pull/11406#issuecomment-6428198340
- Supersedes: Open: merge order with #11413
2026-10-26 · Refute pydanty re-review: skill snippets not executed · iter -
- Decision: No code change; PR body now states test_docs_examples skips .agents paths; pydanty review at bf2779c dismissed (lead-authorized)
- Why: Body never claimed skill snippets run under tests; the .agents skip is deliberate (tests/test_examples.py L141-143, #5271)
- Source: https://github.com/pydantic/pydantic-ai/pull/11406#issuecomment-6429042037
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.