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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(proxy): forward Copilot accounting headers on streaming path (#4431)
divyamnpll66023a0187b3
diff --git a/headroom/proxy/handlers/streaming.py b/headroom/proxy/handlers/streaming.pyindex 2444a6c4f..54f764715 100644--- a/headroom/proxy/handlers/streaming.py+++ b/headroom/proxy/handlers/streaming.py@@ -1616,7 +1616,11 @@ class StreamingMixin:# message id + request id. ``x-litellm-key-*`` carries the per-key# spend/budget envelope a LiteLLM virtual key reports per request;# downstream status widgets read it from the streaming response the- # same way they already do from the buffered path. The buffered+ # same way they already do from the buffered path. GitHub Copilot keys+ # its AI-credit accounting off ``x-github-*`` / ``*copilot*`` headers;+ # without them the Copilot CLI marks streamed usage records+ # ``aiCreditsStatus: unavailable`` and silently under-reports credits.+ # The buffered# (non-streaming) path already forwards every upstream header, so# this keeps the two paths symmetric.forwarded_headers = {@@ -1625,6 +1629,8 @@ class StreamingMixin:if "ratelimit" in k.lower()or k.lower().startswith("x-codex")or k.lower().startswith("x-litellm-key-")+ or k.lower().startswith("x-github")+ or "copilot" in k.lower()or k.lower() in ("request-id", "anthropic-request-id", "x-request-id")}# Headroom's own compression metrics, as the buffered path stamps them.diff --git a/tests/test_proxy_streaming_ratelimit_headers.py b/tests/test_proxy_streaming_ratelimit_headers.pyindex d3e175f26..67779e88e 100644--- a/tests/test_proxy_streaming_ratelimit_headers.py+++ b/tests/test_proxy_streaming_ratelimit_headers.py@@ -349,6 +349,59 @@ class TestStreamingRatelimitHeaderForwarding:assert result.headers.get("x-litellm-key-budget-remaining") == "87.153"assert result.headers.get("cf-ray") is None+ @pytest.mark.asyncio+ async def test_copilot_accounting_headers_forwarded_in_streaming(self, monkeypatch):+ """Copilot's ``x-github-*`` / ``*copilot*`` headers survive the streaming path.++ The Copilot CLI derives AI-credit accounting from these headers; when+ they are dropped it records ``aiCreditsStatus: unavailable`` and+ under-reports credits while token counts still look correct.+ """++ # A Copilot URL makes the proxy mint a Copilot OAuth token, which needs a+ # real login; stub it so the test runs without one.+ async def fake_copilot_auth(headers: dict[str, str], *, url: str) -> dict[str, str]:+ return {**headers, "authorization": "Bearer copilot-test-token"}++ monkeypatch.setattr(streaming_module, "apply_copilot_api_auth", fake_copilot_auth)+ proxy = self._create_mock_proxy()+ mock_response = self._create_mock_upstream_response()+ mock_response.headers = httpx.Headers(+ {+ "content-type": "text/event-stream",+ "x-github-request-id": "ABCD:1234:5678",+ "x-copilot-usage": "nano-aiu=30213500000",+ "cf-ray": "ray-789",+ }+ )+ proxy.http_client.build_request = MagicMock(return_value=MagicMock())+ proxy.http_client.send = AsyncMock(return_value=mock_response)++ result = await proxy._stream_response(+ url="https://api.githubcopilot.com/v1/messages",+ headers={"authorization": "Bearer test"},+ body={+ "model": "claude-opus-4.8",+ "max_tokens": 100,+ "stream": True,+ "messages": [{"role": "user", "content": "hi"}],+ },+ provider="anthropic",+ model="claude-opus-4.8",+ request_id="test-copilot-accounting",+ original_tokens=10,+ optimized_tokens=10,+ tokens_saved=0,+ transforms_applied=[],+ tags={},+ optimization_latency=0.0,+ )++ assert result.media_type == "text/event-stream"+ assert result.headers.get("x-github-request-id") == "ABCD:1234:5678"+ assert result.headers.get("x-copilot-usage") == "nano-aiu=30213500000"+ assert result.headers.get("cf-ray") is None+@pytest.mark.asyncioasync def test_no_ratelimit_headers_still_works(self):"""When upstream has no ratelimit headers, streaming should still work."""