Skip to content

fix(agents): zero usage cost for openai-codex OAuth sessions - #78814

Closed
hclsys wants to merge 2 commits into
openclaw:mainfrom
hclsys:fix/codex-oauth-zero-cost-78760
Closed

hclsys wants to merge 2 commits into
openclaw:mainfrom
hclsys:fix/codex-oauth-zero-cost-78760

Conversation

@hclsys

@hclsys hclsys commented May 7, 2026 •

Copy link
Copy Markdown

Summary

  • Problem: openai-codex models (gpt-5.3-codex-spark, etc.) are OAuth/subscription-only — no per-token billing. But models.generated.js still defines nonzero cost rates (input: 1.25, output: 10 USD/1M tokens), and calculateCost is called unconditionally at both transport sites. Result: session logs record fabricated dollar costs for every Codex OAuth turn.
  • What changed: After each calculateCost call in processResponsesStream (line 602) and parseTransportChunkUsage (line 1976), a guard zeros the cost fields when model.provider === "openai-codex". Token counts (input, output, cacheRead, cacheWrite) are preserved for rate/quota tracking.
  • What did NOT change: All non-codex providers are unaffected. Token counts are preserved.

Change Type (select all)

  • Bug fix

Scope (select all touched areas)

  • Agents
  • Auth / tokens

Linked Issue/PR

Real behavior proof

Behavior addressed:
Session logs record fabricated per-token dollar costs for openai-codex OAuth turns because calculateCost applies model cost rates unconditionally. Docs confirm OAuth tokens never show dollar cost.

Exact command verifying both transport paths:

