Skip to content

server: a streamed Anthropic head carries the prompt's token count - #672

Merged
ashhart merged 1 commit into
ashhart:mainfrom
kky42:fix-streaming-usage-message-start
Oct 12, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
kky42:fix-streaming-usage-message-start

Conversation

@kky42

@kky42 kky42 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Summary

A streamed Anthropic /v1/messages reply opened with message_start.usage all zeros and only sent the real counts in the final message_delta. Spec-following clients read prompt token counts from message_start — Claude Code's context-window indicator showed ~0% used on every streaming request. Verified against a live server: message_start carried {input_tokens: 0, cache_read_input_tokens: 0} while the closing message_delta carried the correct total.

The fix threads the prepared prompt's length to the stream's head: Out.open now carries prompt_len, Run.stream passes prepared.prompt_len (known once chat.prepare/compact.prepare finishes, before the head is written), and a context-refusal head passes 0. Wire.open puts the count into the existing usage object before message_start is sent: input_tokens the full prompt, output_tokens 0, no cache fields. Seven Out implementations are updated (the test sinks ignore the argument).

Two notes on intent:

  • Compaction-correctness. The head's count comes from the same prepared.prompt_len the final usage is built from, so with --compact-at set the head names the compacted prompt, not the raw request. Rendering the request again for the head would have disagreed with message_delta exactly in long sessions that compact.
  • The final message_delta split is left alone. input = full − cached, cache_read = cached there reflects the engine's kept prompt states (a repeated request reports most of its prompt as cache_read, a fresh prompt reports 0) and is the same conversion the non-streaming reply uses. message_start intentionally 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_start now carry the prompt count (e.g. plain-stream: 24 = its delta's 21 + 3).
  • zig tests/server/zig_test.sh: 104/104 passed.
  • fake_serve curl, streaming a system-prompt request: before, message_start {"input_tokens": 0, ...} and message_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).
  • Compaction agreement case: server with a 1024 window and --compact-at 0.05, ~750-token prompt → message_start input_tokens: 733, message_delta input_tokens: 730, cache_read_input_tokens: 3 (730+3=733).
  • /v1/messages/count_tokens re-checked after the change.

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.
@ashhart
ashhart merged commit 540a22e into ashhart:main Oct 12, 2026
2 checks passed
@ashhart

ashhart commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thank you, @kky42. A client that reads the prompt's size from message_start saw an empty context on every streamed request, and this fixes it at the right place. Your commit ad1abffbb landed in 1.0.8 under your name, so GitHub shows this PR as merged.

We served Nemotron on an M5 Max and on a GB10 and streamed /v1/messages with and without your change:

request count_tokens message_start before message_start now message_delta input + cache_read
6,619-token prompt, fresh 6,619 0 6,619 6,619 + 0
the same again 6,619 0 6,619 19 + 6,600 (M5 Max), 475 + 6,144 (GB10)
354-token prompt 354 0 354 354 + 0 (GB10), 23 + 331 (M5 Max, see below)
319 tokens with a tool 319 0 319 319 + 0 (GB10), 24 + 295 repeated (M5 Max)

The message_delta split is the same as before your change in every case on the GB10, and in the long case on the Mac. The short cases' split differs on the Mac only because #669, which landed beside yours, now keeps prompt states for short prompts there.

Your compaction point holds up too. With --context 8192 --compact-at 0.5, count_tokens counted the raw request at 7,172 tokens, the engine ran the compacted prompt (7,294 tokens on the Mac, 7,354 on the GB10), and message_start named exactly that, matching the delta's sum.

The goldens agree: test-golden gives 309/319 with 0 unexpected differences, and with Wire.open ignoring the count, five streamed /v1/messages goldens show unexpected differences (the sixth, tool-use-stream, is already on the known list).

Two small things on our side, in a commit after yours. The comment in Wire.open is now one line and the VTable doc comment fits 120 columns, per our lean rules. Also, your commit message still describes the earlier design (rendering the prompt again the way count_tokens does), while the code uses prepared.prompt_len as your description says. We left your message as you wrote it.

BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
@ashhart ashhart mentioned this pull request Oct 14, 2026
outcastofmusic added a commit to outcastofmusic/TensorFold that referenced this pull request Oct 16, 2026
…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]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.

2 participants