|
| 1 | +"""Tests for LiteLLM cost tracking functionality.""" |
| 2 | + |
| 3 | +import litellm |
| 4 | +import pytest |
| 5 | +from litellm.types.utils import Choices, Message, ModelResponse, Usage |
| 6 | + |
| 7 | +from agents.extensions.models.litellm_model import LitellmModel |
| 8 | +from agents.model_settings import ModelSettings |
| 9 | +from agents.models.interface import ModelTracing |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.allow_call_model_methods |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_cost_extracted_when_track_cost_enabled(monkeypatch): |
| 15 | + """Test that cost is extracted from LiteLLM response when track_cost=True.""" |
| 16 | + |
| 17 | + async def fake_acompletion(model, messages=None, **kwargs): |
| 18 | + msg = Message(role="assistant", content="Test response") |
| 19 | + choice = Choices(index=0, message=msg) |
| 20 | + response = ModelResponse( |
| 21 | + choices=[choice], |
| 22 | + usage=Usage(prompt_tokens=10, completion_tokens=20, total_tokens=30), |
| 23 | + ) |
| 24 | + # Simulate LiteLLM's hidden params with cost. |
| 25 | + response._hidden_params = {"response_cost": 0.00042} |
| 26 | + return response |
| 27 | + |
| 28 | + monkeypatch.setattr(litellm, "acompletion", fake_acompletion) |
| 29 | + |
| 30 | + model = LitellmModel(model="test-model", api_key="test-key") |
| 31 | + result = await model.get_response( |
| 32 | + system_instructions=None, |
| 33 | + input=[], |
| 34 | + model_settings=ModelSettings(track_cost=True), # Enable cost tracking |
| 35 | + tools=[], |
| 36 | + output_schema=None, |
| 37 | + handoffs=[], |
| 38 | + tracing=ModelTracing.DISABLED, |
| 39 | + previous_response_id=None, |
| 40 | + ) |
| 41 | + |
| 42 | + # Verify that cost was extracted. |
| 43 | + assert result.usage.cost == 0.00042 |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.allow_call_model_methods |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_cost_none_when_track_cost_disabled(monkeypatch): |
| 49 | + """Test that cost is None when track_cost=False (default).""" |
| 50 | + |
| 51 | + async def fake_acompletion(model, messages=None, **kwargs): |
| 52 | + msg = Message(role="assistant", content="Test response") |
| 53 | + choice = Choices(index=0, message=msg) |
| 54 | + response = ModelResponse( |
| 55 | + choices=[choice], |
| 56 | + usage=Usage(prompt_tokens=10, completion_tokens=20, total_tokens=30), |
| 57 | + ) |
| 58 | + # Even if LiteLLM provides cost, it should be ignored. |
| 59 | + response._hidden_params = {"response_cost": 0.00042} |
| 60 | + return response |
| 61 | + |
| 62 | + monkeypatch.setattr(litellm, "acompletion", fake_acompletion) |
| 63 | + |
| 64 | + model = LitellmModel(model="test-model", api_key="test-key") |
| 65 | + result = await model.get_response( |
| 66 | + system_instructions=None, |
| 67 | + input=[], |
| 68 | + model_settings=ModelSettings(track_cost=False), # Disabled (default) |
| 69 | + tools=[], |
| 70 | + output_schema=None, |
| 71 | + handoffs=[], |
| 72 | + tracing=ModelTracing.DISABLED, |
| 73 | + previous_response_id=None, |
| 74 | + ) |
| 75 | + |
| 76 | + # Verify that cost is None when tracking is disabled. |
| 77 | + assert result.usage.cost is None |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.allow_call_model_methods |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_cost_none_when_not_provided(monkeypatch): |
| 83 | + """Test that cost is None when LiteLLM doesn't provide it.""" |
| 84 | + |
| 85 | + async def fake_acompletion(model, messages=None, **kwargs): |
| 86 | + msg = Message(role="assistant", content="Test response") |
| 87 | + choice = Choices(index=0, message=msg) |
| 88 | + response = ModelResponse( |
| 89 | + choices=[choice], |
| 90 | + usage=Usage(prompt_tokens=10, completion_tokens=20, total_tokens=30), |
| 91 | + ) |
| 92 | + # No _hidden_params or no cost in hidden params. |
| 93 | + return response |
| 94 | + |
| 95 | + monkeypatch.setattr(litellm, "acompletion", fake_acompletion) |
| 96 | + |
| 97 | + model = LitellmModel(model="test-model", api_key="test-key") |
| 98 | + result = await model.get_response( |
| 99 | + system_instructions=None, |
| 100 | + input=[], |
| 101 | + model_settings=ModelSettings(track_cost=True), |
| 102 | + tools=[], |
| 103 | + output_schema=None, |
| 104 | + handoffs=[], |
| 105 | + tracing=ModelTracing.DISABLED, |
| 106 | + previous_response_id=None, |
| 107 | + ) |
| 108 | + |
| 109 | + # Verify that cost is None when not provided. |
| 110 | + assert result.usage.cost is None |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.allow_call_model_methods |
| 114 | +@pytest.mark.asyncio |
| 115 | +async def test_cost_with_empty_hidden_params(monkeypatch): |
| 116 | + """Test that cost extraction handles empty _hidden_params gracefully.""" |
| 117 | + |
| 118 | + async def fake_acompletion(model, messages=None, **kwargs): |
| 119 | + msg = Message(role="assistant", content="Test response") |
| 120 | + choice = Choices(index=0, message=msg) |
| 121 | + response = ModelResponse( |
| 122 | + choices=[choice], |
| 123 | + usage=Usage(prompt_tokens=10, completion_tokens=20, total_tokens=30), |
| 124 | + ) |
| 125 | + # Empty hidden params. |
| 126 | + response._hidden_params = {} |
| 127 | + return response |
| 128 | + |
| 129 | + monkeypatch.setattr(litellm, "acompletion", fake_acompletion) |
| 130 | + |
| 131 | + model = LitellmModel(model="test-model", api_key="test-key") |
| 132 | + result = await model.get_response( |
| 133 | + system_instructions=None, |
| 134 | + input=[], |
| 135 | + model_settings=ModelSettings(track_cost=True), |
| 136 | + tools=[], |
| 137 | + output_schema=None, |
| 138 | + handoffs=[], |
| 139 | + tracing=ModelTracing.DISABLED, |
| 140 | + previous_response_id=None, |
| 141 | + ) |
| 142 | + |
| 143 | + # Verify that cost is None with empty hidden params. |
| 144 | + assert result.usage.cost is None |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.allow_call_model_methods |
| 148 | +@pytest.mark.asyncio |
| 149 | +async def test_cost_extraction_preserves_other_usage_fields(monkeypatch): |
| 150 | + """Test that cost extraction doesn't affect other usage fields.""" |
| 151 | + |
| 152 | + async def fake_acompletion(model, messages=None, **kwargs): |
| 153 | + msg = Message(role="assistant", content="Test response") |
| 154 | + choice = Choices(index=0, message=msg) |
| 155 | + response = ModelResponse( |
| 156 | + choices=[choice], |
| 157 | + usage=Usage(prompt_tokens=100, completion_tokens=50, total_tokens=150), |
| 158 | + ) |
| 159 | + response._hidden_params = {"response_cost": 0.001} |
| 160 | + return response |
| 161 | + |
| 162 | + monkeypatch.setattr(litellm, "acompletion", fake_acompletion) |
| 163 | + |
| 164 | + model = LitellmModel(model="test-model", api_key="test-key") |
| 165 | + result = await model.get_response( |
| 166 | + system_instructions=None, |
| 167 | + input=[], |
| 168 | + model_settings=ModelSettings(track_cost=True), |
| 169 | + tools=[], |
| 170 | + output_schema=None, |
| 171 | + handoffs=[], |
| 172 | + tracing=ModelTracing.DISABLED, |
| 173 | + previous_response_id=None, |
| 174 | + ) |
| 175 | + |
| 176 | + # Verify all usage fields are correct. |
| 177 | + assert result.usage.input_tokens == 100 |
| 178 | + assert result.usage.output_tokens == 50 |
| 179 | + assert result.usage.total_tokens == 150 |
| 180 | + assert result.usage.cost == 0.001 |
| 181 | + assert result.usage.requests == 1 |
0 commit comments