Repository navigation
Expand file tree
/
Copy pathtest_llm_usage.py
More file actions
209 lines (166 loc) · 6.06 KB
/
Copy pathtest_llm_usage.py
File metadata and controls
209 lines (166 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import asyncio
from types import SimpleNamespace
from leansearchv2.llm import LLMClient
def test_record_usage_supports_anthropic_fields() -> None:
client = LLMClient.__new__(LLMClient)
client._usage = {
"requests": 0,
"input_tokens": 0,
"output_tokens": 0,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
}
client._record_usage(
SimpleNamespace(
input_tokens=120,
output_tokens=30,
cache_creation_input_tokens=10,
cache_read_input_tokens=20,
)
)
assert client.usage_snapshot() == {
"requests": 1,
"input_tokens": 120,
"output_tokens": 30,
"cache_creation_input_tokens": 10,
"cache_read_input_tokens": 20,
}
def test_record_usage_supports_openai_fields() -> None:
client = LLMClient.__new__(LLMClient)
client._usage = {
"requests": 0,
"input_tokens": 0,
"output_tokens": 0,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
}
client._record_usage(SimpleNamespace(prompt_tokens=9, completion_tokens=4))
assert client.usage_snapshot()["input_tokens"] == 9
assert client.usage_snapshot()["output_tokens"] == 4
def test_record_usage_supports_gemini_mapping() -> None:
client = LLMClient.__new__(LLMClient)
client._usage = {
"requests": 0,
"input_tokens": 0,
"output_tokens": 0,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
}
client._record_usage(
{
"input_tokens": 90,
"output_tokens": 45,
"cache_read_input_tokens": 12,
"thinking_tokens": 30,
}
)
assert client.usage_snapshot() == {
"requests": 1,
"input_tokens": 90,
"output_tokens": 45,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 12,
}
def test_gemini_vertex_chat_forwards_high_thinking_without_tools() -> None:
captured: dict = {}
class _GeminiClient:
async def complete(self, messages, tools, sampling, seed):
captured.update(
messages=messages,
tools=tools,
sampling=sampling,
seed=seed,
)
return SimpleNamespace(
content="answer",
usage={"input_tokens": 7, "output_tokens": 11},
)
client = LLMClient.__new__(LLMClient)
client.provider = "gemini_vertex"
client.profile = "gemini31pro"
client.model = "gemini-3.1-pro-preview-customtools"
client._temperature_default = 0.0
client._temperature_configured = False
client._top_p = 1.0
client._max_tokens_default = 65536
client._thinking_level = "HIGH"
client._seed = 20260806
client._client = _GeminiClient()
client._usage = {
"requests": 0,
"input_tokens": 0,
"output_tokens": 0,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
}
result = asyncio.run(client.chat([{"role": "user", "content": "test"}]))
assert result == "answer"
assert captured["tools"] == []
assert captured["sampling"]["thinking_level"] == "HIGH"
assert captured["sampling"]["temperature"] is None
assert captured["sampling"]["tool_choice"] == "none"
assert captured["sampling"]["max_tokens"] == 65536
assert captured["seed"] == 20260806
assert client.usage_snapshot()["output_tokens"] == 11
def test_llm_client_aclose_supports_async_provider_clients() -> None:
closed = False
class _Client:
async def aclose(self):
nonlocal closed
closed = True
client = LLMClient.__new__(LLMClient)
client._client = _Client()
asyncio.run(client.aclose())
assert closed
def test_sonnet5_vertex_omits_deprecated_temperature() -> None:
captured: dict = {}
class _Messages:
async def create(self, **kwargs):
captured.update(kwargs)
return SimpleNamespace(content=[], usage=None)
client = LLMClient.__new__(LLMClient)
client.provider = "anthropic_vertex"
client.profile = "sonnet5"
client.model = "claude-sonnet-5"
client._temperature_default = 0.0
client._top_p = None
client._max_tokens_default = 1024
client._client = SimpleNamespace(messages=_Messages())
asyncio.run(client.chat([{"role": "user", "content": "test"}], temperature=0.0))
assert captured["model"] == "claude-sonnet-5"
assert "temperature" not in captured
def test_sonnet5_vertex_forwards_thinking_and_effort_configuration() -> None:
captured: dict = {}
class _Messages:
async def create(self, **kwargs):
captured.update(kwargs)
return SimpleNamespace(content=[], usage=None)
client = LLMClient.__new__(LLMClient)
client.provider = "anthropic_vertex"
client.profile = "sonnet5"
client.model = "claude-sonnet-5"
client._temperature_default = 0.0
client._top_p = None
client._max_tokens_default = 1024
client._thinking = {"type": "adaptive", "display": "omitted"}
client._output_config = {"effort": "high"}
client._client = SimpleNamespace(messages=_Messages())
asyncio.run(client.chat([{"role": "user", "content": "test"}]))
assert captured["thinking"] == {"type": "adaptive", "display": "omitted"}
assert captured["output_config"] == {"effort": "high"}
def test_sonnet45_vertex_keeps_temperature() -> None:
captured: dict = {}
class _Messages:
async def create(self, **kwargs):
captured.update(kwargs)
return SimpleNamespace(content=[], usage=None)
client = LLMClient.__new__(LLMClient)
client.provider = "anthropic_vertex"
client.profile = "sonnet45"
client.model = "claude-sonnet-4-5@20250929"
client._temperature_default = 0.0
client._top_p = None
client._max_tokens_default = 1024
client._client = SimpleNamespace(messages=_Messages())
asyncio.run(client.chat([{"role": "user", "content": "test"}], temperature=0.0))
assert captured["temperature"] == 0.0