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(backends/litellm): report requested model in OpenAI streaming chunks (#2690)

Abhay Singh8538a831531b
diff --git a/headroom/backends/litellm.py b/headroom/backends/litellm.py
index ac3aad7ac..d2822d016 100644
--- a/headroom/backends/litellm.py
+++ b/headroom/backends/litellm.py
@@ -1979,6 +1979,15 @@ class LiteLLMBackend(Backend):
async for chunk in response:
chunk_dict = chunk.model_dump(exclude_none=True, exclude_unset=True)
+ # Report the model the client requested, not the LiteLLM-mapped
+ # provider slug (e.g. "openrouter/qwen3",
+ # "bedrock/us.anthropic.claude-..."). send_openai_message already
+ # rewrites the model to original_model on the non-streaming path;
+ # without this the streaming and non-streaming responses disagree
+ # and OpenAI clients that key cost/telemetry on the model field
+ # see an unrecognized name for every streamed request.
+ if "model" in chunk_dict:
+ chunk_dict["model"] = original_model
yield f"data: {json.dumps(chunk_dict)}\n\n"
yield "data: [DONE]\n\n"
diff --git a/tests/test_litellm_openai_passthrough.py b/tests/test_litellm_openai_passthrough.py
index 79ea524a2..430497656 100644
--- a/tests/test_litellm_openai_passthrough.py
+++ b/tests/test_litellm_openai_passthrough.py
@@ -105,6 +105,40 @@ async def test_chat_template_kwargs_forwarded_streaming() -> None:
assert chunks[-1] == "data: [DONE]\n\n"
+async def test_streaming_chunks_report_requested_model_not_mapped_slug() -> None:
+ """Streamed chunks must carry the client's requested model, not the
+ LiteLLM-mapped provider slug (matches the non-streaming path)."""
+ import json
+
+ backend = make_backend() # provider="openrouter"
+
+ # LiteLLM tags each chunk with the mapped model it was called with
+ # ("openrouter/qwen3"); the client asked for "qwen3".
+ stream = FakeAsyncStream(
+ [
+ SimpleNamespace(
+ model_dump=lambda **kwargs: {
+ "id": "chunk1",
+ "model": "openrouter/qwen3",
+ "choices": [{"index": 0, "delta": {"content": "hi"}}],
+ }
+ ),
+ ]
+ )
+
+ with patch("headroom.backends.litellm.acompletion", new_callable=AsyncMock) as mock_acomp:
+ mock_acomp.return_value = stream
+
+ chunks = [
+ chunk async for chunk in backend.stream_openai_message(request_body(model="qwen3"), {})
+ ]
+
+ data_chunks = [c for c in chunks if c.startswith("data: ") and "[DONE]" not in c]
+ payload = json.loads(data_chunks[0][len("data: ") :])
+ assert payload["model"] == "qwen3"
+
+
@pytest.mark.asyncio
async def test_standard_only_body_has_no_extra_body() -> None:
backend = make_backend()