Skip to content

Studio GGUF tool turns: overlap each round's two prompt counts and reuse the reservation's native count - #18931

Merged
danielhanchen merged 6 commits into
mainfrom
perf/r38-tool-round-count-reuse
Nov 23, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
perf/r38-tool-round-count-reuse

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

A GGUF tool turn with admission on counts its whole history twice at the top of every round, one count straight after the other. The admission re-cost asks llama-server's native /v1/chat/completions/input_tokens, then the preflight fit renders the same conversation with /apply-template and counts it with /tokenize. On a 10 round turn that is 12 native counts (the reservation plus 11 re-costs) and 21 render counts, all of the full prompt. Two of these are pure waits:

  • The re-cost and the fit's first count price the same conversation and nothing runs between them that changes it, yet the second only starts when the first returns. llama-server serves both endpoints on its HTTP threads, so they can run at the same time.
  • The reservation count and round zero's re-cost send the identical native body a few milliseconds apart. The count memo from Studio: reuse an identical llama-server prompt count within a tool round #16756 skips prefer_native calls, so round zero pays for the whole native count again before the first token.

Change

  • generate_chat_completion_with_tools: when a round will run the preflight fit and has a re-cost to overlap with, the fit's first count starts in a worker thread just before the re-cost, with the same arguments the fit passes (the fit's count is now one helper, _fit_count_tokens, which both call; the round's tool list and reasoning kwargs are likewise one helper each). The thread only fills the existing count memo. The loop joins it right before the fit, which then makes its own call and reads the stored number back. If the prefetch fails or priced something different, the fit counts exactly as before. The re-cost, the iteration cap, the cancel check and every event keep their order. A cancel during the re-cost still returns at once without waiting for the thread.
  • count_chat_tokens(prefer_native = True): a successful native count is kept in its own two entry memo keyed on the exact request body plus the llama-server process (URL, model, process object and pid, date), the same scope as the render memo. A respawn, a model switch or different messages miss. Only native answers are stored: a failed or refused native call still falls back to the render path every time, so endpoint failover is unchanged. should_abort is still checked before a hit. The native memo is separate so per-round re-costs (which never repeat) do not evict render counts.

Results

Colab CPU high-RAM VM (8 vCPU, load 0.8), Studio's prebuilt llama.cpp b11643 on CPU, Qwen3-0.6B Q4_K_M. One llama-server is shared by every arm. Each arm is its own process importing its own tree (merge base c7b1865, the same tree again as an A/A arm, and this branch), interleaved in 6 rotated blocks with 2 measured turns each (12 per arm per size). Each turn is a real 10 round tool loop through _reserve_counted_gguf_chat, _openai_llama_admission_recost (wired like _gguf_recost) and generate_chat_completion_with_tools with context_overflow = "truncate_oldest". Only generation is faked, so the numbers are counting time. Qwen3-0.6B tops out at a 40960 token window, so the largest size is 30k tokens.

Time from the reservation count to the first generation request (median, min to max):

prompt base base A/A head
15k tokens 172.7 ms (168.8 to 180.6) 170.3 ms (166.8 to 188.1) 124.9 ms (122.3 to 131.9)
30k tokens 295.6 ms (289.4 to 301.0) 294.3 ms (290.9 to 298.9) 211.5 ms (209.5 to 225.1)

Top of each later round, re-cost start to the fit's first count done (median of rounds 1 to 10 per turn, then median over turns):

prompt base base A/A head
15k tokens 132.2 ms 133.6 ms 77.2 ms
30k tokens 214.4 ms 213.7 ms 122.7 ms

Whole 10 round turn (counting only): 3748 / 3716 / 2768 ms at 30k, 2295 / 2293 / 1730 ms at 15k. On the same server a native count alone takes 81 ms at 30k, a render count 92 ms, the two back to back 172 ms and the two at once 95 ms.

Server calls per turn: base 12 native + 21 render, head 11 native + 21 render (the fit never renders a prompt the prefetch already rendered). With the same inputs, the generation request bodies, the yielded events and every admission want are identical between base and head, on that llama-server and on a fake server with fixed counts at 30k tokens.

Tests

tests/test_tool_round_count_reuse.py (new, 15 tests): the prefetch fills the memo and the fit renders nothing twice; a turn with the prefetch disabled sends the same requests and events; a failed prefetch falls back to the fit's own count; no prefetch without a re-cost or a fit; a repeated native count is served from memory; a hit still honours should_abort; a restart, respawn, model change, different messages or tools miss; a failed native count is asked again; a server without the native endpoint keeps failing over; native counts never evict render counts. 5 of them fail on the merge base, as expected.

The 162 existing test files that touch count_chat_tokens, _gguf_recost, _fit_context, admission or the tool loop were run on base and head and the failing test IDs compared. One test failed only on head: test_exact_count_endpoint_failover_preserves_short_chat_concurrency[legacy] asserted three native calls for three identical chats while the native endpoint works. Those chats now share the first answer, so that case accepts one to three calls (they race); every failure case still asserts three, which is the failover guarantee. All other failures are the same IDs on both sides (host environment, unrelated to counting). The new file, test_llama_count_memo.py and test_gguf_admission_token_count.py pass three runs in a row.

…ation's native count

Each GGUF tool round counts its prompt twice back to back: the admission re-cost
(native /input_tokens) and the preflight fit (/apply-template + /tokenize). The fit's
first count now starts in a worker thread with the fit's own arguments while the
re-cost runs; it only fills the count memo, which the fit reads back after a join.

The reservation and round zero's re-cost send the same native body milliseconds
apart. Successful native counts are now kept in a separate two-entry memo keyed on
the request body and the llama-server process, so round zero reuses the
reservation's answer. Failed native counts are never stored, so endpoint failover
still retries.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T03:49:11.707334Z d4d996a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 1c550ea094

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen marked this pull request as ready for review November 23, 2026 03:47
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: d4d996a524

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 4770491 into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the perf/r38-tool-round-count-reuse branch November 23, 2026 05:27
Lyxot added a commit that referenced this pull request Nov 24, 2026
…19159)

Every GGUF chat turn counts the same prompt twice: once for the
admission reservation and once for the context fit. Both return the same
number. #18931 caches the native count, but the fit never reads that
cache, so it still renders and tokenizes the whole prompt every turn.

The fit now reuses the cached native count when it would render the
exact same request. Anything different is still counted fresh, and a
failed native count is never cached.

RTX 5000 Ada, Qwen3-4B-Instruct-2507 Q4_K_M:

| | main | this branch |
|---|---|---|
| llama-server counts per turn | 2 | 1 |
| Time to first token, 7.4k token chat | 176 ms | 132 ms |
| Time to first token, 21.6k token chat | 322 ms | 220 ms |
| Time to first token, 48k token chat | 539 ms | 387 ms |

---------

Co-authored-by: Lyxot <[email protected]>
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