Repository navigation
server: a streamed Anthropic head carries the prompt's token count - #672
Conversation
message_start.usage held zeros and the real counts only arrived in the final message_delta, so spec-following clients (Claude Code's context indicator) read a 0-token prompt. A streaming Messages request now renders the prompt once more the way count_tokens does and puts its token count in message_start: input_tokens the full prompt, output 0, no cache. message_delta keeps the engine's usage, the same conversion the whole reply already used.
|
Thank you, @kky42. A client that reads the prompt's size from We served Nemotron on an M5 Max and on a GB10 and streamed
The Your compaction point holds up too. With The goldens agree: Two small things on our side, in a commit after yours. The comment in |
…mpt's size message_start carried usage.input_tokens 0, so a client that reads the context size there showed an empty context. The CUDA server now counts the prompt through app.prepare, the path the reply runs, once the stream opens. The Mac server keeps 0: its count renders through the lane tokenizer, which the HTTP tests' fake app does not have. Upstream TensorFold 1.0.8 (ashhart#672) does the same on its server. Tests: tests/test_anthropic_api.py, the stream's first event against /v1/messages/count_tokens. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ashhart#672 landed above as eadb04a81 (the author's commit, rebased onto this candidate). This keeps the PR's own head commit in the history, with no change to the tree.
Summary
A streamed Anthropic
/v1/messagesreply opened withmessage_start.usageall zeros and only sent the real counts in the finalmessage_delta. Spec-following clients read prompt token counts frommessage_start— Claude Code's context-window indicator showed ~0% used on every streaming request. Verified against a live server:message_startcarried{input_tokens: 0, cache_read_input_tokens: 0}while the closingmessage_deltacarried the correct total.The fix threads the prepared prompt's length to the stream's head:
Out.opennow carriesprompt_len,Run.streampassesprepared.prompt_len(known oncechat.prepare/compact.preparefinishes, before the head is written), and a context-refusal head passes 0.Wire.openputs the count into the existing usage object beforemessage_startis sent:input_tokensthe full prompt,output_tokens0, no cache fields. SevenOutimplementations are updated (the test sinks ignore the argument).Two notes on intent:
prepared.prompt_lenthe final usage is built from, so with--compact-atset the head names the compacted prompt, not the raw request. Rendering the request again for the head would have disagreed withmessage_deltaexactly in long sessions that compact.message_deltasplit is left alone.input = full − cached, cache_read = cachedthere reflects the engine's kept prompt states (a repeated request reports most of its prompt ascache_read, a fresh prompt reports 0) and is the same conversion the non-streaming reply uses.message_startintentionally reports the whole prompt with cache fields 0; the split arrives with the delta.Validation
zig build test-golden: 309/319 equal to the frozen answers, 0 unexpected differences, 10 known — the same known set as before the change. The six affected streaming goldens'message_startnow carry the prompt count (e.g.plain-stream: 24 = its delta's 21 + 3).zig tests/server/zig_test.sh: 104/104 passed.fake_servecurl, streaming a system-prompt request: before,message_start{"input_tokens": 0, ...}andmessage_delta{"input_tokens": 107, "cache_read_input_tokens": 3}(107+3=110); after,message_start{"input_tokens": 110, "output_tokens": 0, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0}with the delta unchanged (110 = 107+3).--compact-at 0.05, ~750-token prompt →message_startinput_tokens: 733,message_deltainput_tokens: 730, cache_read_input_tokens: 3(730+3=733)./v1/messages/count_tokensre-checked after the change.