Skip to content

prompt cache: long conversations survive 8-16 concurrent agents - #2699

Merged
ashhart merged 22 commits into
mainfrom
lead/prompt-cache-agents
Nov 10, 2026
Merged

ashhart merged 22 commits into
mainfrom
lead/prompt-cache-agents

Conversation

@ashhart

@ashhart ashhart commented Nov 9, 2026

Copy link
Copy Markdown
Owner

What this changes

When 8 to 16 agents with long conversations send their follow-ups at once, the prompt cache used to evict exactly the states those follow-ups needed. Every follow-up then prefilled its whole conversation again. On Qwen3.8-27B with DFlash2, the median first token for a follow-up reached 50 s at 8 agents and 546 s at 16 on an M3 Ultra, and 114 s at 16 on a GB10. Three things caused it:

  • every kept copy was an equal eviction candidate, so a conversation's only state could go before another's spare copy;
  • admitting a new request evicted states that queued follow-ups were about to resume;
  • the default budget was fixed (16 GiB on Metal, 8 GiB on CUDA) whatever the machine's memory.

Now:

  • Eviction order: redundant copies go first, and each conversation's newest state goes last.
  • Admission: a new request can't evict the exact state a queued request resumes. Bytes and slots promised to admitted passes are reserved, so concurrent keeps don't count the same free room twice. An eviction that couldn't make room is refused before anything is evicted.
  • Budget: the default is half the room left after streams on Metal and on unified-memory CUDA (GB10), with the old default as a floor. Discrete CUDA hosts keep 8 GiB of pinned host memory, and Flash Next's batch host keeps its 16 GiB ceiling. --prompt-cache-gib still overrides it.
  • Report line: the prompt cache line also counts trimmed marks.

Cache policy changes only which states are kept. Lookup and restore are unchanged, so no reply's tokens change.

Measured

A follow-up harness: N agents, each with its own ~17k-token conversation, send turn 1 together, then all follow-ups together. Temperature 0, thinking off, Qwen3.8-27B MLX 4-bit with DFlash2. Every reply in both turns matched main's token for token, at every N.

