[FEATURE] Auto-memory writes invalidate entire prompt cache chain — MEMORY.md should not sit in cache-sensitive prefix

Resolved 💬 3 comments Opened Apr 11, 2026 by ZWhiteTrace Closed Apr 13, 2026

Problem

Auto-memory (MEMORY.md) is injected into the system prompt's Block 4 (dynamic content, after DYNAMIC_BOUNDARY). Every time Claude writes a memory entry, MEMORY.md content changes, which breaks the prompt cache chain from Block 4 onward — including the entire tools section and all accumulated message history.

This is architecturally contradictory: auto-memory is designed to be written frequently during sessions (learning user preferences, storing project context), yet it sits in the most cache-sensitive position possible.

Impact

A single memory write mid-session causes:

  • ~10x input token cost for that turn (entire message history recalculated at full price)
  • 2-3 subsequent turns at elevated cost while cache rebuilds
  • On a session with 80K tokens of message history, one memory write costs ~80K extra full-price input tokens

For active users who let Claude save memories naturally, this can happen 2-3 times per session, effectively negating most of the prompt cache savings.

Current workaround

Users can instruct Claude to batch memory writes to session end, but this defeats the purpose of auto-memory (which should capture context as it emerges, not retroactively).

Suggested solutions (non-exhaustive)

  1. Move MEMORY.md after the cache breakpoint — place it in the message stream or as a late system block so it doesn't invalidate the tools section and prior messages
  2. Use a dedicated cache segment for dynamic content — separate cache_control markers for static vs dynamic system blocks
  3. Diff-aware cache — only invalidate from the point of change rather than the entire suffix (may require API-level changes)
  4. Deferred memory injection — load MEMORY.md content on first reference rather than injecting it into every system prompt

Related

  • #44166 (exempt CLAUDE.md/auto-memory from compaction — different concern, same file)
  • #44820 (PreMemoryWrite/PostMemoryWrite hooks — complementary)

Environment

  • Claude Code version: latest (2026-04-11)
  • Model: claude-opus-4-6 with 1M context
  • Plan: Max (1-hour cache TTL)
  • Platform: macOS

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