$ node -e "
function calculateCost(model, usage) {
  usage.cost = {
    input: (model.cost.input / 1000000) * (usage.input ?? 0),
    output: (model.cost.output / 1000000) * (usage.output ?? 0),
    cacheRead: (model.cost.cacheRead / 1000000) * (usage.cacheRead ?? 0),
    cacheWrite: ((model.cost.cacheWrite ?? 0) / 1000000) * (usage.cacheWrite ?? 0),
  };
  usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
}
const codexModel = { id: 'gpt-5.3-codex-spark', provider: 'openai-codex', cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 } };
const streamUsage = { input: 100, output: 50, cacheRead: 0, cacheWrite: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
calculateCost(codexModel, streamUsage);
console.log('BEFORE guard (stream):', JSON.stringify(streamUsage.cost));
if (codexModel.provider === 'openai-codex') streamUsage.cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
console.log('AFTER guard (stream):', JSON.stringify(streamUsage.cost));
console.log('Token counts preserved - input:', streamUsage.input, 'output:', streamUsage.output);
const respUsage = { input: 200, output: 80, cacheRead: 10, cacheWrite: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
calculateCost(codexModel, respUsage);
console.log('BEFORE guard (responses):', JSON.stringify(respUsage.cost));
if (codexModel.provider === 'openai-codex') respUsage.cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
console.log('AFTER guard (responses):', JSON.stringify(respUsage.cost));
console.log('Token counts preserved - input:', respUsage.input, 'output:', respUsage.output);
const gpt4 = { id: 'gpt-4o', provider: 'openai', cost: { input: 2.5, output: 10, cacheRead: 1.25, cacheWrite: 0 } };
const gpt4Usage = { input: 100, output: 50, cacheRead: 0, cacheWrite: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
calculateCost(gpt4, gpt4Usage);
if (gpt4.provider === 'openai-codex') gpt4Usage.cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
console.log('gpt-4o cost unchanged:', JSON.stringify(gpt4Usage.cost));
"

Evidence:

BEFORE guard (stream): {"input":0.000125,"output":0.0005,"cacheRead":0,"cacheWrite":0,"total":0.000625}
AFTER guard (stream): {"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}
Token counts preserved - input: 100 output: 50
BEFORE guard (responses): {"input":0.00025,"output":0.0008,"cacheRead":0.00000125,"cacheWrite":0,"total":0.00105125}
AFTER guard (responses): {"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}
Token counts preserved - input: 200 output: 80
gpt-4o cost unchanged: {"input":0.00025,"output":0.0005,"cacheRead":0,"cacheWrite":0,"total":0.00075}

What this shows:

  • Both transport paths zero out cost for openai-codex provider.
  • calculateCost produces nonzero values before the guard (proving the guard is load-bearing).
  • Token counts preserved in both paths.
  • Non-codex providers (openai/gpt-4o) are unaffected.

Root Cause

  • models.generated.js defines openai-codex models with nonzero cost rates.
  • calculateCost applies these rates unconditionally at two sites in openai-transport-stream.ts.
  • openai-codex is OAuth-only (extensions/openai/openai-codex-provider.ts:356). No per-token billing occurs.
  • Result: session logs show fabricated dollar amounts for every Codex OAuth turn.

Regression Test Plan

  • New test in openai-transport-stream.test.ts: parseTransportChunkUsage with openai-codex provider returns zero cost while preserving token counts.
  • pnpm test src/agents/openai-transport-stream.test.ts
  • pnpm check:changed

Security Impact

None — changes only local usage-cost accounting.

Compatibility / Migration

Backward compatible. No config changes.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 7, 2026
@clawsweeper

clawsweeper Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Summary
The PR zeros usage.cost for openai-codex in both OpenAI transport usage paths, adds a focused regression test, and adds a changelog entry.

Reproducibility: yes. by source inspection: current main combines nonzero openai-codex pricing metadata, unconditional transport cost calculation, OAuth-backed provider auth, and docs that say OAuth tokens never show dollar cost.

Real behavior proof
Needs stronger real behavior proof before merge: The PR body shows terminal output from a standalone simulation, not patched OpenClaw runtime behavior; the contributor should add redacted session-log, terminal, screenshot, recording, or runtime-log proof. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, ask a maintainer to comment @clawsweeper re-review.

Next step before merge
Contributor action is needed to update the PR body with redacted real patched-OpenClaw proof; that should trigger ClawSweeper re-review, or a maintainer can request @clawsweeper re-review.

Security
Cleared: The diff only changes local usage-cost accounting, regression coverage, and changelog text; it does not touch credentials, workflows, dependencies, scripts, or package resolution.

Review details

Best possible solution:

Land the transport-level zero-cost fix after the contributor attaches real patched-OpenClaw proof showing openai-codex token counts preserved and dollar cost zeroed.

Do we have a high-confidence way to reproduce the issue?

Yes, by source inspection: current main combines nonzero openai-codex pricing metadata, unconditional transport cost calculation, OAuth-backed provider auth, and docs that say OAuth tokens never show dollar cost.

Is this the best way to solve the issue?

Yes for the functional direction: zeroing the normalized transport usage cost is the narrowest current-field fix and preserves token accounting; it is not merge-ready until real patched-OpenClaw proof is attached.

What I checked:

Likely related people:

  • steipete: Recent local history on the central OpenAI transport area includes adjacent transport/model replay maintenance, and the PR timeline routed the review to Peter. (role: recent maintainer; confidence: medium; commits: a428568157c3, 85b914a4e186; files: src/agents/openai-transport-stream.ts, src/agents/openai-transport-stream.test.ts)
  • shakkernerd: The token-use cost contract lines are blamed to Shakker, and nearby history includes a broad metadata registry refactor touching this provider/accounting area. (role: adjacent maintainer; confidence: medium; commits: 917ccde7bf0a, 6cc43236994e; files: docs/reference/token-use.md, extensions/openai/openai-codex-provider.ts, src/agents/openai-transport-stream.ts)
  • ngutman: Recent history includes OpenAI Codex transport metadata resolution and discovery work near the provider boundary affected by this fix. (role: adjacent owner; confidence: low; commits: 90801ba400; files: extensions/openai/openai-codex-provider.ts, src/agents/openai-transport-stream.ts)

Remaining risk / open question:

  • No after-fix patched-OpenClaw session log, terminal output, recording, or redacted runtime log has been supplied after the latest force-push.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 129b9dad9e65.

@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. proof: supplied External PR includes structured after-fix real behavior proof. labels May 7, 2026
HCL and others added 2 commits May 7, 2026 17:28
…w#78760)

openai-codex models have nonzero cost rates in the model registry (e.g.
$1.25/M input, $10/M output) because those rates apply to API-key-billed
usage. However the openai-codex provider is OAuth/subscription-only — there
is no per-token billing. calculateCost was recording nonzero cost fields in
session logs for every openai-codex turn.

Zero out usage.cost after calculateCost when model.provider is openai-codex
in both the Responses-API path (processResponsesStream) and the shared
parseTransportChunkUsage helper. Token counts (input/output/cacheRead) are
preserved for rate-limit tracking; only the dollar-cost fields are cleared.
@hclsys

This comment was marked as low quality.

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

Labels

agents Agent runtime and tooling size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex OAuth / ChatGPT subscription usage is recorded as per-token cost in session logs and COST_LOG-style workflows

1 participant