Skip to content

Synthesize or estimate usage when an upstream provider omits it, so Codex clients keep context accounting #7427

Closed

Description

@RHODIZSECURITY

Area

Proxy and routing

What are you trying to accomplish?

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.
  • I removed secrets and personal data.

Activity

  1. github-actions commented on Oct 20, 2026

    @github-actions
    Contributor

    Automated translation bookkeeping — detected language: English.

  2. added
    enhancement
    proxy
    on Oct 20, 2026
  3. Ingwannu commented on Oct 20, 2026

    @Ingwannu
    Owner

    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.

  4. lidge-jun commented on Oct 28, 2026

    @lidge-jun
    Owner

    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

    1. 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.
    2. 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.
    3. 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.

  5. lidge-jun commented on Nov 16, 2026

    @lidge-jun
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementproxy

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions