Skip to content

The agent that grows with you

fix(codex): stop double-counting cached input in app-server usage

briandevans1c362700b33d
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.py
index 6d606dff2de..600c9348b54 100644
--- a/agent/codex_runtime.py
+++ b/agent/codex_runtime.py
@@ -107,9 +107,14 @@ def _record_codex_app_server_usage(agent, turn, messages=None) -> dict[str, Any]
counts=lambda: billing(billing_mode="subscription_included"))
return {}
from agent.usage_pricing import CanonicalUsage, estimate_usage_cost
+ # ``inputTokens`` is INCLUSIVE of ``cachedInputTokens`` (same contract as the Responses API, see
+ # normalize_usage's codex_responses branch); CanonicalUsage.prompt_tokens re-adds cache_read on top of
+ # input_tokens, so the canonical input bucket must be the UNCACHED remainder or cached tokens count twice.
+ cache_read_tokens = _coerce_usage_int(usage.get("cachedInputTokens"))
canonical_usage = CanonicalUsage(
- input_tokens=_coerce_usage_int(usage.get("inputTokens")), output_tokens=_coerce_usage_int(usage.get("outputTokens")),
- cache_read_tokens=_coerce_usage_int(usage.get("cachedInputTokens")), cache_write_tokens=0,
+ input_tokens=max(0, _coerce_usage_int(usage.get("inputTokens")) - cache_read_tokens),
+ output_tokens=_coerce_usage_int(usage.get("outputTokens")),
+ cache_read_tokens=cache_read_tokens, cache_write_tokens=0,
reasoning_tokens=_coerce_usage_int(usage.get("reasoningOutputTokens")), raw_usage=usage,
)
prompt_tokens = canonical_usage.prompt_tokens
diff --git a/tests/agent/test_codex_app_server_integration.py b/tests/agent/test_codex_app_server_integration.py
index 0500f8115ec..2505c3dbe16 100644
--- a/tests/agent/test_codex_app_server_integration.py
+++ b/tests/agent/test_codex_app_server_integration.py
@@ -111,27 +111,29 @@ class TestRunConversationCodexPath:
with patch.object(agent, "_spawn_background_review", return_value=None):
result = agent.run_conversation("hello")
+ # inputTokens (80) is INCLUSIVE of cachedInputTokens (20): uncached=60, prompt=60+20=80 (never 100),
+ # totalTokens stays the provider passthrough (130). #105412 / #63654
assert result["api_calls"] == 1
- assert result["prompt_tokens"] == 100
+ assert result["prompt_tokens"] == 80
assert result["completion_tokens"] == 25
assert result["total_tokens"] == 130
- assert result["input_tokens"] == 80
+ assert result["input_tokens"] == 60
assert result["output_tokens"] == 25
assert result["cache_read_tokens"] == 20
assert result["cache_write_tokens"] == 0
assert result["reasoning_tokens"] == 5
- assert result["last_prompt_tokens"] == 100
+ assert result["last_prompt_tokens"] == 80
assert agent.session_api_calls == 1
- assert agent.session_prompt_tokens == 100
+ assert agent.session_prompt_tokens == 80
assert agent.session_completion_tokens == 25
assert agent.session_total_tokens == 130
- assert agent.session_input_tokens == 80
+ assert agent.session_input_tokens == 60
assert agent.session_output_tokens == 25
assert agent.session_cache_read_tokens == 20
assert agent.session_cache_write_tokens == 0
assert agent.session_reasoning_tokens == 5
- assert agent.context_compressor.last_prompt_tokens == 100
+ assert agent.context_compressor.last_prompt_tokens == 80
assert agent.context_compressor.last_completion_tokens == 25
assert agent.context_compressor.last_total_tokens == 130
assert agent.context_compressor.context_length == 200000