Practical technical guides / Recent development: openai-agents-python 0.25.0, released 18 November 2026

OpenAI Agents SDK 0.25.0 Counts Tokens From Failed and Incomplete Attempts

Version 0.25.0 keeps provider-reported usage from failed and incomplete Responses calls, once per attempt. Why the old totals were low and what to change.

By Sandeep Belgavi · Published 2026-11-22

Version 0.25.0 keeps provider-reported usage from failed and incomplete Responses calls, once per attempt. Why the old totals were low and what to change.

The release and the fix

Version 0.25.0 of the OpenAI Agents SDK for Python was published on 18 November 2026; its release notes are dated 17 November. Most of the highlights are about removing a sandbox provider, checkpoint changes and model defaults. One line at the end of the highlights is about money: failed and incomplete Responses calls now retain provider-reported token usage.

The fix is pull request 6046, "preserve usage from failed and incomplete Responses calls", merged on 16 November. Its description says it fixes an undercount when the Responses API returns a failed or incomplete response with usage: HTTP and WebSocket runs record the provider's counts in run usage and in the SDK's response span before raising ModelBehaviorError, and when a non-streaming retry succeeds or exhausts its budget, each attempt's reported usage is counted once.

This guide reads that change, explains why the old numbers were low, reproduces the accounting in a small model, and turns it into rules for any agent runtime that retries model calls and enforces token budgets.

Where usage is read

The SDK's usage documentation describes the counters a run keeps: requests, the number of model API calls; input, output and total tokens; and request_usage_entries, a per-request breakdown. After a run, they are available on result.context_wrapper.usage. Applications use them to display cost, enforce limits and record analytics, so an undercount there is an undercount everywhere downstream.

Why failed attempts used to count as zero

Before the fix, the retry helper apply_retry_attempt_usage handled a successful call that needed retries by adding one zero-token entry per failed attempt in front of the successful entry, and adding the failed attempts to the request count. The requests were counted; their tokens were not. That is reasonable when a failed attempt is a connection error, because the provider reported nothing. It is wrong when the provider returned a response with status incomplete or failed and a usage object: those tokens were processed and, under usual provider pricing, billed.

The fix introduces a map from attempt number to reported usage. When an attempt fails with a ModelBehaviorError that carries terminal usage, the usage is stored under that attempt. When the retry loop finishes with a success, each failed attempt contributes its reported usage if there is one, and a zero entry only if there is none. If the loop exits with an exception instead, a context manager adds the recorded usage to the run's totals before the exception propagates, so a call that never succeeds still shows its cost. The streaming path adds terminal usage to the run before raising.

The SDK's own regression test uses an incomplete first attempt with 12 input and 16 output tokens, followed by a retry.

A model of the accounting

The companion example is a standalone model of the same rules; it does not import the SDK or call a provider. One model call of a help-desk agent at the fictional Belgavi's Book Store takes three physical attempts:

  1. Incomplete, with reported usage of 12 input and 16 output tokens.
  2. A connection reset, with no usage reported.
  3. Completed, with 12 input and 24 output tokens.

Four ways to count it:

  • Success only, the old behavior: 3 requests, 36 tokens.
  • Per attempt, keyed by attempt number, the new behavior: 3 requests, 64 tokens, and one attempt flagged as having no reported usage.
  • A tempting bug, adding usage when the error is raised and again when the retry loop finishes: 92 tokens.
  • Two incomplete attempts and then giving up: 0 tokens before the fix, 58 after.

A budget check makes the difference concrete. With a 60-token limit for this step, the old ledger says 36 and lets the agent continue; the new one says 64 and stops it. The agent was over budget in both cases; only one ledger knew.

def per_attempt(attempts):
    seen, unreported = {}, 0
    for a in attempts:
        if a['usage'] is None:
            unreported += 1
            continue
        seen[a['n']] = a['usage']          # keyed, so a replayed attempt is not added twice
    tokens = sum(u['input'] + u['output'] for u in seen.values())
    return {'requests': len(attempts), 'tokens': tokens, 'unreported_attempts': unreported}

Why the keying matters

Usage can reach an accounting layer by two routes: attached to the exception when an attempt fails, and assembled by the retry loop when it finishes. Adding at both points double-counts, which the model shows as 92 instead of 64. The SDK's approach, a dictionary from attempt number to usage filled at error time and consumed once at completion, makes each attempt's usage idempotent. The same idea applies to any runtime: give every physical attempt an identity, and let the ledger accept each identity once.

What to do in your application

  1. Upgrade with the rest of the 0.25.0 changes in view. The release notes ask Runloop sandbox users to migrate first, change checkpoint handling and say applications sharing checkpoints across workers should upgrade readers first. Usage accounting has no migration step of its own.
  2. Expect higher reported numbers for runs with retries, incomplete responses or errors. That is a correction, not a regression. Note the version change next to any cost dashboard so the step is explained.
  3. Revisit token budgets. A budget tuned against undercounted numbers was effectively higher than written. Decide whether to keep the old effective limit or the written one.
  4. Count unreported attempts separately. Attempts with no usage, such as connection resets, are real requests with unknown cost. Show them as a count, not as zero tokens.
  5. Reconcile with the provider. Client-side usage is an estimate assembled from responses. Compare it with the provider's usage reports periodically; a persistent gap points to another route where usage is lost.

Beyond this SDK

The same undercount appears wherever a runtime keeps usage only from successful responses: in custom retry wrappers, in gateways that log usage after a response is accepted, and in agent loops that discard a truncated answer and retry with a larger output limit. Three checks find it:

  • Force an incomplete response, for example with a very small output token limit, and confirm the ledger shows its tokens.
  • Force a retry that eventually succeeds and confirm the ledger's total equals the sum of the attempts.
  • Force every attempt to fail and confirm the run's usage is not zero.

Each check is cheap, deterministic with a mocked transport, and catches a class of cost error that dashboards do not reveal.

Requests, attempts and calls

Three counts are easy to conflate. A model call is what the agent loop asks for once: "get the next response". An attempt is one physical request to the provider; a call with retries has several. A request in the SDK's requests counter corresponds to attempts, which is why the old code already added failed attempts to the request count while leaving their tokens at zero. Dashboards that divide tokens by requests to get "tokens per request" were therefore dividing an undercounted numerator by a correct denominator, which made retried calls look cheap. After the fix both numbers describe the same attempts. If you report cost per agent step, aggregate by call, and keep the attempt count beside it as a measure of how much retries are costing.

Practical implications

For SDK users on 0.24 or earlier, reported usage under retries and incomplete responses was low by the tokens of those attempts; how much depends entirely on how often your runs retry. After upgrading, the numbers are higher and closer to what the provider bills. If you enforce budgets inside agent runs, they become stricter in practice. No configuration is needed to get the new behavior.

Verification

The companion example's six tests pin the four ledgers above, the budget decision flipping from continue to stop, and that replaying an attempt does not add its usage twice. Its README states the limits: it models the accounting rules described in the pull request, and provider billing remains the authority.

Run the example yourself

The three-attempt call, the four ledgers, the budget check and six tests. It does not call the SDK or a provider. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Primary references and authorship

Sources read 22 November 2026. Original explanation, measurements and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.

Related on this site: