Skip to content

fix(proxy): read input_tokens from message_start in streaming usage - #252

Open
pcbeingused333 wants to merge 1 commit into
BlockRunAI:mainfrom
pcbeingused333:fix/stream-input-tokens
Open

pcbeingused333 wants to merge 1 commit into
BlockRunAI:mainfrom
pcbeingused333:fix/stream-input-tokens

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Streaming requests through the proxy record input_tokens = 0 in stats, the audit log and the estimated cost.

Cause. In the streaming path of src/proxy/server.ts, usage was parsed only from the top-level usage of each SSE event. Anthropic reports input_tokens in message_start under message.usage, and only output_tokens in message_delta, so inputTokens stayed 0. Stats, the audit log and the estimated cost (estimateCost, used when nothing was charged) all saw 0 input tokens. The non-streaming path was already correct, and the agent's own stream reader (src/agent/llm.ts, message_start case) already takes input_tokens from message.usage.

Fix. Extract the parsing into an exported parseStreamUsage() that reads both usage and message.usage, keeping the last non-zero value. Behaviour is otherwise unchanged.

Test. proxy streaming usage reads input_tokens from message_start in test/local.mjs: fails on main (function missing / input 0), passes with the fix.


This PR was prepared with the assistance of an AI coding agent (Claude); it has not been reviewed line by line by a human before submission.

The streaming path only looked at top-level usage, so Anthropic's
message_start (message.usage.input_tokens) was ignored and stats, audit
and cost estimates recorded input_tokens=0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant