Charge `SpendLimits` budgets for responses a `FallbackModel` rejected
SpendLimits has never charged for a response a FallbackModel rejected with a fallback_on response handler. The provider billed it, and since #8745 core adds its tokens and cost to RunUsage and the token/cost UsageLimits. SpendLimits only accrues the responses on the request context, though, so spend budgets under-count fallback chains that reject responses.
What changes:
-
Core:
ModelRequestContextgets a private_usage_attemptsnext to_usage_responses(same ledger, soreplace()copies share it). When every model of aFallbackModelfails on a non-streaming request, core appends theFallbackExceptionGroup.attemptsto it in_recover_model_request_error, before anyon_model_request_errorhook runs, which is inside the wrapped handler and so beforewrap_model_requestunwinds. Streamed requests record nothing:FallbackModelonly rejects responses outside streaming, so a failed stream's attempts never carry usage.A nested
FallbackModelalready flattens its attempts into the outer group, so each attempt is appended once. A request that doesn't fail with a group is unaffected. -
SpendLimits.wrap_model_requestaccrues every billed attempt (usage is not None):- from each accrued response's
failed_attempts, ahead of the response that carries it, mirroring core's_record_attempts_usage; - from
_usage_attempts, also when an error hook recovered from the group.
It adds no hooks, so an agent without a
FallbackModelhas no new step in its error path. - from each accrued response's
-
Pricing: an attempt is priced as a
ModelResponsewith the attempt's model, provider, usage and timestamp, but no parts. A user'spricestays authoritative and prices it the way it would price the accepted response. WhenpricereturnsNone(or isn't set), the attempt'susage.cost(which core filled from the rejected response, including its provider URL) is used if it is finite and not negative; otherwise the registry prices it. -
SpendRecordedEventgainsfailed_attempt: bool = False, which isTruefor an event accrued from a rejected attempt, so listeners can tell those apart from the answer. -
Each attempt counts as one
Spent.requests, sounpriced_requestsstays a subset of it. Core'sRunUsage.requestscounts only the responses the agent acted on, so the two can differ; this is documented onSpent.requests. -
Replay tokens: an attempt's token extends its response's token with its position. Attempts with no response are identified by run id, run step and a digest of the attempts. Attempt timestamps and durations come from the local clock, so they are left out of both. A response without
failed_attemptskeeps its existing token. -
Error ordering is unchanged: each accrual's pricing or callback error is collected, and only the first one is raised, only on the success path.
-
Part of #9335
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code