Non-streaming path

This commit is contained in:
Ian 2026-09-29 18:22:03 -04:00 • committed by Ahmed Allam
parent c9aebc6c87
commit 3fbccc6b1d
2 changed files with 32 additions and 5 deletions

View file

@ -792,6 +792,16 @@ def _install_openrouter_stream_cost_capture() -> None:
json_mode=json_mode,
)
def transform_response(self, *args: Any, **kwargs: Any) -> Any:
# Non-streamed replies (LLM_DISABLE_STREAMING) skip the chunk parser.
response = super().transform_response(*args, **kwargs)
raw_response = kwargs.get("raw_response", args[1] if len(args) > 1 else None)
with contextlib.suppress(Exception):
body = raw_response.json() # type: ignore[union-attr]
if body.get("usage"):
record_openrouter_provider(body.get("provider"), body["usage"])
return response
def transform_request(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
# Pin each agent's calls to one upstream provider so its prompt cache
# survives between turns.

View file

@ -5,8 +5,9 @@ from __future__ import annotations
import uuid
from types import SimpleNamespace
from typing import TYPE_CHECKING, Any
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, call, patch
import httpx
import litellm
import pytest
from litellm.types.utils import LlmProviders
@ -267,7 +268,7 @@ def test_openrouter_stream_handler_records_cost() -> None:
)
def test_openrouter_stream_handler_tallies_provider() -> None:
def test_openrouter_tallies_provider() -> None:
_install_openrouter_stream_cost_capture()
config = ProviderConfigManager.get_provider_chat_config(
model="z-ai/glm-5.3", provider=LlmProviders.OPENROUTER
@ -292,10 +293,26 @@ def test_openrouter_stream_handler_tallies_provider() -> None:
"usage": usage,
}
)
# Non-streamed replies (LLM_DISABLE_STREAMING) carry the same fields.
reply = {
"choices": [{"message": {"role": "assistant"}}],
"provider": "Together",
"usage": usage,
}
config.transform_response(
"z-ai/glm-5.3",
httpx.Response(200, json=reply),
litellm.ModelResponse(),
MagicMock(),
{},
[],
{},
{},
None,
)
report_state.record_llm_provider.assert_called_once_with(
"Together", agent_id=None, input_tokens=1000, cached_tokens=900, cost=0.002
)
tally = call("Together", agent_id=None, input_tokens=1000, cached_tokens=900, cost=0.002)
assert report_state.record_llm_provider.call_args_list == [tally, tally]
def test_provider_tally_survives_run_record_round_trip() -> None: