Document recovering a failed run's messages with `capture_run_messages` and wrapping up before `request_limit`

Merged
DO
1mo ago
+249-6

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 with message_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.md and the UsageLimitExceeded docstring 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" in TESTING-AND-DEBUGGING.md; gotchas for both; history_processors= now described as removed in v2 (raises TypeError).

Verification

  • recover_failed_run.py and wrap_up_near_request_limit.py run as test_docs_examples in tests/test_examples.py, with mock responses for get_order_status and search_web; -k 'agent.md or production.md or exceptions': 50 passed, 4 skipped.
  • test_docs_examples doesn't collect the skill's snippets, since it skips every .agents path (tests/test_examples.py, since #5271). Instead, inline scripts (not committed) checked them: both output validators retry and then succeed, the recovery snippet resumes after request_limit=1, Agent(history_processors=[]) raises TypeError, 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 -

2026-10-26 · Document the over-limit response gap instead of fixing it here · iter -

2026-10-26 · pre-push review 1 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -

2026-10-26 · pre-push review 1 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -

2026-10-26 · pre-push review 2 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -

2026-10-26 · pre-push review 2 of 3 at da671018abe421240fddb3bd0cb7bdae726d76cc · iter -

2026-10-26 · pre-push review 3 of 3 at 8e798676b1313f3ae1447a023893b2bcc5065879 · iter -

2026-10-26 · pre-push review 3 of 3 at 8e798676b1313f3ae1447a023893b2bcc5065879 · iter -

2026-10-26 · Additional review surface: pydanty:review-lite · iter -

2026-10-26 · Push 2bf866aea87075c2b23aeafb274f200268883460 without local re-review · iter -

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 -

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 -

2026-10-26 · Refute pydanty re-review: skill snippets not executed · iter -

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.