Commit e3427ff
fix(usage): record UsageLog on the streaming path too
Stage A.1 hooked only Router.Chat; the production agent uses
Router.ChatStream, so after deploy usage_log stayed empty despite
"usage logging enabled" and successful LLM calls.
Fix plumbs usage through the streaming path end to end:
* StreamChunk gets a Usage field populated on the final Done chunk.
* openai_compat.readSSE parses the SSE terminal chunk's usage block
(OpenRouter sends one with choices=[] when usage.include=true),
including cached_tokens and reasoning_tokens from the nested
*_details sub-objects. Bumps the SSE scanner buffer to 1MB so long
reasoning traces don't hide the usage chunk.
* Router.ChatStream wraps the provider's channel with
instrumentedStream: it tees chunks through to the caller, and when
the Done chunk arrives it calls the same recordUsage routine
Router.Chat uses. Start-of-stream errors and sync/fallback paths
also get a UsageLog row (with error_class set) so dashboards
surface failed calls.
After this, every LLM request — streamed or not, successful or not —
produces exactly one usage_log record per provider hop.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 22c80ac commit e3427ff
3 files changed
Lines changed: 74 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
143 | 144 | | |
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
| 151 | + | |
150 | 152 | | |
151 | 153 | | |
152 | 154 | | |
| |||
455 | 457 | | |
456 | 458 | | |
457 | 459 | | |
| 460 | + | |
| 461 | + | |
458 | 462 | | |
459 | 463 | | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
460 | 468 | | |
461 | 469 | | |
462 | 470 | | |
| |||
471 | 479 | | |
472 | 480 | | |
473 | 481 | | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
474 | 500 | | |
475 | 501 | | |
476 | 502 | | |
| |||
517 | 543 | | |
518 | 544 | | |
519 | 545 | | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
520 | 549 | | |
521 | 550 | | |
522 | 551 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
436 | 436 | | |
437 | 437 | | |
438 | 438 | | |
| 439 | + | |
439 | 440 | | |
440 | 441 | | |
441 | | - | |
| 442 | + | |
442 | 443 | | |
443 | 444 | | |
444 | 445 | | |
445 | | - | |
446 | | - | |
447 | | - | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
448 | 454 | | |
449 | | - | |
| 455 | + | |
450 | 456 | | |
451 | 457 | | |
452 | 458 | | |
453 | 459 | | |
454 | 460 | | |
455 | | - | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
456 | 487 | | |
| 488 | + | |
| 489 | + | |
457 | 490 | | |
| 491 | + | |
458 | 492 | | |
459 | 493 | | |
460 | 494 | | |
461 | 495 | | |
462 | | - | |
| 496 | + | |
463 | 497 | | |
464 | 498 | | |
465 | 499 | | |
| |||
479 | 513 | | |
480 | 514 | | |
481 | 515 | | |
482 | | - | |
| 516 | + | |
483 | 517 | | |
484 | 518 | | |
485 | 519 | | |
486 | | - | |
| 520 | + | |
487 | 521 | | |
| 522 | + | |
488 | 523 | | |
489 | 524 | | |
490 | 525 | | |
| |||
0 commit comments