Skip to content

cache: preserve root system prompt checkpoint across turns and eviction - #65

Closed
code-martin wants to merge 1 commit into
Niko1221:mainfrom
code-martin:feature/root-checkpoint-retention
Closed

code-martin wants to merge 1 commit into
Niko1221:mainfrom
code-martin:feature/root-checkpoint-retention

Conversation

@code-martin

Copy link
Copy Markdown
Contributor

Summary

This PR addresses root checkpoint retention for --serve prompt caching, keeping the root system prompt cached across branching chats and new conversations without compromising v0.1.13 prefill throughput:

  1. Root & Last Turn Checkpoints Only:

    • Rather than checkpointing every turn (which triggers repeated borrow/refill cycles and large memory copies), the prompt reading loop identifies at most two turn boundaries:
      • urn_first: the first <|im_start|> past token 0 (the root boundary after the system prompt).
      • urn_last: the final <|im_start|> before the assistant turn.
    • Long conversation histories remain batched in full chunks (up to 8192 tokens), preserving v0.1.13 speedup.
  2. Root Preservation During Eviction:

    • In checkpoint_at(), when checks.size() > o.prompt_cache, if checks.size() > 2 && checks[0].ids.size() <= checks[1].ids.size(), eviction drops checks[1] (the oldest intermediate turn) instead of checks[0].
    • The root system prompt checkpoint remains resident in checks[0]. Subsequent requests sharing the same system prompt match checks[0] and resume immediately (
      esume == checks[0].ids.size()) without prefilling the system prompt again.

Capture the root turn boundary (system prompt) during the initial prompt read alongside the last turn boundary, avoiding checkpoints at every intermediate turn to keep v0.1.13 prefill fast. During prompt cache eviction, preserve checks[0] so branching chats and new conversations sharing the system prompt can reuse it immediately without re-reading.
@maedoc

maedoc commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

you might wanna jump in over here #57 where the caching action is

Niko1221 added a commit that referenced this pull request Sep 28, 2026
…n top of #62); setup requires it

A prompt read from token 0 is also checkpointed at its first turn boundary when that is 2,048 tokens or
more (--prompt-cache-root N, 0 = off). It becomes the chain's root, which #62's retention policy pins,
so a new chat of the same client reads only what follows the system prompt and tools. Without it, #62's
root existed only for system prompts past --prompt-cache-every (16K).

Co-Authored-By: code-martin <code-martin@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0154bwiad8gY7wRehCXy27pN
@Niko1221

Copy link
Copy Markdown
Owner

Thanks @code-martin - saving a checkpoint at the end of the system prompt was exactly the piece #62 was missing. Since both changed the same eviction loop, I folded your idea into #62 (with credit in the commit), with a minimum of 2,048 tokens so short system prompts don't pay for the extra split (~0.3 s), and a switch (--prompt-cache-root). It's in v0.1.20, so I'm closing this one.

@Niko1221 Niko1221 closed this Sep 28, 2026
@code-martin
code-martin deleted the feature/root-checkpoint-retention branch September 28, 2026 20:19
13600k added a commit to 13600k/Strata-for-daniel that referenced this pull request Sep 28, 2026
Brings in engine 0.1.13-0.1.20 and upstream PRs Niko1221#42-Niko1221#44, Niko1221#54, Niko1221#59, Niko1221#62-Niko1221#65,
Niko1221#67, Niko1221#69 on top of the fork's experimental multi-GPU expert caches. Host
test suites pass (expert placement 5935 checks, conversation cache, setup
GPU selection, GPU telemetry, IQ pack, calibration); CUDA build and GPU
parity remain pending hardware validation.
MistVVK pushed a commit to MistVVK/XeStrata that referenced this pull request Oct 3, 2026
…221#65 on top of Niko1221#62); setup requires it

A prompt read from token 0 is also checkpointed at its first turn boundary when that is 2,048 tokens or
more (--prompt-cache-root N, 0 = off). It becomes the chain's root, which Niko1221#62's retention policy pins,
so a new chat of the same client reads only what follows the system prompt and tools. Without it, Niko1221#62's
root existed only for system prompts past --prompt-cache-every (16K).

Co-Authored-By: code-martin <code-martin@users.noreply.github.com>
Hugua700 pushed a commit to Hugua700/Strata that referenced this pull request Oct 6, 2026
…221#65 on top of Niko1221#62); setup requires it

A prompt read from token 0 is also checkpointed at its first turn boundary when that is 2,048 tokens or
more (--prompt-cache-root N, 0 = off). It becomes the chain's root, which Niko1221#62's retention policy pins,
so a new chat of the same client reads only what follows the system prompt and tools. Without it, Niko1221#62's
root existed only for system prompts past --prompt-cache-every (16K).

Co-Authored-By: code-martin <code-martin@users.noreply.github.com>
anon761 pushed a commit to anon761/Strata-upstream that referenced this pull request Oct 6, 2026
…221#65 on top of Niko1221#62); setup requires it

A prompt read from token 0 is also checkpointed at its first turn boundary when that is 2,048 tokens or
more (--prompt-cache-root N, 0 = off). It becomes the chain's root, which Niko1221#62's retention policy pins,
so a new chat of the same client reads only what follows the system prompt and tools. Without it, Niko1221#62's
root existed only for system prompts past --prompt-cache-every (16K).

Co-Authored-By: code-martin <code-martin@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0154bwiad8gY7wRehCXy27pN
konijiwa110 added a commit to konijiwa110/Strata that referenced this pull request Oct 7, 2026
…pt instead of reading it again

The system-prompt checkpoint (Niko1221#62, Niko1221#65) lives in the one K/V arena, so it is gone as soon as a request
with another start comes in, and after every restart. --prefix-cache-dir keeps it in a file: after
DONE, the root checkpoint's running state and the K/V up to it (draft layer included) are streamed
into DIR once per distinct system prompt; a later prompt that starts with one restores it and reads
only the rest. Files are capped by --prefix-cache-disk-mib (LRU), the most recent ones optionally
kept in RAM (--prefix-cache-ram-mib); without the RAM they stream through a 64 MB buffer. A file
from another engine build, model or --kv is deleted at start-up. One GPU without --batch.
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.

3 participants