fix(streaming): record Responses terminal usage instead of zero tokens - #165
Conversation
ProxyResponsesStream hardcoded the terminal message_delta usage to 0/0 and processResponsesSSELine never read response.completed, so wire_format responses models (muse-spark) logged and stored 0 input/output tokens on every streaming request. Capture flat or nested response.usage from the terminal event into the Anthropic message_delta; missing usage keeps 0. Co-Authored-By: Claude Code <noreply@anthropic.com>
48bb601 to
6e1ef5b
Compare
Code Review Roast 🔥Verdict: 1 Issue Found | Recommendation: Add CHANGELOG entry, then merge Overview
Issue Details (click to expand)
🏆 Best part: The author actually wrote tests for the bug they fixed — three of them covering flat nested, and missing usage on 💀 Worst part: The CHANGELOG got ghosted. You fixed a user-facing bug that changes what users see in their logs and dashboard, but left the changelog entry field blank. That's the kind of omission that turns a 5-minute blame search into a 3am rabbit hole. 📊 Overall: Like a surgeon's precision on a papercut — the fix is clean, tested, and correct, routing both flat and nested Files Reviewed (3 files)
Reviewed by free · Input: 236.5K · Output: 35.7K · Cached: 820.4K Review guidance: REVIEW.md from base branch |
Bug
Streaming spark requests logged and stored
0/0tokens. The opencode dashboard showed real usage for the same calls.Why
The Responses streaming path dropped upstream usage.
ProxyResponsesStream(internal/transformer/stream.go) built the terminal Anthropicmessage_deltawith usage hardcoded to zero, andprocessResponsesSSELinenever handledresponse.completed, which is where upstream puts usage.extractUsageFromSSE(internal/handlers/messages.go) only readsmessage_deltachunks, so the log and history both got the hardcoded zeros. Chat streaming and non-streaming Responses read usage fine. This path was the only one that did not.Fix
Read the terminal event and pass its usage through:
pkg/types/zen.go:ResponsesChunknow decodes the nestedresponse.usageshape alongside the flat one.internal/transformer/stream.go: stash usage fromresponse.completed(flat first, nested second, last one wins) and emit it in the terminalmessage_deltathroughresponsesUsageToAnthropic. A stream with no usage event still records 0.handleResponsesStreaming,StreamProxy.ProxyStream) shareProxyResponsesStream, so one change fixes both.Validation
go test ./internal/transformer/ -run TestProxyResponsesStream: 8 passed (5 existing, 3 new covering flat, nested, and missing usage).go test ./...green;gofmt,go vetclean./api/history. Other models unaffected.