You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Synthesize or estimate usage when an upstream provider omits it, so Codex clients keep context accounting #7427
Keep Codex clients' context meter and context accounting correct on routes whose upstream provider does not return a usage object, so long-lived Codex threads show their real context occupancy and are never rejected as out of room by missing accounting.
What prevents this today?
In passthrough routes, when the upstream response carries no usage, the proxy forwards none. Codex then has nothing to account with: its client shows no context usage, and its in-server accounting degrades — in our production case this contributed to threads being rejected with "ran out of room" while their real context was ~15% of the window.
What should OpenCodex do?
When an upstream omits usage on a route, synthesize or estimate it (for example from tokenized request/response sizes, the way the Kiro adapter already rewrites totals) and include it in the response usage, so downstream clients always receive an accounting signal.
Example usage or interface
POST /v1/responses (route to provider X, upstream response without usage)
-> OpenCodex response includes:
"usage": { "input_tokens": <estimated>, "output_tokens": <estimated>, "total_tokens": <estimated> }
Alternatives or workarounds
Restarting the Codex app-server recomputes accounting from history, but that is a manual recovery for a proxy-side gap, and it interrupts live sessions.
Additional context
Observed in production on OpenCodex 2.84.0 (VM) with Codex app-server 0.163.0 clients. The Kiro adapter's total-rewriting is the in-codebase precedent for touching usage. Reported by RHODIZ IT LLC.
Checks
I searched existing issues and documentation.
This request describes a concrete OpenCodex workflow rather than merely naming a desired technology.
Checked against trusted dev 12025981e5a304cdb6b1b54f525ba8554f623977. Keep this as a protocol/accounting design decision. A local token estimate is not upstream-reported usage or a measurement of Codex's full active context; making it look authoritative can corrupt quota/cost accounting as well as the context meter. Existing logging distinguishes estimates from reported evidence. A proposal must name supported tokenization/model families, omitted-versus-zero semantics, streaming terminal placement, cache/reasoning/tool accounting, provenance and opt-in behavior. It must preserve actual usage unchanged and avoid estimating a billing balance from bytes. No synthesized wire usage is enabled by this triage.
Design proposal: estimated usage when an upstream omits it
Following @Ingwannu's triage, this is a proposal only. Nothing below is implemented, and no synthesized wire usage is enabled by it.
What exists today
src/lib/token-estimate.ts already produces a script-segmented heuristic estimate, and the Kiro adapter (src/adapters/kiro/usage.ts, stream.ts) uses it to fill response.completed.usage because Kiro reports nothing.
src/usage/log.ts:63 already distinguishes reported | unreported | unsupported | estimated, and the request log keys estimated-usage detection off the route adapter.
The OpenAI Responses passthrough (src/adapters/openai-responses/passthrough.ts around line 646) forwards usage only when the upstream sent one. That is the gap the issue describes.
Options
Status quo plus visibility. Keep the wire untouched and only surface "usage not reported" in the request log and dashboard. Zero accounting risk, but the Codex context meter stays blank.
Opt-in, per-provider estimated wire usage (recommended). A provider-level setting, default off, lets the proxy add an estimated usage object to the terminal event only when the upstream omitted usage entirely.
Always estimate when missing. Simplest, but it makes a heuristic look authoritative on every route and changes behavior for users who never asked for it. Not recommended.
Proposed semantics for option 2
Opt-in and scope: a typed boolean on the provider row (for example estimateMissingUsage), default off. It is a non-secret behavioral field, but it lands in PROVIDER_CONFIG_FIELD_POLICY (src/server/auth-cors.ts), so it needs the usual restricted-surface review.
Omitted versus zero: synthesize only when no usage object arrived at all. An explicit usage object, including all-zero or partial ones, is forwarded unchanged. Actual upstream usage is never rewritten or merged with an estimate.
Streaming placement: the estimate is attached once, to the single terminal response.completed / response.incomplete event (and to the non-streaming body), after the stream ends. If upstream usage arrives in any event before the terminal, it wins and no estimate is added. Failed or cancelled turns get no estimate.
What is counted: input = the serialized request actually sent upstream (instructions, input items, tool declarations); output = visible output text plus tool-call arguments plus any returned reasoning summary text. Both go through token-estimate.ts with the route's model family: the existing Claude/GPT/Gemini ratios plus the generic default, with no new tokenizer dependency.
Cache, reasoning and tool details:input_tokens_details.cached_tokens and output_tokens_details.reasoning_tokens are omitted, never estimated. The proxy cannot know cache hits or hidden reasoning, so it does not claim them.
Provenance: the request log records usageStatus: "estimated", and the response carries a marker header (for example x-opencodex-usage: estimated). The estimate exists only for the client's context meter and auto-compact.
Accounting isolation: estimated usage never feeds cost, quota, spend-reservation settlement, pool balances or billing summaries. Those keep using reported usage only (or their existing pre-send reservation). This is the main invariant to test.
Over- versus under-count: the estimator already biases toward over-counting, so auto-compact fires early rather than late. That is the safer failure for the "ran out of room" symptom, at the cost of earlier compaction on these routes.
Risks
Codex treats the numbers as authoritative, so a large estimation error moves its compaction point. The over-count bias limits the dangerous direction but does not remove it.
Combo and fallback turns where one attempt reported usage and a later one did not need per-attempt handling, so an estimate never overwrites a reported value.
WebSocket and Chat/Messages ingress paths have their own terminal framing; each needs the same "only when omitted" rule and its own test.
Suggested first slice once accepted
Responses passthrough only: the opt-in flag, terminal-event attachment, usageStatus: "estimated" in the log, and tests for omitted → estimated, explicit zero → unchanged, reported → unchanged, estimate excluded from cost/quota summaries, and flag off → unchanged.
Feedback on the flag name and on whether a response header is acceptable as the provenance marker would settle the open points.
Owner decision (lidge-jun, 2026-11-16): add a per-provider opt-in that attaches estimated usage when the upstream omits it (default off). Estimates never feed cost or quota accounting, and a response header marks them as estimated.
Activity
github-actions commentedon Oct 20, 2026
Automated translation bookkeeping — detected language: English.
Ingwannu commentedon Oct 20, 2026
Checked against trusted dev
12025981e5a304cdb6b1b54f525ba8554f623977. Keep this as a protocol/accounting design decision. A local token estimate is not upstream-reported usage or a measurement of Codex's full active context; making it look authoritative can corrupt quota/cost accounting as well as the context meter. Existing logging distinguishes estimates from reported evidence. A proposal must name supported tokenization/model families, omitted-versus-zero semantics, streaming terminal placement, cache/reasoning/tool accounting, provenance and opt-in behavior. It must preserve actual usage unchanged and avoid estimating a billing balance from bytes. No synthesized wire usage is enabled by this triage.lidge-jun commentedon Oct 28, 2026
Design proposal: estimated usage when an upstream omits it
Following @Ingwannu's triage, this is a proposal only. Nothing below is implemented, and no synthesized wire usage is enabled by it.
What exists today
src/lib/token-estimate.tsalready produces a script-segmented heuristic estimate, and the Kiro adapter (src/adapters/kiro/usage.ts,stream.ts) uses it to fillresponse.completed.usagebecause Kiro reports nothing.src/usage/log.ts:63already distinguishesreported | unreported | unsupported | estimated, and the request log keys estimated-usage detection off the route adapter.src/adapters/openai-responses/passthrough.tsaround line 646) forwards usage only when the upstream sent one. That is the gap the issue describes.Options
usageobject to the terminal event only when the upstream omitted usage entirely.Proposed semantics for option 2
estimateMissingUsage), default off. It is a non-secret behavioral field, but it lands inPROVIDER_CONFIG_FIELD_POLICY(src/server/auth-cors.ts), so it needs the usual restricted-surface review.response.completed/response.incompleteevent (and to the non-streaming body), after the stream ends. If upstream usage arrives in any event before the terminal, it wins and no estimate is added. Failed or cancelled turns get no estimate.token-estimate.tswith the route's model family: the existing Claude/GPT/Gemini ratios plus the generic default, with no new tokenizer dependency.input_tokens_details.cached_tokensandoutput_tokens_details.reasoning_tokensare omitted, never estimated. The proxy cannot know cache hits or hidden reasoning, so it does not claim them.usageStatus: "estimated", and the response carries a marker header (for examplex-opencodex-usage: estimated). The estimate exists only for the client's context meter and auto-compact.Risks
Suggested first slice once accepted
Responses passthrough only: the opt-in flag, terminal-event attachment,
usageStatus: "estimated"in the log, and tests for omitted → estimated, explicit zero → unchanged, reported → unchanged, estimate excluded from cost/quota summaries, and flag off → unchanged.Feedback on the flag name and on whether a response header is acceptable as the provenance marker would settle the open points.
lidge-jun commentedon Nov 16, 2026
Owner decision (lidge-jun, 2026-11-16): add a per-provider opt-in that attaches estimated usage when the upstream omits it (default off). Estimates never feed cost or quota accounting, and a response header marks them as estimated.
fix(responses): keep estimated usage out of spend settlement and non-…
docs(responses): state the estimated-usage scope and the nested-proxy…
fix(responses): keep estimated JSON usage out of request history and …
fix(responses): estimate a sparse streamed terminal from its streamed…
feat(responses): opt-in estimated usage when a native provider omits …