[GH-ISSUE #7865] [Issue]: Claude (Subscription) re-caches the whole chat history every turn #4327

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

Originally created by @stonebystone on GitHub (Oct 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7865

Originally assigned to: @SpicyMarinara on GitHub.

Summary

On the Claude (Subscription) connection, prompt caching only ever reuses the system prompt. Every turn writes the full chat history to the cache again and never reads it back. The cost per turn is the whole history in the prompt, which can reach Claude's context limit (up to a million tokens) on a long chat, or less where a chat summary replaces older messages. It burns subscription usage and adds latency.

Expected behavior

Each turn reads the previous turn's history from the cache and writes only the new messages and the per-turn blocks.

Actual behavior

Measured on a 300-message roleplay chat (Opus 5.5, 1-hour cache on). For this test the preset was arranged so nothing before the chat history changes between turns: its lorebook and depth blocks were moved to depth 0. Presets that place World Info or depth injections earlier break the cache sooner, but even this best case reuses only the system prompt.

Turn Cache read Cache write
N 8,508 126,630
N+1 8,508 127,337

The 8,508 tokens are the system prompt. The two stored prompts (extra.cachedPrompt) are identical through every history message except the last two.

Cause: Claude Code places its message cache marker on the current turn only. Marinara's current turn carries per-turn blocks: tracker <context>, the <last_message> wrapper, and depth-0 sections such as post-history instructions (and World Info, when a preset places it there). A turn later that message comes back as plain history, so the saved prefix never matches again. The resumed JSONL history has no marker of its own, so nothing between the system prompt and the current turn is reusable.

A second, smaller break: with xml wrapping, </chat_history> is appended to the last history message (marker-expander.ts), so that message also changes every turn.

Steps to reproduce

  1. Use a Claude (Subscription) connection with a long chat (any preset; xml wrap makes it certain).
  2. Send two turns.
  3. Compare generationInfo.tokensCachedPrompt / tokensCacheWritePrompt on the two replies: the read stays at the system-prompt size, and the write is roughly the whole prompt.

Environment

  • Marinara Engine version: 2.5.0 (staging @ e1e28a367)
  • Install type: source
  • OS + version: Windows 11
  • Browser or app shell: browser

Additional context

  • #6693 described the same gap ("a history cache marker on the last completed assistant turn"), but only its SDK bump (#6747) landed. The marker part never reached staging.
  • Proposed fix in the linked PR: put one cache_control on the newest assistant reply in the resumed JSONL that isn't the last history entry. Claude Code passes it through. Live test on the subscription with the 1-hour cache on: turn 2 read 24,963 of 26k tokens (without the marker: 2,398). With the 1-hour cache off, Claude Code's own TTL is account-dependent and an extra marker exceeds the four-marker limit, so that mode stays a known gap.
  • The Anthropic API provider is not affected; it already marks cachingAtDepth messages back.

Template check

  • I DID NOT read this template and provide the requested details.

cc @SpicyMarinara

Originally created by @stonebystone on GitHub (Oct 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7865 Originally assigned to: @SpicyMarinara on GitHub. ## Summary On the Claude (Subscription) connection, prompt caching only ever reuses the system prompt. Every turn writes the full chat history to the cache again and never reads it back. The cost per turn is the whole history in the prompt, which can reach Claude's context limit (up to a million tokens) on a long chat, or less where a chat summary replaces older messages. It burns subscription usage and adds latency. ## Expected behavior Each turn reads the previous turn's history from the cache and writes only the new messages and the per-turn blocks. ## Actual behavior Measured on a 300-message roleplay chat (Opus 5.5, 1-hour cache on). For this test the preset was arranged so nothing before the chat history changes between turns: its lorebook and depth blocks were moved to depth 0. Presets that place World Info or depth injections earlier break the cache sooner, but even this best case reuses only the system prompt. | Turn | Cache read | Cache write | | --- | --- | --- | | N | 8,508 | 126,630 | | N+1 | 8,508 | 127,337 | The 8,508 tokens are the system prompt. The two stored prompts (`extra.cachedPrompt`) are identical through every history message except the last two. **Cause:** Claude Code places its message cache marker on the current turn only. Marinara's current turn carries per-turn blocks: tracker `<context>`, the `<last_message>` wrapper, and depth-0 sections such as post-history instructions (and World Info, when a preset places it there). A turn later that message comes back as plain history, so the saved prefix never matches again. The resumed JSONL history has no marker of its own, so nothing between the system prompt and the current turn is reusable. A second, smaller break: with xml wrapping, `</chat_history>` is appended to the last history message (`marker-expander.ts`), so that message also changes every turn. ## Steps to reproduce 1. Use a Claude (Subscription) connection with a long chat (any preset; xml wrap makes it certain). 2. Send two turns. 3. Compare `generationInfo.tokensCachedPrompt` / `tokensCacheWritePrompt` on the two replies: the read stays at the system-prompt size, and the write is roughly the whole prompt. ## Environment - Marinara Engine version: 2.5.0 (staging @ e1e28a367) - Install type: source - OS + version: Windows 11 - Browser or app shell: browser ## Additional context - #6693 described the same gap ("a history cache marker on the last completed assistant turn"), but only its SDK bump (#6747) landed. The marker part never reached staging. - Proposed fix in the linked PR: put one `cache_control` on the newest assistant reply in the resumed JSONL that isn't the last history entry. Claude Code passes it through. Live test on the subscription with the 1-hour cache on: turn 2 read 24,963 of 26k tokens (without the marker: 2,398). With the 1-hour cache off, Claude Code's own TTL is account-dependent and an extra marker exceeds the four-marker limit, so that mode stays a known gap. - The Anthropic API provider is not affected; it already marks `cachingAtDepth` messages back. ## Template check - [ ] I DID NOT read this template and provide the requested details. cc @SpicyMarinara
bsakaguchi 2026-10-21 06:14:53 +07:00
  • closed this issue
  • added the
    in-pr
    label
Author
Owner

@SpicyMarinara commented on GitHub (Oct 20, 2026):

Fixed on staging by #7866. Thank you @stonebystone!

<!-- gh-comment-id:6286600315 --> @SpicyMarinara commented on GitHub (Oct 20, 2026): Fixed on staging by #7866. Thank you @stonebystone!
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#4327
No description provided.