Machine N main: follow-up first token (median) this change follow-up tok/s, main → this
M3 Ultra (Metal) 8 50 s 1.4 s 15.4 → 88
M3 Ultra (Metal) 16 546 s 16.5 s 5.9 → 89
M3 Ultra at a 32 GiB budget (a 128 GB Mac's default) 16 546 s 16.2 s 5.9 → 89
GB10 (CUDA) 8 15-20 s 7.7 s 47 → 94
GB10 (CUDA) 16 90-114 s 28 s 32 → 101

These numbers are from the change's second round. The last rounds change only refusal and accounting corners, and the runs on this exact head are in progress. They'll be posted here before it lands.

Checks

  • Host gates: the conversation cache, cache progress, lane API, CUDA cache budget and cache fit steps pass.
  • Every new rule has a mutation control: removing it turns a named test red.
  • zig fmt --check, the lean check (no new problems), and source files at most 600 lines.

Not covered here

The remaining wait at 16 agents is the 8 stream slots a server opens by default, so half the agents queue. That's a separate design.

…the pre-check's room saturates

From the independent review of round four: with no outside promise, a freed or spare buffer may take the state for
less than a fresh buffer's bytes, so the pre-check no longer refuses it. README keeps main's text, with the prompt cache
row describing the new defaults.
@ashhart
ashhart force-pushed the lead/prompt-cache-agents branch from 2baf1b9 to c81e58a Compare November 10, 2026 09:00
@ashhart
ashhart merged commit 33ada77 into main Nov 10, 2026
21 of 36 checks passed
@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner Author

Device results on this PR's head (2baf1b9f), against main. Each agent sends one ~17K-token prompt, then a follow-up on the same conversation. "Follow-up first token" is the median across agents, and "replies" compares every agent's two reply hashes with main's.

Qwen3.8-27B, M3 Ultra (Metal), default cache budget

Agents Follow-up first token, main → this PR Follow-up tok/s (all agents) Replies
1 0.29 s → 0.30 s 52.5 → 51.2 equal
4 4.2 s → 0.73 s 25.7 → 83.1 equal
8 50.1 s → 1.42 s 15.4 → 79.5 equal
16 546 s → 15.9 s 5.9 → 88.5 equal

With a 16 GiB cache budget (a smaller Mac): 8 agents 50.1 s → 1.38 s, 16 agents 546 s → 47.2 s, replies equal.

Qwen3.8-27B, GB10 (CUDA)

Agents Follow-up first token, main → this PR Follow-up tok/s Replies
1 1.07 s → 1.11 s 48.7 → 48.3 equal
4 4.26 s → 4.30 s 86.5 → 86.8 equal
8 14.6 s → 7.7 s 47.7 → 93.9 equal
16 105 s → 25.8 s 33.6 → 100.8 equal

16 GiB budget: 16 agents 105 s → 36.3 s, replies equal.

Qwen3.8-Flash-Next, GB10 (CUDA): level with main at 1-16 agents (16 agents: 32.3 → 32.3 tok/s follow-up), replies equal. Its cache on this box holds about one conversation, so follow-ups still miss there; that's separate work.

First-turn speed is unchanged everywhere (Metal 16 agents 6.0 → 8.8 tok/s, CUDA 31.4 → 31.3).

ashhart added a commit that referenced this pull request Nov 10, 2026
Figures corrected to the PRs' measured ones (#2699 head runs, #2509,
#2633, #2436, #2644, #2593, #2430, #2568), scope fixed (#2253, #2287,
#2511, #2569, #2337, #2627, #2351, #2626), unsourced figures dropped
(#2181, #2606, #2257's pricing clauses), the shipped #2176, #2565,
#2548 and gfx12 silu entries and the first #2625 copy removed, and
the #2568 and #2352 entries unwrapped to one line each.
@ashhart
ashhart deleted the lead/prompt-cache-agents branch November 10, 2026 16:34
volfco pushed a commit to volfco/TensorFold that referenced this pull request Nov 11, 2026
…the dashboard (ashhart#2655)

The branch's six commits squashed and rebased onto main after ashhart#2699. The `seen` updates move with the functions
ashhart#2699 moved: a resume in `prompt_cache_lifetime.zig` (`resumedWithReason`), a re-keep and a new entry in
`prompt_cache_keep.zig` (`keepBorn`); the shared-cut entry in `prompt_cache.zig` keeps its own. `used` stays the
store's clock.
ashhart pushed a commit to volfco/TensorFold that referenced this pull request Nov 11, 2026
…the dashboard (ashhart#2655)

The branch's six commits squashed and rebased onto main after ashhart#2699. The `seen` updates move with the functions
ashhart#2699 moved: a resume in `prompt_cache_lifetime.zig` (`resumedWithReason`), a re-keep and a new entry in
`prompt_cache_keep.zig` (`keepBorn`); the shared-cut entry in `prompt_cache.zig` keeps its own. `used` stays the
store's clock.
ashhart pushed a commit to volfco/TensorFold that referenced this pull request Nov 11, 2026
…the dashboard (ashhart#2655)

The branch's six commits squashed and rebased onto main after ashhart#2699. The `seen` updates move with the functions
ashhart#2699 moved: a resume in `prompt_cache_lifetime.zig` (`resumedWithReason`), a re-keep and a new entry in
`prompt_cache_keep.zig` (`keepBorn`); the shared-cut entry in `prompt_cache.zig` keeps its own. `used` stays the
store's clock.
ashhart pushed a commit that referenced this pull request Nov 11, 2026
…the dashboard (#2655)

The branch's six commits squashed and rebased onto main after #2699. The `seen` updates move with the functions
#2699 moved: a resume in `prompt_cache_lifetime.zig` (`resumedWithReason`), a re-keep and a new entry in
`prompt_cache_keep.zig` (`keepBorn`); the shared-cut entry in `prompt_cache.zig` keeps its own. `used` stays the
store's clock.
ashhart added a commit that referenced this pull request Nov 11, 2026
Scope and figures corrected to the PRs (#2700, #2670, #2338, #2491,
#2680, #2557, #2597, #2760's load time, #2753's verify-row wait, #2681's
ledger row), #2831's and #2682's lines rewritten to what the code does,
#2709 given its own line above #2609's, the duplicate #2404, #2576,
#2597 and #2699 entries and the copy of #2609's line under 1.2.6
removed, entries spaced one blank line apart, and the released sections
restored to the v1.2.6 tag's text (#1651's 1.2.2 line and six blank
lines in 1.2.5 and older).
ashhart added a commit to javierbits/TensorFold that referenced this pull request Nov 12, 2026
Since ashhart#2699 the victim order keeps each conversation's newest state
before redundant copies. A ladder checkpoint was exempt from the
superseded pick, so once the plain redundant copies were gone the
oldest state went next, which could be another conversation's newest.
A superseded checkpoint now goes after every other redundant copy and
before any newest state.

Under a reservation guard, fitting took the marks in position order,
so the low ladder positions could take the room the rewind state
needed. Ladder positions now fit last, after the endpoint and every
other mark, in the room other passes' promises leave.

Co-authored-by: javierbits <[email protected]>
ashhart added a commit to javierbits/TensorFold that referenced this pull request Nov 13, 2026
Since ashhart#2699 the victim order keeps each conversation's newest state
before redundant copies. A ladder checkpoint was exempt from the
superseded pick, so once the plain redundant copies were gone the
oldest state went next, which could be another conversation's newest.
A superseded checkpoint now goes after every other redundant copy and
before any newest state.

Under a reservation guard, fitting took the marks in position order,
so the low ladder positions could take the room the rewind state
needed. Ladder positions now fit last, after the endpoint and every
other mark, in the room other passes' promises leave.

Co-authored-by: javierbits <[email protected]>
ashhart added a commit that referenced this pull request Nov 13, 2026
Since #2699 the victim order keeps each conversation's newest state
before redundant copies. A ladder checkpoint was exempt from the
superseded pick, so once the plain redundant copies were gone the
oldest state went next, which could be another conversation's newest.
A superseded checkpoint now goes after every other redundant copy and
before any newest state.

Under a reservation guard, fitting took the marks in position order,
so the low ladder positions could take the room the rewind state
needed. Ladder positions now fit last, after the endpoint and every
other mark, in the room other passes' promises leave.

Co-authored-by: javierbits <[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