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(proxy/bedrock): record the real upstream status on InvokeModel outcomes (#4025)

Hill Patel159daa25a088
diff --git a/headroom/proxy/handlers/bedrock.py b/headroom/proxy/handlers/bedrock.py
index 0a40886c4..24c5d510a 100644
--- a/headroom/proxy/handlers/bedrock.py
+++ b/headroom/proxy/handlers/bedrock.py
@@ -243,6 +243,14 @@ class BedrockHandlerMixin:
# contract treats 0 as "not measured") — Bedrock responses are forwarded
# byte-faithfully and never parsed. The valuable figure, request-side
# compression, is recorded in full.
+ #
+ # status_code=response.status_code: unlike streaming.py's SDK-based
+ # _stream_response_bedrock (which never holds an HTTP response object
+ # and so keeps the dataclass default of 200 — see PR #2682), this
+ # handler forwards via raw httpx and always has the real upstream
+ # status on the returned Response/StreamingResponse. Leaving it out
+ # here made every upstream 4xx/5xx (throttling, auth failure, model
+ # not found) record as a 200 success in the savings/cost funnel.
try:
from headroom.proxy.outcome import RequestOutcome
@@ -261,6 +269,7 @@ class BedrockHandlerMixin:
pipeline_timing=pipeline_timing,
tags=tags,
client=client,
+ status_code=response.status_code,
)
)
except Exception:
diff --git a/tests/test_proxy/test_bedrock_passthrough.py b/tests/test_proxy/test_bedrock_passthrough.py
index 45d4ed029..10f23205c 100644
--- a/tests/test_proxy/test_bedrock_passthrough.py
+++ b/tests/test_proxy/test_bedrock_passthrough.py
@@ -450,6 +450,34 @@ def test_outcome_recorded_with_bedrock_provider():
assert outcome.tokens_saved == 750
[email protected]("upstream_status", [400, 403, 429, 500, 503])
+def test_outcome_carries_real_upstream_status_on_error(upstream_status):
+ """Regression: handle_bedrock_invoke built RequestOutcome without a
+ status_code kwarg, so an upstream throttle/auth/model error recorded as
+ the dataclass default of 200 — the failed turn then fed the success
+ funnel (savings/cost/request-log) in emit_request_outcome instead of
+ being short-circuited by its `status_code >= 400` guard."""
+ app = create_app(_make_config())
+ with TestClient(app) as client:
+ proxy = client.app.state.proxy
+ _install_fake_client(
+ proxy, _FakeUpstream(status_code=upstream_status, chunks=(b'{"error":true}',))
+ )
+ proxy._record_request_outcome = AsyncMock()
+ proxy.anthropic_pipeline.apply = MagicMock(
+ return_value=_FakeResult([{"role": "user", "content": "c"}], 1000, 250)
+ )
+ resp = client.post(
+ INVOKE,
+ json={"messages": [{"role": "user", "content": "q" * 3000}], "max_tokens": 8},
+ )
+
+ assert resp.status_code == upstream_status
+ assert proxy._record_request_outcome.await_count == 1
+ outcome = proxy._record_request_outcome.await_args.args[0]
+ assert outcome.status_code == upstream_status
+
+
# ── env config path ───────────────────────────────────────────────────