Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Fix/openrouter stream usage id 8913 (#11004)

daarko10e2d147102d62
diff --git a/litellm/llms/openrouter/chat/transformation.py b/litellm/llms/openrouter/chat/transformation.py
index 77f402a1317..e3f9d5c3dd0 100644
--- a/litellm/llms/openrouter/chat/transformation.py
+++ b/litellm/llms/openrouter/chat/transformation.py
@@ -120,6 +120,7 @@ class OpenRouterChatCompletionStreamingHandler(BaseModelResponseIterator):
id=chunk["id"],
object="chat.completion.chunk",
created=chunk["created"],
+ usage=chunk.get("usage"),
model=chunk["model"],
choices=new_choices,
)
diff --git a/tests/litellm/llms/openrouter/chat/test_openrouter_chat_transformation.py b/tests/litellm/llms/openrouter/chat/test_openrouter_chat_transformation.py
index de0b284f0a3..ecdcadfb735 100644
--- a/tests/litellm/llms/openrouter/chat/test_openrouter_chat_transformation.py
+++ b/tests/litellm/llms/openrouter/chat/test_openrouter_chat_transformation.py
@@ -26,6 +26,11 @@ class TestOpenRouterChatCompletionStreamingHandler:
"id": "test_id",
"created": 1234567890,
"model": "test_model",
+ "usage": {
+ "prompt_tokens": 10,
+ "completion_tokens": 20,
+ "total_tokens": 30
+ },
"choices": [
{"delta": {"content": "test content", "reasoning": "test reasoning"}}
],
@@ -39,6 +44,9 @@ class TestOpenRouterChatCompletionStreamingHandler:
assert result.object == "chat.completion.chunk"
assert result.created == 1234567890
assert result.model == "test_model"
+ assert result.usage.prompt_tokens == chunk["usage"]["prompt_tokens"]
+ assert result.usage.completion_tokens == chunk["usage"]["completion_tokens"]
+ assert result.usage.total_tokens == chunk["usage"]["total_tokens"]
assert len(result.choices) == 1
assert result.choices[0]["delta"]["reasoning_content"] == "test reasoning"