From a190dee04f64ebb1c98a92fa82675f7d4af38c1a Mon Sep 17 00:00:00 2001 From: Chenglun Hu Date: Wed, 26 Aug 2026 23:57:40 +0800 Subject: [PATCH] test(anthropic): assert count_tokens return value in api_base routing tests Capture the TokenCountResponse and assert total_tokens flows through from the handler, so each test verifies a real caller-observable output rather than only the mocked handler's api_base kwarg (clears the test-quality gate). --- .../test_anthropic_count_tokens_transformation.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py b/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py index b64a1749fea..1b6c2860e24 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_count_tokens_transformation.py @@ -165,13 +165,14 @@ async def test_count_tokens_env_fallback_prefers_anthropic_api_base(monkeypatch) monkeypatch.setenv("ANTHROPIC_BASE_URL", "http://from-base-url:9000") with patch(_HANDLER, new=AsyncMock(return_value={"input_tokens": 5})) as m: - await AnthropicTokenCounter().count_tokens( + result = await AnthropicTokenCounter().count_tokens( model_to_use="claude-3-5-sonnet", messages=[{"role": "user", "content": "hi"}], contents=None, ) assert m.call_args.kwargs["api_base"] == "http://from-api-base:8000" + assert result.total_tokens == 5 @pytest.mark.asyncio @@ -184,13 +185,14 @@ async def test_count_tokens_falls_back_to_anthropic_base_url(monkeypatch): monkeypatch.setenv("ANTHROPIC_BASE_URL", "http://from-base-url:9000") with patch(_HANDLER, new=AsyncMock(return_value={"input_tokens": 5})) as m: - await AnthropicTokenCounter().count_tokens( + result = await AnthropicTokenCounter().count_tokens( model_to_use="claude-3-5-sonnet", messages=[{"role": "user", "content": "hi"}], contents=None, ) assert m.call_args.kwargs["api_base"] == "http://from-base-url:9000" + assert result.total_tokens == 5 @pytest.mark.asyncio @@ -201,7 +203,7 @@ async def test_count_tokens_litellm_params_api_base_wins(monkeypatch): monkeypatch.setenv("ANTHROPIC_BASE_URL", "http://from-base-url:9000") with patch(_HANDLER, new=AsyncMock(return_value={"input_tokens": 5})) as m: - await AnthropicTokenCounter().count_tokens( + result = await AnthropicTokenCounter().count_tokens( model_to_use="claude-3-5-sonnet", messages=[{"role": "user", "content": "hi"}], contents=None, @@ -209,3 +211,4 @@ async def test_count_tokens_litellm_params_api_base_wins(monkeypatch): ) assert m.call_args.kwargs["api_base"] == "http://explicit:7000" + assert result.total_tokens == 5