Charge `SpendLimits` budgets for responses a `FallbackModel` rejected

Merged
DO
2mo ago
+525-36

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: ModelRequestContext gets a private _usage_attempts next to _usage_responses (same ledger, so replace() copies share it). When every model of a FallbackModel fails on a non-streaming request, core appends the FallbackExceptionGroup.attempts to it in _recover_model_request_error, before any on_model_request_error hook runs, which is inside the wrapped handler and so before wrap_model_request unwinds. Streamed requests record nothing: FallbackModel only rejects responses outside streaming, so a failed stream's attempts never carry usage.

    A nested FallbackModel already 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_request accrues 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 FallbackModel has no new step in its error path.

  • Pricing: an attempt is priced as a ModelResponse with the attempt's model, provider, usage and timestamp, but no parts. A user's price stays authoritative and prices it the way it would price the accepted response. When price returns None (or isn't set), the attempt's usage.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.

  • SpendRecordedEvent gains failed_attempt: bool = False, which is True for an event accrued from a rejected attempt, so listeners can tell those apart from the answer.

  • Each attempt counts as one Spent.requests, so unpriced_requests stays a subset of it. Core's RunUsage.requests counts only the responses the agent acted on, so the two can differ; this is documented on Spent.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_attempts keeps 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

  • Follow-up to #8899 / #8745

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