Commit 684bbb6
authored
feat(message): add MessageMetadata TypedDict for token/cost tracking (#943)
* feat(message): add MessageMetadata TypedDict for token/cost tracking
Per Erik's review on PR #939, adding Message.metadata field to store:
- Token usage (input_tokens, output_tokens, cache_read_tokens, cache_creation_tokens)
- Cost in USD
- Model used for the response
Design decisions:
- TypedDict with total=False for type safety + optional fields
- Only non-None metadata is serialized (compact JSONL storage)
- Full support for JSON/JSONL and TOML roundtrips
Part of the cost awareness feature. Follow-up to PR #939.
🤖 Generated with [gptme](https://github.com/gptme/gptme)
* refactor(message): restructure MessageMetadata to use nested tokens format
Address Erik's review: https://github.com/gptme/gptme/pull/943/files#r2603233028
Changes:
- Replace flat token fields with nested tokens structure
- tokens.input can be dict (base, cache_read, cache_write) or int
- tokens.output is int
- Add TokensInput and Tokens TypedDicts
- Add helper functions for TOML serialization of nested dicts
- Update tests to use new structure with proper type narrowing
New format:
{
"model": "claude-sonnet",
"tokens": {
"input": {"base": 100, "cache_read": 80},
"output": 50
},
"cost": 0.005
}
* refactor(message): use flat token format per review feedback
Per Erik's feedback: #943 (comment)
Changes:
- Remove nested TokensInput and Tokens TypedDicts
- Flatten MessageMetadata to use: input_tokens, output_tokens,
cache_read_tokens, cache_creation_tokens (matches cost_tracker)
- Simplify _format_toml_value (no longer needs dict handling)
- Update tests for new flat format
This aligns with cost_tracker.py and common industry conventions.
* feat(llm): integrate MessageMetadata into message generation
Per Erik's review: integrate metadata into gptme/llm/ where messages are
constructed/generated.
Changes:
- Modify _record_usage() in both providers to return MessageMetadata
- Update chat() functions to return tuple[str, MessageMetadata | None]
- Update stream() generators to return metadata via generator return value
- Add _StreamWithMetadata wrapper to capture generator return values
- Update _reply_stream() to attach metadata to returned Messages
- Update _chat_complete() to handle tuple returns and propagate metadata
- Fix callers in util/prompt.py and util/auto_naming.py
The MessageMetadata now flows from provider usage tracking through to
the final Message object, enabling token/cost tracking per message.
* fix: update callers of _chat_complete for tuple return type
After integrating MessageMetadata into _chat_complete (returning
tuple[str, MessageMetadata | None]), several callers were not updated:
- gptme/tools/morph.py: Unpack tuple to get string for .strip()
- gptme/hooks/form_autodetect.py: Unpack tuple for re.search/json.loads
- gptme/server/api_v2_sessions.py: Handle non-streaming case by
wrapping response in list for iteration compatibility
All three files now correctly unpack the tuple return value.
Co-authored-by: Bob <bob@superuserlabs.org>
* fix(test): update mock to match new _chat_complete tuple return format
The test mock was returning the old format [["response"]] but
_chat_complete now returns (str, MessageMetadata | None) tuple.1 parent ed409cb commit 684bbb6
File tree
11 files changed
+268
-50
lines changed- gptme
- hooks
- llm
- server
- tools
- util
- tests
11 files changed
+268
-50
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
| 126 | + | |
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| 159 | + | |
159 | 160 | | |
160 | 161 | | |
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
164 | | - | |
| 165 | + | |
165 | 166 | | |
166 | | - | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
167 | 170 | | |
168 | 171 | | |
169 | 172 | | |
| |||
173 | 176 | | |
174 | 177 | | |
175 | 178 | | |
176 | | - | |
| 179 | + | |
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
| |||
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
194 | | - | |
| 197 | + | |
195 | 198 | | |
196 | 199 | | |
197 | 200 | | |
| |||
202 | 205 | | |
203 | 206 | | |
204 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
205 | 229 | | |
206 | 230 | | |
207 | 231 | | |
208 | 232 | | |
209 | 233 | | |
210 | 234 | | |
211 | | - | |
| 235 | + | |
212 | 236 | | |
213 | 237 | | |
214 | 238 | | |
215 | | - | |
| 239 | + | |
| 240 | + | |
216 | 241 | | |
217 | | - | |
| 242 | + | |
218 | 243 | | |
219 | 244 | | |
| 245 | + | |
220 | 246 | | |
221 | 247 | | |
222 | 248 | | |
| |||
247 | 273 | | |
248 | 274 | | |
249 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
250 | 280 | | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
| 281 | + | |
256 | 282 | | |
257 | 283 | | |
258 | 284 | | |
| |||
310 | 336 | | |
311 | 337 | | |
312 | 338 | | |
313 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
314 | 342 | | |
315 | 343 | | |
316 | 344 | | |
| |||
322 | 350 | | |
323 | 351 | | |
324 | 352 | | |
325 | | - | |
| 353 | + | |
326 | 354 | | |
327 | 355 | | |
328 | 356 | | |
| |||
349 | 377 | | |
350 | 378 | | |
351 | 379 | | |
352 | | - | |
| 380 | + | |
353 | 381 | | |
354 | 382 | | |
355 | 383 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
99 | | - | |
| 98 | + | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
128 | 140 | | |
129 | 141 | | |
130 | 142 | | |
| |||
305 | 317 | | |
306 | 318 | | |
307 | 319 | | |
308 | | - | |
| 320 | + | |
309 | 321 | | |
310 | 322 | | |
311 | 323 | | |
| |||
361 | 373 | | |
362 | 374 | | |
363 | 375 | | |
364 | | - | |
| 376 | + | |
365 | 377 | | |
366 | 378 | | |
367 | 379 | | |
| |||
374 | 386 | | |
375 | 387 | | |
376 | 388 | | |
377 | | - | |
| 389 | + | |
378 | 390 | | |
379 | 391 | | |
380 | 392 | | |
| |||
383 | 395 | | |
384 | 396 | | |
385 | 397 | | |
386 | | - | |
| 398 | + | |
387 | 399 | | |
388 | 400 | | |
389 | 401 | | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
390 | 405 | | |
391 | 406 | | |
392 | 407 | | |
| |||
493 | 508 | | |
494 | 509 | | |
495 | 510 | | |
496 | | - | |
| 511 | + | |
| 512 | + | |
497 | 513 | | |
498 | 514 | | |
499 | 515 | | |
500 | 516 | | |
501 | 517 | | |
502 | 518 | | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
503 | 522 | | |
504 | 523 | | |
505 | 524 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
| 39 | + | |
| 40 | + | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
70 | 80 | | |
71 | 81 | | |
72 | 82 | | |
| |||
260 | 270 | | |
261 | 271 | | |
262 | 272 | | |
263 | | - | |
| 273 | + | |
264 | 274 | | |
265 | 275 | | |
266 | 276 | | |
| |||
294 | 304 | | |
295 | 305 | | |
296 | 306 | | |
297 | | - | |
| 307 | + | |
298 | 308 | | |
299 | 309 | | |
300 | 310 | | |
| |||
313 | 323 | | |
314 | 324 | | |
315 | 325 | | |
316 | | - | |
| 326 | + | |
317 | 327 | | |
318 | 328 | | |
319 | 329 | | |
| |||
345 | 355 | | |
346 | 356 | | |
347 | 357 | | |
348 | | - | |
| 358 | + | |
349 | 359 | | |
350 | 360 | | |
351 | 361 | | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
352 | 365 | | |
353 | 366 | | |
354 | 367 | | |
| |||
389 | 402 | | |
390 | 403 | | |
391 | 404 | | |
| 405 | + | |
392 | 406 | | |
393 | | - | |
| 407 | + | |
394 | 408 | | |
395 | 409 | | |
396 | 410 | | |
| |||
441 | 455 | | |
442 | 456 | | |
443 | 457 | | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
444 | 461 | | |
445 | 462 | | |
446 | 463 | | |
| |||
0 commit comments