[GH-ISSUE #6693] [Feature]: Prompt caching: Claude subscription history marker, Opus 5.5 SDK, opt-in cache-friendly layout, low-cache warning and diagnostics #3747

Closed
opened 2026-10-21 06:06:47 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @SirAdoni on GitHub (Sep 26, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6693

Originally assigned to: @SpicyMarinara on GitHub.

Part of the contribution roadmap in #6624 (package F).

What problem does this solve?

On long chats with a lot of lore, prompt caching decides most of the cost and wait of each turn, and a few things make it miss more than it needs to:

  • Claude (Subscription): a turn that ends with an injection placed after the user's message gets no history cache marker, so the next turn writes the whole history again. One measured case was 61% cached on a 520k-token prompt.
  • Claude Opus 5.5 is listed for the Anthropic and Claude (Subscription) connections, but the pinned Agent SDK bundles a CLI that does not know the model yet.
  • Prompt order: the keyword lorebook scan and per-turn blocks (recalled memories, the fallback summary, awareness and social activity) sit early in the prompt, so one changing block invalidates the cached prefix after it.
  • Visibility: nothing tells the user before a send that will miss the cache, and nothing shows how much of a turn was cached or where two prompts start to differ.

Proposed solution

Build on the existing provider and prompt assembly code rather than restructure it:

  • History cache marker fix on the Claude subscription provider: one marker on the last completed assistant turn, also when the turn ends on an injection. Requests without markers are sent exactly as before.
  • Agent SDK 0.3.280, the first version whose bundled CLI knows Claude Opus 5.5.
  • Cache-friendly prompt layout for Claude (Subscription) and ChatGPT: the chat's enabled lore goes into one fixed block at the start, and per-turn blocks move next to the current turn. A chat can keep the keyword scan if it prefers.
  • ChatGPT cache session: full-lore requests send a per-chat session id and prompt_cache_key.
  • Warn before a low-cache send: a per-chat option in Advanced Parameters. When the predicted cached share is under a threshold (80% by default), the send is held and the user decides.
  • Diagnostics: MARINARA_CACHE_DIAGNOSTICS logs hashes, lengths and reported cache usage (never prompt text); Peek Prompt can show the prompt as the next turn will send it; the token usage line adds "N% cached".

How it's switched: the layout (cacheFriendlyPromptLayout) and the per-chat warning (cacheSendGuard) are off by default, and the diagnostics need their environment variable. With all three off, prompts are byte-identical to staging, checked by a regression that captures the request at the SDK. The marker fix and the SDK bump are bug fixes with no switch.

  • Areas touched (for spotting overlap):
    • packages/server/src/services/llm/providers: Claude subscription (and its jsonl-entries), OpenAI, ChatGPT and its cache helper, base provider, cache diagnostics
    • packages/server/src/services/generation: new prompt-cache-layout.ts and cache-send-guard.ts, memory recall context
    • packages/server/src/services/prompt: assembler, marker expander, merger
    • packages/server/src/routes: generate (and dry run), chats (Peek Prompt preview)
    • packages/shared/src/schemas: chat metadata, feature switches, GM verb table (two engine-owned metadata prefixes)
    • Client: chat message usage line, Peek Prompt modal, chat settings Advanced Parameters, Features settings, use-generate, en.json
    • packages/server/package.json, pnpm-workspace.yaml, lockfile; docs under docs/configuration and docs/prompts
  • Size: about 47 files, about 3,500 added lines, including regressions.
  • Status: implemented and tested in a fork, on Windows and on Linux (WSL, the CI-equivalent run).

Alternatives considered

  • Turning the layout on by default: it changes what the model sees and in which order, so it stays opt-in.
  • Fixing the cache per provider inside each provider only: the per-turn block order is decided in assembly, so a provider-only fix could not keep the prefix stable.
  • The Game Mode parts of the layout (GM runtime blocks, World Maps, the game lore split) are left out of this package. They are described in the Game Mode long campaigns issue listed in #6624.

Additional context

  • Implementation depends on #6647 (foundations); PRs will follow once it merges, one package at a time.
  • If you're working in any of these areas, please comment so we can coordinate before a PR.

Screenshots

Taken from the fork build with invented demo data (characters, lorebooks and a campaign made up for these shots). Model replies and token counts come from a local test stub, so no real model was called.

Warn before a low-cache send: the send is held, and the dialog shows the predicted cached share and where the prompt first changed.

Warn before a low-cache send

Peek Prompt showing the prompt as the next turn will send it, with the chat's enabled lore in one block at the start.

Peek Prompt showing the prompt as the next turn will send it, with the chat's enabled lore in one block at the start

The token usage line with the cached share, and its breakdown (cache read, cache write, hit ratio).

The token usage line with the cached share, and its breakdown (cache read, cache write, hit ratio)

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @SirAdoni on GitHub (Sep 26, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6693 Originally assigned to: @SpicyMarinara on GitHub. <!-- roadmap issue F, draft v1.0 --> Part of the contribution roadmap in #6624 (package F). ## What problem does this solve? On long chats with a lot of lore, prompt caching decides most of the cost and wait of each turn, and a few things make it miss more than it needs to: - **Claude (Subscription):** a turn that ends with an injection placed after the user's message gets no history cache marker, so the next turn writes the whole history again. One measured case was 61% cached on a 520k-token prompt. - **Claude Opus 5.5** is listed for the Anthropic and Claude (Subscription) connections, but the pinned Agent SDK bundles a CLI that does not know the model yet. - **Prompt order:** the keyword lorebook scan and per-turn blocks (recalled memories, the fallback summary, awareness and social activity) sit early in the prompt, so one changing block invalidates the cached prefix after it. - **Visibility:** nothing tells the user before a send that will miss the cache, and nothing shows how much of a turn was cached or where two prompts start to differ. ## Proposed solution Build on the existing provider and prompt assembly code rather than restructure it: - **History cache marker fix** on the Claude subscription provider: one marker on the last completed assistant turn, also when the turn ends on an injection. Requests without markers are sent exactly as before. - **Agent SDK 0.3.280**, the first version whose bundled CLI knows Claude Opus 5.5. - **Cache-friendly prompt layout** for Claude (Subscription) and ChatGPT: the chat's enabled lore goes into one fixed block at the start, and per-turn blocks move next to the current turn. A chat can keep the keyword scan if it prefers. - **ChatGPT cache session:** full-lore requests send a per-chat session id and `prompt_cache_key`. - **Warn before a low-cache send:** a per-chat option in Advanced Parameters. When the predicted cached share is under a threshold (80% by default), the send is held and the user decides. - **Diagnostics:** `MARINARA_CACHE_DIAGNOSTICS` logs hashes, lengths and reported cache usage (never prompt text); Peek Prompt can show the prompt as the next turn will send it; the token usage line adds "N% cached". **How it's switched:** the layout (`cacheFriendlyPromptLayout`) and the per-chat warning (`cacheSendGuard`) are off by default, and the diagnostics need their environment variable. With all three off, prompts are byte-identical to `staging`, checked by a regression that captures the request at the SDK. The marker fix and the SDK bump are bug fixes with no switch. - **Areas touched (for spotting overlap):** - `packages/server/src/services/llm/providers`: Claude subscription (and its `jsonl-entries`), OpenAI, ChatGPT and its cache helper, base provider, cache diagnostics - `packages/server/src/services/generation`: new `prompt-cache-layout.ts` and `cache-send-guard.ts`, memory recall context - `packages/server/src/services/prompt`: assembler, marker expander, merger - `packages/server/src/routes`: generate (and dry run), chats (Peek Prompt preview) - `packages/shared/src/schemas`: chat metadata, feature switches, GM verb table (two engine-owned metadata prefixes) - Client: chat message usage line, Peek Prompt modal, chat settings Advanced Parameters, Features settings, `use-generate`, `en.json` - `packages/server/package.json`, `pnpm-workspace.yaml`, lockfile; docs under `docs/configuration` and `docs/prompts` - **Size:** about 47 files, about 3,500 added lines, including regressions. - **Status:** implemented and tested in a fork, on Windows and on Linux (WSL, the CI-equivalent run). ## Alternatives considered - **Turning the layout on by default:** it changes what the model sees and in which order, so it stays opt-in. - **Fixing the cache per provider inside each provider only:** the per-turn block order is decided in assembly, so a provider-only fix could not keep the prefix stable. - The Game Mode parts of the layout (GM runtime blocks, World Maps, the game lore split) are left out of this package. They are described in the Game Mode long campaigns issue listed in #6624. ## Additional context - Implementation depends on #6647 (foundations); PRs will follow once it merges, one package at a time. - If you're working in any of these areas, please comment so we can coordinate before a PR. ## Screenshots Taken from the fork build with invented demo data (characters, lorebooks and a campaign made up for these shots). Model replies and token counts come from a local test stub, so no real model was called. **Warn before a low-cache send: the send is held, and the dialog shows the predicted cached share and where the prompt first changed.** ![Warn before a low-cache send](https://raw.githubusercontent.com/SirAdoni/Marinara-Engine-contrib/e98c448478676a9b4a6fad4bed1b6c29c046e9a4/roadmap/F-1-low-cache-warning.png) **Peek Prompt showing the prompt as the next turn will send it, with the chat's enabled lore in one block at the start.** ![Peek Prompt showing the prompt as the next turn will send it, with the chat's enabled lore in one block at the start](https://raw.githubusercontent.com/SirAdoni/Marinara-Engine-contrib/e98c448478676a9b4a6fad4bed1b6c29c046e9a4/roadmap/F-2-peek-next-turn-preview.png) **The token usage line with the cached share, and its breakdown (cache read, cache write, hit ratio).** ![The token usage line with the cached share, and its breakdown (cache read, cache write, hit ratio)](https://raw.githubusercontent.com/SirAdoni/Marinara-Engine-contrib/e98c448478676a9b4a6fad4bed1b6c29c046e9a4/roadmap/F-3-usage-line-cache-share.png) ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 06:06:47 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 26, 2026):

Awaiting PR.

<!-- gh-comment-id:5848653895 --> @SpicyMarinara commented on GitHub (Sep 26, 2026): Awaiting PR.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#3747
No description provided.