mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-16 23:41:43 +00:00
fix(openrouter): track Responses API stream cost
This commit is contained in:
parent
2f833f93e6
commit
8a051733cb
2 changed files with 60 additions and 1 deletions
|
|
@ -15,7 +15,12 @@ import httpx
|
|||
import litellm
|
||||
from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig
|
||||
from litellm.secret_managers.main import get_secret_str
|
||||
from litellm.types.llms.openai import ResponseInputParam, ResponsesAPIResponse
|
||||
from litellm.types.llms.openai import (
|
||||
ResponseCompletedEvent,
|
||||
ResponseInputParam,
|
||||
ResponsesAPIResponse,
|
||||
ResponsesAPIStreamingResponse,
|
||||
)
|
||||
from litellm.types.router import GenericLiteLLMParams
|
||||
from litellm.types.utils import LlmProviders
|
||||
|
||||
|
|
@ -143,3 +148,27 @@ class OpenRouterResponsesAPIConfig(OpenAIResponsesAPIConfig):
|
|||
pass
|
||||
|
||||
return response
|
||||
|
||||
def transform_streaming_response(
|
||||
self,
|
||||
model: str,
|
||||
parsed_chunk: dict,
|
||||
logging_obj: Any,
|
||||
) -> ResponsesAPIStreamingResponse:
|
||||
response_event: Final = super().transform_streaming_response(
|
||||
model=model,
|
||||
parsed_chunk=parsed_chunk,
|
||||
logging_obj=logging_obj,
|
||||
)
|
||||
if not isinstance(response_event, ResponseCompletedEvent):
|
||||
return response_event
|
||||
|
||||
usage: Final = response_event.response.usage
|
||||
if usage is None or usage.cost is None:
|
||||
return response_event
|
||||
|
||||
response_event.response._hidden_params["additional_headers"] = {
|
||||
**response_event.response._hidden_params.get("additional_headers", {}),
|
||||
"llm_provider-x-litellm-response-cost": float(usage.cost),
|
||||
}
|
||||
return response_event
|
||||
|
|
|
|||
|
|
@ -186,6 +186,36 @@ class TestOpenRouterResponsesAPICostTracking:
|
|||
"additional_headers", {}
|
||||
)
|
||||
|
||||
def test_transform_streaming_response_extracts_completed_response_cost(self):
|
||||
config = OpenRouterResponsesAPIConfig()
|
||||
|
||||
result = config.transform_streaming_response(
|
||||
model="openai/gpt-5-mini",
|
||||
parsed_chunk={
|
||||
"type": "response.completed",
|
||||
"response": {
|
||||
"id": "resp_abc123",
|
||||
"object": "response",
|
||||
"created_at": 1700000000,
|
||||
"status": "completed",
|
||||
"model": "openai/gpt-5-mini",
|
||||
"output": [],
|
||||
"usage": {
|
||||
"input_tokens": 100,
|
||||
"output_tokens": 50,
|
||||
"total_tokens": 150,
|
||||
"cost": 0.01234,
|
||||
},
|
||||
},
|
||||
},
|
||||
logging_obj=Mock(),
|
||||
)
|
||||
|
||||
assert (
|
||||
result.response._hidden_params["additional_headers"]["llm_provider-x-litellm-response-cost"]
|
||||
== 0.01234
|
||||
)
|
||||
|
||||
|
||||
class TestOpenRouterResponsesAPIRegistration:
|
||||
"""Test that OpenRouter is properly registered as a native Responses API provider."""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue