Skip to content

Count Chat Completions input tokens using the generation prompt - #136

Open
gplume wants to merge 2 commits into
rizagp:mainfrom
gplume:codex/chat-input-tokens
Open

gplume wants to merge 2 commits into
rizagp:mainfrom
gplume:codex/chat-input-tokens

Conversation

@gplume

@gplume gplume commented Dec 4, 2026

Copy link
Copy Markdown

What and why

Closes #108.

Add POST /v1/chat/completions/input_tokens. Send a Chat Completions body to get {"input_tokens": N} for the loaded model. Counting uses the same file preparation, template options, tokenizer and media expansion as generation. The generation path checks the encoder output against the planned number of rows before caching it. The Anthropic counter uses the same path after request translation.

Ordinary prompts can be counted past the context limit. Counting returns an explicit error when a speech transcript is needed, an OCR workflow has no single admissible page, or Chat would switch to another model.

Where it applies

Server-side prompt preparation for Chat Completions, including API-only mode. The change is independent of GPU kind, weight type, slot count and drafting mode.

Accuracy

Tested on Linux with one Arc Pro B70, driver 26.35.39758.21, Qwen3.8-27B-Q5_K_M and its Qwen3-VL projector. Both HTTP modes returned the same counts as usage.prompt_tokens for a plain message (58), declared tools (364), a tool call and result (524), thinking disabled (13), low reasoning effort (45), one image (313) and two images (580). A text prompt of 500,052 tokens was counted exactly at context 2,048.

The generation statistics, saved session contents and reuse of a different live KV prefix were unchanged across counting. Both count endpoints rejected an inactive model with HTTP 409. A transcript-dependent request returned HTTP 400. With OCR mode selected by alias, one fitting PDF page matched generation (298). The counter rejected the multi-page request and an oversized single page.

Upstream 06cb3be1 and this branch returned the same ordinary greedy answer, Blue sky, with 25 prompt tokens. The CPU chat golden tests and API compatibility tests passed. All four runz --dump-logits outputs had SHA-256 f4f2edf8d8768f498112f33c34f04980660f18ff27ddf1d0e840807da1b2711b.

Speed

Same command in A B B A order (A = 06cb3be1, B = a0c5c925):

runz "$MODEL" 'Daytime sky color:' --chunk 128 --ctx 512 --n 4 --dump-logits "$OUT"
Run A B B A
Time to first token, ms 55.0 49.9 49.9 49.9
Generation, tokens/s 24.19 23.89 24.33 24.30

Start lines included GPU: Intel(R) Arc(TM) Pro B70 Graphics, driver 26.35.39758.21, 256 EUs, 2800 MHz, 30.3 GiB of its own, XMX yes, 16-lane sub-groups and inferzero: H8 rotates the activations from f32. The HTTP checks used --ctx 2048 --chunk 128 and ran with and without --no-webui.

Not tested

Windows PowerShell 5.1/7, native audio projector inference, OCR-specific weights, other GPU families, multi-GPU, multiple slots and speculative decoding.

@gplume
gplume force-pushed the codex/chat-input-tokens branch from a0c5c92 to d8255c0 Compare December 4, 2026 19:15

This branch has not been deployed

No deployments
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.

Expose an exact input token count for Chat Completions

1 participant