Preserve rejected-response usage when an outer `FallbackModel` handles an exception
Closed
XX
23d ago+55+55+01234567890123456789-6-6-0123456789
Closes #12853
Why we make these changes
When an inner FallbackModel rejects a response and then raises an exception handled only by an outer fallback, AttemptLog.record() drops the attempts carried on that exception. Preserve those records using carried_attempts(), alongside the existing response and exception-group paths.
New public surface
none
User-visible behavior
A rejected response billed at 120 input tokens / 0.012 cost followed by a successful outer backup at 45 / 0.0045 previously reported only 45 / 0.0045. It now reports 165 / 0.0165, and token limits of 150 or cost limits of 0.015 raise UsageLimitExceeded.
Verification
- Extended nested attempt preservation to cover unhandled inner exceptions.
- Added accounting and limit regressions through both
FallbackModelandModelFallback, including a passing budget that would fail if rejected usage were counted twice. - Before the fix: the nested test selection had 5 failures and 5 passes. After: all 10 pass; the full fallback test file passes 143 tests.
- An additional 24-case offline matrix passes. Changed-file Pyright,
make format lint, and all pre-commit hooks pass. Full repository CI, including coverage, passed on5f63067.
What changes for existing users
Nested fallback runs retain all billed usage and enforce the configured token and cost limits without double-counting.
PR decisions
2026-11-08 · pre-push review 1 of 3 at 5f6306727a14fcbd48b5426a184182cb79c5d54b · iter -
- Decision: Reserved fresh independent review dispatch 1 of 3.
- Why: Tree 2343abfbf4bba7fe0932c2c74fe50c9c6541d7f1; full branch diff a355f7a80ebcafeea1baa21b005fd91bcd13f58c..5f6306727a14fcbd48b5426a184182cb79c5d54b.
- Source: https://github.com/xxx-x0/pydantic-ai/tree/fix/nested-fallback-carried-usage
- Session: 01a1bba0-283b-71f0-9668-f498d52a801e
2026-11-08 · pre-push review 1 of 3 at 5f6306727a14fcbd48b5426a184182cb79c5d54b · iter -
- Decision: No findings; current at 5f6306727a14fcbd48b5426a184182cb79c5d54b.
- Why: Reviewed tree 2343abfbf4bba7fe0932c2c74fe50c9c6541d7f1; full branch diff a355f7a80ebcafeea1baa21b005fd91bcd13f58c..5f6306727a14fcbd48b5426a184182cb79c5d54b under target-branch instructions.
- Source: https://github.com/xxx-x0/pydantic-ai/tree/fix/nested-fallback-carried-usage
- Session: 01a1bba0-283b-71f0-9668-f498d52a801e
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. (Not applicable: restores documented usage accounting without a public API change.)
- PR title is fit for the release changelog.