Reuse cached chat transcript prefixes#370
Open
aleroot wants to merge 4 commits into
Open
Conversation
aleroot
force-pushed
the
auto-chat-prefix-cache-reuse
branch
3 times, most recently
from
June 25, 2026 17:57
f4c9879 to
c180cb5
Compare
Contributor
Author
|
I have reworked completely from my previous #367 since that was too simplistic |
aleroot
force-pushed
the
auto-chat-prefix-cache-reuse
branch
3 times, most recently
from
June 27, 2026 18:45
b2a9f7b to
4553688
Compare
ChatSession now keeps an exact token ledger next to the KV cache and reuses a cached prefix only when the re-rendered transcript starts with those tokens and all KV offsets match. Otherwise it falls back to full prefill or throws on prompt configuration changes. Reuse is disabled for stateful or unsafe paths, including logit processors, speculative decoding, bounded/non-reusable caches, and multimodal or masked inputs. Tests cover suffix-only prefill, fallback behavior, prompt/tool/context mutations, clear behavior, and cache capability reporting.
aleroot
force-pushed
the
auto-chat-prefix-cache-reuse
branch
from
June 29, 2026 19:00
4553688 to
d6e020c
Compare
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Changes ChatSession to reuse the KV cache for chat prefixes automatically.
Instead of repeatedly prefilling prompt-rendered chat-prefix tokens on later turns, the session keeps an exact token ledger for the prompt and generated assistant output. On the next turn it re-renders the full transcript, checks that the cached tokens are an exact prefix and that KV cache offsets match, then prefills only the new suffix. If anything does not line up, it falls back to full prefill or throws when the prompt configuration changed.
Reuse is intentionally disabled for unsafe paths like logit processors, speculative decoding, bounded/non-reusable caches, and multimodal or masked inputs.
This should improve performance on coding agents using the library.
Notes
This is the first step to make a Codex-like app based on the library. At the moment prefix reuse is enabled just for
KVCacheSimplewith no logit processors, no speculative decoding, and no dynamic KV quantization. The safest subset. Each opt-out is a known performance floor with a clear unlock path:RotatingKVCache— The transcript prefix eventually gets overwritten onceoffset > maxSize. A future two-tier cache (static prefix + rotating suffix) would enable reuse for the dominant production config (anyone settingmaxKVSize).Logit processors —
PenaltyProcessor'sTokenRingstate is stale when skipping the prefix. On a prefix-reused turn, callingprocessor.prompt()with the prefix tokens before generation would sync the rings. Small mechanical change once plumbing is ready.Speculative decoding —
trimPromptCacheafter rejected draft tokens can desync KV offsets from the ledger. The speculative iterator already tracks accepted/rejected per round — wiringGenerationTrace.committedTokensto the net effect would unlock this.The
supportsStaticPrefixReuse/allowsPrefixReusegates are single-point switches designed for independent enablement.Important
Once #360 lands,
PromptLedger.Messageshould carry the structured tool metadata, not justroleandcontent, so cached replay preserves correlated tool calls exactly.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes