Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(openai): read Responses cache_write_tokens instead of inferring writes (#4237)

Tejas Chopra46306024e74b
diff --git a/headroom/proxy/gateway_turn.py b/headroom/proxy/gateway_turn.py
index 46a7b68a0..51f169622 100644
--- a/headroom/proxy/gateway_turn.py
+++ b/headroom/proxy/gateway_turn.py
@@ -342,7 +342,9 @@ def normalize_usage(usage: dict[str, Any]) -> NormalizedUsage:
# ``input_tokens`` too, so the key name cannot be the discriminator. Keying
# off Anthropic's cache fields can misread only a relay that mixes the two
# vocabularies, and then it errs towards not inflating the prompt.
- input_includes_cache = not (
+ # Responses' ``input_tokens_details`` marks an inclusive total even when a
+ # relay also flattened a top-level read beside it.
+ input_includes_cache = "input_tokens_details" in usage or not (
"cache_read_input_tokens" in usage or "cache_creation_input_tokens" in usage
)
output_tokens = _usage_int(usage, "output_tokens")
@@ -351,10 +353,16 @@ def normalize_usage(usage: dict[str, Any]) -> NormalizedUsage:
cache_read = _usage_int(usage, "cache_read_input_tokens")
cache_write = _usage_int(usage, "cache_creation_input_tokens")
+ if cache_write is None:
+ # Responses reports billed writes as input_tokens_details.cache_write_tokens;
+ # chat reports none. Read whether or not a top-level read was relayed.
+ details = usage.get("input_tokens_details")
+ if isinstance(details, dict):
+ cache_write = _usage_int(details, "cache_write_tokens")
if cache_read is None:
- # OpenAI reports reads only (no write counter exists), nested under
- # prompt_tokens_details (chat), input_tokens_details (Responses) or —
- # in Kong's flattened log shape — at the top level.
+ # OpenAI nests reads under prompt_tokens_details (chat),
+ # input_tokens_details (Responses) or — in Kong's flattened log shape —
+ # puts them at the top level.
for details_key in ("prompt_tokens_details", "input_tokens_details"):
details = usage.get(details_key)
if isinstance(details, dict):
diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.py
index 145cbcea8..4b5d299bc 100644
--- a/headroom/proxy/handlers/openai.py
+++ b/headroom/proxy/handlers/openai.py
@@ -351,13 +351,22 @@ class TurnHookUsage:
the safe direction for a bill.
"""
- __slots__ = ("_seen", "input_tokens", "output_tokens", "cache_read_tokens", "extra_calls")
+ __slots__ = (
+ "_seen",
+ "input_tokens",
+ "output_tokens",
+ "cache_read_tokens",
+ "cache_write_tokens",
+ "extra_calls",
+ )
def __init__(self) -> None:
- self._seen: list[tuple[int, Any, int, int, int]] = []
+ self._seen: list[tuple[int, Any, int, int, int, int]] = []
self.input_tokens = 0
self.output_tokens = 0
self.cache_read_tokens = 0
+ #: Reported (Responses ``cache_write_tokens``) writes only; 0 otherwise.
+ self.cache_write_tokens = 0
self.extra_calls = 0
def record(
@@ -381,17 +390,19 @@ class TurnHookUsage:
usage_int(usage.get(input_key)),
usage_int(usage.get(output_key)),
cached,
+ _reported_cache_write(details) or 0,
)
else:
- entry = (id(payload), payload, 0, 0, 0)
+ entry = (id(payload), payload, 0, 0, 0, 0)
self._seen.append(entry)
def settle(self, final: Any) -> None:
"""Total everything except ``final``, which the usage block will read."""
self.input_tokens = self.output_tokens = self.cache_read_tokens = 0
+ self.cache_write_tokens = 0
self.extra_calls = 0
dropped = False
- for _ident, payload, tin, tout, cached in self._seen:
+ for _ident, payload, tin, tout, cached, written in self._seen:
if not dropped and payload is final:
dropped = True
continue
@@ -399,6 +410,7 @@ class TurnHookUsage:
self.input_tokens += tin
self.output_tokens += tout
self.cache_read_tokens += cached
+ self.cache_write_tokens += written
def _sanitize_forwarded_response_headers(
@@ -614,7 +626,13 @@ def _passthrough_usage_from_json(payload: Any, provider: str | None = None) -> d
"input_tokens": billed_input_from_usage(usage, provider) or usage_int(input_tokens),
"output_tokens": usage_int(output_tokens),
"cache_read_input_tokens": usage_int(usage.get("cache_read_input_tokens", cache_read)),
- "cache_creation_input_tokens": usage_int(usage.get("cache_creation_input_tokens")),
+ # Responses spells a billed write ``input_tokens_details.cache_write_tokens``.
+ "cache_creation_input_tokens": usage_int(
+ usage.get(
+ "cache_creation_input_tokens",
+ details.get("cache_write_tokens") if isinstance(details, dict) else None,
+ )
+ ),
}
return {}
@@ -1650,6 +1668,48 @@ def _infer_openai_cache_write_tokens(input_tokens: int, cache_read_tokens: int)
return max(input_tokens - cache_read_tokens, 0)
+def _reported_cache_write(details: Any) -> int | None:
+ """``input_tokens_details.cache_write_tokens``, or ``None`` when not reported."""
+ if not isinstance(details, dict) or details.get("cache_write_tokens") is None:
+ return None
+ return usage_int(details.get("cache_write_tokens"))
+
+
+def _hook_reported_write(final_write: int | None, hook_usage: Any) -> int | None:
+ """The final response's reported write plus any turn-hook re-drives' writes.
+
+ A final response without the field still carries the re-drives' reported
+ writes: treating its own write as 0 prices the same as inferring it, since
+ an inferred write is dropped from pricing anyway.
+ """
+ hook_writes = hook_usage.cache_write_tokens if hook_usage.extra_calls else 0
+ if final_write is None:
+ return hook_writes or None
+ return final_write + hook_writes
+
+
+def _openai_responses_cache_split(
+ input_tokens: int, cached: int, reported_write: int | None
+) -> tuple[int, int, int, bool]:
+ """Split a Responses ``input_tokens`` total into ``(read, write, uncached, inferred)``.
+
+ Responses usage now reports cache writes itself as
+ ``input_tokens_details.cache_write_tokens``, billed at the write rate
+ (above list input on current models). When present it is the truth and the
+ three buckets are disjoint. Older responses omit it; the write is then
+ inferred from the uncached slice and flagged, so pricing drops it rather
+ than billing the same tokens twice.
+ """
+ if reported_write is not None:
+ return cached, reported_write, max(input_tokens - cached - reported_write, 0), False
+ return (
+ cached,
+ _infer_openai_cache_write_tokens(input_tokens, cached),
+ max(input_tokens - cached, 0),
+ True,
+ )
+
+
def _deferrable_savings_delta(input_delta: int, saved_delta: int) -> int:
"""Gate a WS Responses turn's compression-savings delta on input accounting.
@@ -1670,32 +1730,36 @@ def _deferrable_savings_delta(input_delta: int, saved_delta: int) -> int:
return saved_delta
-def _extract_responses_usage(event: dict[str, Any]) -> tuple[int, int, int, int, int]:
+def _extract_responses_usage(event: dict[str, Any]) -> tuple[int, int, int, int, int, bool]:
"""Return input/output/cache usage from a Responses event.
Codex WebSocket streams include usage on ``response.completed`` events.
The shape mirrors HTTP Responses usage:
``response.usage.input_tokens`` plus
- ``response.usage.input_tokens_details.cached_tokens``.
+ ``response.usage.input_tokens_details.cached_tokens`` / ``cache_write_tokens``.
+ The last element says whether the write was inferred (see
+ :func:`_openai_responses_cache_split`).
"""
if event.get("type") != "response.completed":
- return 0, 0, 0, 0, 0
+ return 0, 0, 0, 0, 0, True
response = event.get("response")
if not isinstance(response, dict):
response = {}
usage = response.get("usage") or event.get("usage")
if not isinstance(usage, dict):
- return 0, 0, 0, 0, 0
+ return 0, 0, 0, 0, 0, True
input_tokens = usage_int(usage.get("input_tokens"))
output_tokens = usage_int(usage.get("output_tokens"))
details = usage.get("input_tokens_details")
- cached_tokens = usage_int(details.get("cached_tokens")) if isinstance(details, dict) else 0
- cache_write_tokens = _infer_openai_cache_write_tokens(input_tokens, cached_tokens)
- uncached_tokens = max(input_tokens - cached_tokens, 0)
- return input_tokens, output_tokens, cached_tokens, cache_write_tokens, uncached_tokens
+ cached, write, uncached, inferred = _openai_responses_cache_split(
+ input_tokens,
+ usage_int(details.get("cached_tokens")) if isinstance(details, dict) else 0,
+ _reported_cache_write(details),
+ )
+ return input_tokens, output_tokens, cached, write, uncached, inferred
def _prefers_http1_passthrough(base_url: str) -> bool:
@@ -6538,6 +6602,7 @@ class OpenAIHandlerMixin:
total_input_tokens = original_tokens # fallback
output_tokens = 0
cache_read_tokens = 0
+ details: Any = None
resp_json = None
try:
resp_json = response.json()
@@ -6757,11 +6822,17 @@ class OpenAIHandlerMixin:
# Cost is recorded once by the outcome funnel below; here we only
# compute the cache-write / uncached split the funnel needs.
# (Recording here too double-counted spend + budget on this path.)
- cache_write_tokens = _infer_openai_cache_write_tokens(
+ # Reads include the hook's re-drives added above.
+ (
+ cache_read_tokens,
+ cache_write_tokens,
+ uncached_input_tokens,
+ cache_inferred,
+ ) = _openai_responses_cache_split(
total_input_tokens,
cache_read_tokens,
+ _hook_reported_write(_reported_cache_write(details), _resp_hook_usage),
)
- uncached_input_tokens = max(0, total_input_tokens - cache_read_tokens)
# Was: optimized := provider count, then original := max(original,
# optimized + saved) to stop `attempted` exceeding `original`.
@@ -6803,7 +6874,7 @@ class OpenAIHandlerMixin:
cache_read_tokens=cache_read_tokens,
cache_write_tokens=cache_write_tokens,
uncached_input_tokens=uncached_input_tokens,
- cache_inferred=True,
+ cache_inferred=cache_inferred,
total_latency_ms=total_latency,
overhead_ms=optimization_latency,
transforms_applied=tuple(transforms_applied),
@@ -7564,6 +7635,11 @@ class OpenAIHandlerMixin:
ws_cache_read_tokens_total = 0
ws_cache_write_tokens_total = 0
ws_uncached_input_tokens_total = 0
+ # Whether the latest usage frame's write was inferred rather than
+ # reported (see _openai_responses_cache_split). One flag for the
+ # summed deltas: a session that mixed reported and unreported
+ # frames would misprice one kind, which Codex does not do.
+ ws_cache_inferred = True
ws_recorded_input_tokens_total = 0
ws_recorded_output_tokens_total = 0
ws_recorded_cache_read_tokens_total = 0
@@ -8890,7 +8966,7 @@ class OpenAIHandlerMixin:
nonlocal ws_memory_response_pending
nonlocal ws_input_tokens_total, ws_output_tokens_total
nonlocal ws_cache_read_tokens_total, ws_cache_write_tokens_total
- nonlocal ws_uncached_input_tokens_total
+ nonlocal ws_uncached_input_tokens_total, ws_cache_inferred
nonlocal ws_recorded_input_tokens_total
nonlocal ws_recorded_output_tokens_total
nonlocal ws_recorded_cache_read_tokens_total
@@ -9020,6 +9096,7 @@ class OpenAIHandlerMixin:
cache_read_tokens=max(0, cache_read_delta),
cache_write_tokens=max(0, cache_write_delta),
uncached_input_tokens=max(0, uncached_delta),
+ cache_inferred=ws_cache_inferred,
total_latency_ms=latency_ms,
overhead_ms=overhead_delta_ms,
ttfb_ms=ttfb_for_record_ms,
@@ -9164,8 +9241,10 @@ class OpenAIHandlerMixin:
usage_cache_read_tokens,
usage_cache_write_tokens,
usage_uncached_tokens,
+ usage_cache_inferred,
) = _extract_responses_usage(event)
if usage_input_tokens or usage_output_tokens:
+ ws_cache_inferred = usage_cache_inferred
ws_input_tokens_total += usage_input_tokens
ws_output_tokens_total += usage_output_tokens
ws_cache_read_tokens_total += usage_cache_read_tokens
@@ -9559,6 +9638,7 @@ class OpenAIHandlerMixin:
fb_cache_read_tokens,
fb_cache_write_tokens,
fb_uncached_tokens,
+ ws_cache_inferred,
) = await self._ws_http_fallback(
websocket, body, first_msg_raw, upstream_headers, request_id
)
@@ -9690,6 +9770,7 @@ class OpenAIHandlerMixin:
cache_read_tokens=residual_cache_read_tokens,
cache_write_tokens=residual_cache_write_tokens,
uncached_input_tokens=residual_uncached_input_tokens,
+ cache_inferred=ws_cache_inferred,
total_latency_ms=ws_session_duration_ms,
overhead_ms=final_overhead_delta_ms,
ttfb_ms=final_ttfb_ms,
@@ -9836,7 +9917,7 @@ class OpenAIHandlerMixin:
first_msg_raw: str,
upstream_headers: dict[str, str],
request_id: str,
- ) -> tuple[int, int, int, int, int]:
+ ) -> tuple[int, int, int, int, int, bool]:
"""Fall back to HTTP POST streaming when upstream WS fails.
Converts the WS ``response.create`` message to an HTTP POST to
@@ -9850,7 +9931,9 @@ class OpenAIHandlerMixin:
authoritative wire-token count; otherwise a fallback recorded
``input_tokens=0`` and savings percentages blew past 100 (#2957).
"""
- fallback_usage = [0, 0, 0, 0, 0]
+ # Five summed counters, then whether the latest frame's write was
+ # inferred (see _openai_responses_cache_split).
+ fallback_usage: list[int] = [0, 0, 0, 0, 0, True]
def _accumulate_usage(data_str: str) -> None:
try:
@@ -9858,8 +9941,10 @@ class OpenAIHandlerMixin:
except (json.JSONDecodeError, TypeError):
return
if isinstance(event, dict) and event.get("type") == "response.completed":
- for i, value in enumerate(_extract_responses_usage(event)):
+ usage = _extract_responses_usage(event)
+ for i, value in enumerate(usage[:5]):
fallback_usage[i] += value
+ fallback_usage[5] = usage[5]
# Route to correct endpoint based on auth mode
is_chatgpt_fallback = has_chatgpt_account_header(upstream_headers)
diff --git a/headroom/proxy/handlers/streaming.py b/headroom/proxy/handlers/streaming.py
index d776bc7fb..c8de2c75f 100644
--- a/headroom/proxy/handlers/streaming.py
+++ b/headroom/proxy/handlers/streaming.py
@@ -421,6 +421,13 @@ class StreamingMixin:
usage_found["cache_read_input_tokens"] = usage_int(
details.get("cached_tokens")
)
+ # Responses reports billed cache writes itself; the
+ # Anthropic-shape key routes them down the real-write
+ # branch instead of inferring them from uncached.
+ if details.get("cache_write_tokens") is not None:
+ usage_found["cache_creation_input_tokens"] = usage_int(
+ details.get("cache_write_tokens")
+ )
elif provider == "gemini":
usage_meta = _sse_dict(data.get("usageMetadata"))
@@ -2425,12 +2432,15 @@ class StreamingMixin:
# Bedrock/Anthropic shape when present. Fall back to
# inferring write count from total - read for OpenAI
# shape (which has no separate write counter).
- cache_creation_input_tokens = stream_state.get("cache_creation_input_tokens") or 0
+ # None means "not reported"; an explicit 0 is a reported zero
+ # and must not fall through to the inferred branch.
+ reported_write = stream_state.get("cache_creation_input_tokens")
+ cache_creation_input_tokens = reported_write or 0
if upstream_input is None:
cache_write_tokens = 0
uncached_input_tokens = 0
cache_inferred = False
- elif cache_creation_input_tokens > 0:
+ elif reported_write is not None:
cache_write_tokens = cache_creation_input_tokens
uncached_input_tokens = max(
upstream_input - cache_read_tokens - cache_write_tokens, 0
diff --git a/tests/test_gateway_turn_unit.py b/tests/test_gateway_turn_unit.py
index 3ccb28fb6..6bb740047 100644
--- a/tests/test_gateway_turn_unit.py
+++ b/tests/test_gateway_turn_unit.py
@@ -448,6 +448,32 @@ def test_normalize_openai_responses_shape() -> None:
assert u == NormalizedUsage(10, 2, 8, None, input_includes_cache=True)
+def test_normalize_openai_responses_reported_cache_writes() -> None:
+ u = normalize_usage(
+ {
+ "input_tokens": 10,
+ "output_tokens": 2,
+ "input_tokens_details": {"cached_tokens": 6, "cache_write_tokens": 3},
+ }
+ )
+ assert u == NormalizedUsage(10, 2, 6, 3, input_includes_cache=True)
+
+
+def test_normalize_reads_nested_writes_beside_a_top_level_read() -> None:
+ u = normalize_usage(
+ {
+ "input_tokens": 100,
+ "cache_read_input_tokens": 70,
+ "input_tokens_details": {"cached_tokens": 70, "cache_write_tokens": 20},
+ }
+ )
+ assert (u.cache_read, u.cache_write) == (70, 20)
+ # The Responses total already contains both: billing must not add them.
+ done = gt.complete_outcome(_outcome(), u, status=200, latency_ms=1)
+ assert done.provider_input_tokens == 100
+ assert done.uncached_input_tokens == 10
+
+
def test_normalize_kong_flat_shape() -> None:
u = normalize_usage({"prompt_tokens": 50, "completion_tokens": 5, "cached_tokens": 40})
assert u == NormalizedUsage(50, 5, 40, None, input_includes_cache=True)
diff --git a/tests/test_openai_codex_ws_lifecycle.py b/tests/test_openai_codex_ws_lifecycle.py
index 2b8e9d9d6..adfdd1463 100644
--- a/tests/test_openai_codex_ws_lifecycle.py
+++ b/tests/test_openai_codex_ws_lifecycle.py
@@ -899,6 +899,43 @@ async def test_ws_session_metrics_include_response_completed_usage():
assert recorded["cache_read_tokens"] == 75
assert recorded["cache_write_tokens"] == 25
assert recorded["uncached_input_tokens"] == 25
+ # The write above is the uncached slice, not a billed write: flagged, or
+ # pricing bills those 25 tokens twice.
+ assert recorded["cache_inferred"] is True
+
+
+async def test_ws_session_metrics_use_reported_cache_writes():
+ """Responses now reports billed cache writes; they are the truth, not the
+ uncached remainder, and pricing must treat them as real writes."""
+
+ upstream_events = [
+ json.dumps({"type": "response.created", "response": {"id": "r_1"}}),
+ json.dumps(
+ {
+ "type": "response.completed",
+ "response": {
+ "id": "r_1",
+ "usage": {
+ "input_tokens": 100,
+ "input_tokens_details": {"cached_tokens": 75, "cache_write_tokens": 20},
+ "output_tokens": 12,
+ },
+ },
+ }
+ ),
+ ]
+ fake_ws_mod = _make_fake_websockets_module(_FakeUpstream(upstream_events))
+ handler = _DummyOpenAIHandler()
+
+ with patch.dict(sys.modules, {"websockets": fake_ws_mod}):
+ await handler.handle_openai_responses_ws(_FakeWebSocket(frames=[_first_frame()]))
+
+ recorded = handler.metrics.recorded_requests[-1]
+ assert recorded["cache_read_tokens"] == 75
+ assert recorded["cache_write_tokens"] == 20
+ assert recorded["uncached_input_tokens"] == 5
+ assert recorded["cache_inferred"] is False
@pytest.mark.asyncio
diff --git a/tests/test_proxy_budget.py b/tests/test_proxy_budget.py
index 38ac92ce0..f91858ab9 100644
--- a/tests/test_proxy_budget.py
+++ b/tests/test_proxy_budget.py
@@ -718,7 +718,7 @@ def test_websocket_http_fallback_blocked_when_budget_exhausted() -> None:
async def _run() -> None:
proxy = _ws_budget_proxy(retry_max_attempts=1)
- proxy._ws_http_fallback = AsyncMock(return_value=(0, 0, 0, 0, 0))
+ proxy._ws_http_fallback = AsyncMock(return_value=(0, 0, 0, 0, 0, True))
async def _fake_connect(*args, **kwargs):
_spend_past_budget(proxy)
diff --git a/tests/test_route_advice.py b/tests/test_route_advice.py
index faf607273..b61566f93 100644
--- a/tests/test_route_advice.py
+++ b/tests/test_route_advice.py
@@ -7,6 +7,7 @@ with nothing published, every path is exactly what it was before.
from __future__ import annotations
import asyncio
+import json
from types import SimpleNamespace
import pytest
@@ -626,3 +627,38 @@ def test_the_resolver_follows_a_reassigned_default():
assert first.default is None
h.anthropic_backend = DEFAULT
assert BR(h.anthropic_backend).default is DEFAULT
+
+
[email protected](("written", "inferred"), [(0, False), (20, False), (None, True)])
+def test_backend_openai_stream_reads_reported_cache_writes(written, inferred):
+ """A reported write, zero included, is the provider's word; only an absent
+ field falls back to inferring writes from the uncached slice."""
+ from types import MethodType
+
+ from headroom.proxy.handlers.streaming import StreamingMixin
+
+ details = {"cached_tokens": 75}
+ if written is not None:
+ details["cache_write_tokens"] = written
+ usage = {"input_tokens": 100, "output_tokens": 5, "input_tokens_details": details}
+
+ class Backend(_Backend):
+ async def stream_openai_message(self, body, headers):
+ yield f"data: {json.dumps({'type': 'response.completed', 'response': {'usage': usage}})}\n\n"
+ yield "data: [DONE]\n\n"
+
+ outcomes = []
+
+ async def record(outcome):
+ outcomes.append(outcome)
+
+ handler = _handler(Backend("openai"))
+ handler._record_request_outcome = record
+ handler._parse_sse_usage_from_buffer = MethodType(
+ StreamingMixin._parse_sse_usage_from_buffer, handler
+ )
+ asyncio.run(_drive_openai_with_outcome(handler, handler.anthropic_backend))
+
+ o = outcomes[0]
+ assert o.cache_inferred is inferred
+ assert o.cache_write_tokens == (25 if written is None else written)
diff --git a/tests/test_turn_hook_usage.py b/tests/test_turn_hook_usage.py
index cbf5a185e..3ec905909 100644
--- a/tests/test_turn_hook_usage.py
+++ b/tests/test_turn_hook_usage.py
@@ -320,6 +320,68 @@ def test_responses_bills_the_original_plus_the_redrive(monkeypatch, _no_hooks) -
assert o.output_tokens == 90, f"want A+B=90, got {o.output_tokens}"
+def test_responses_reported_cache_writes_include_the_redrive(monkeypatch, _no_hooks) -> None:
+ """Responses reports billed writes itself; both calls' writes are writes,
+ not an uncached remainder priced at list."""
+ register_turn_hook(_RedriveOnce())
+ app, outcomes = _app_and_outcomes(monkeypatch)
+
+ def body(rid: str, total: int, cached: int, written: int) -> dict:
+ return {
+ "id": rid,
+ "output": [{"type": "message", "role": "assistant", "content": []}],
+ "usage": {
+ "input_tokens": total,
+ "output_tokens": 10,
+ "input_tokens_details": {"cached_tokens": cached, "cache_write_tokens": written},
+ },
+ }
+
+ sent = iter([body("a", 400, 300, 80), body("b", 500, 400, 60)])
+ respx.post("https://api.openai.com/v1/responses").mock(
+ side_effect=lambda request: httpx.Response(200, json=next(sent))
+ )
+
+ with TestClient(app) as client:
+ r = client.post(
+ "/v1/responses",
+ json={
+ "model": "gpt-4o",
+ "input": [{"type": "message", "role": "user", "content": []}],
+ "stream": False,
+ },
+ headers={"authorization": "Bearer sk-test"},
+ )
+ assert r.status_code == 200
+ o = outcomes[-1]
+ assert (o.provider_input_tokens, o.cache_read_tokens, o.cache_write_tokens) == (900, 700, 140)
+ assert o.uncached_input_tokens == 60
+ assert o.cache_inferred is False
+
+
+def test_redrive_writes_survive_a_final_response_without_the_field() -> None:
+ """Treating the final call's own write as 0 prices the same as inferring
+ it (inferred writes are dropped), so the re-drives' real writes stay."""
+ from headroom.proxy.handlers.openai import _hook_reported_write
+
+ u = TurnHookUsage()
+ a = {
+ "usage": {
+ "input_tokens": 100,
+ "output_tokens": 1,
+ "input_tokens_details": {"cached_tokens": 60, "cache_write_tokens": 30},
+ }
+ }
+ b = {"usage": {"input_tokens": 100, "output_tokens": 1}}
+ u.record(a, **RESPONSES_USAGE_KEYS)
+ u.record(b, **RESPONSES_USAGE_KEYS)
+ u.settle(b)
+ assert _hook_reported_write(None, u) == 30
+ assert _hook_reported_write(5, u) == 35
+ assert _hook_reported_write(None, TurnHookUsage()) is None
+
+
@respx.mock
def test_no_hook_registered_bills_exactly_the_one_call(monkeypatch, _no_hooks) -> None:
"""The regression guard in the other direction: with no hook, accounting must
diff --git a/tests/test_ws_http_fallback.py b/tests/test_ws_http_fallback.py
index 8d3317715..8d61972d3 100644
--- a/tests/test_ws_http_fallback.py
+++ b/tests/test_ws_http_fallback.py
@@ -410,7 +410,8 @@ class TestWsHttpFallback:
body = {"model": "gpt-5.4", "input": "big context"}
usage = asyncio.run(handler._ws_http_fallback(ws, body, json.dumps(body), {}, "req_usage"))
- input_tokens, output_tokens, cache_read, _cache_write, uncached = usage
+ input_tokens, output_tokens, cache_read, _cache_write, uncached, inferred = usage
+ assert inferred is True # no cache_write_tokens on this frame
assert input_tokens == 31055
assert output_tokens == 246
assert cache_read == 20000
@@ -427,7 +428,30 @@ class TestWsHttpFallback:
ws, {"model": "gpt-5.4", "input": "hi"}, json.dumps({"input": "hi"}), {}, "req_none"
)
)
- assert usage == (0, 0, 0, 0, 0)
+ assert usage == (0, 0, 0, 0, 0, True)
+
+ def test_fallback_returns_reported_cache_writes_as_real(self):
+ """A reported write must not come back flagged inferred: pricing drops
+ inferred writes, so the written tokens would be billed at nothing."""
+ handler = _make_handler()
+ completed = {
+ "type": "response.completed",
+ "response": {
+ "usage": {
+ "input_tokens": 100,
+ "output_tokens": 5,
+ "input_tokens_details": {"cached_tokens": 75, "cache_write_tokens": 20},
+ }
+ },
+ }
+ handler.http_client = FakeHttpClient(
+ FakeStreamResponse(200, [f"data: {json.dumps(completed)}\n\n", "data: [DONE]\n\n"])
+ )
+ body = {"model": "gpt-5.4", "input": "hi"}
+ usage = asyncio.run(
+ handler._ws_http_fallback(FakeWebSocket(), body, json.dumps(body), {}, "req_w")
+ )
+ assert usage == (100, 5, 75, 20, 5, False)
def test_fallback_refreshes_codex_rate_limit_state(self, monkeypatch):
"""A successful fallback refreshes Codex /stats from response headers.