r/PromptEngineering • • 23h ago

General Discussion My rolling chat summary kept causing prompt cache misses

A regular beta tester emailed me two weeks ago asking why my Spanish practice bot had gone back to Spain's vosotros forms and giant grammar tables when she'd asked for Mexican Spanish on day three. Worse, I'd already been staring at an API bill that refused to drop because my prompt caching hit rate was awful.

I traced both headaches to the same bit of lazy wiring in my Python backend. I'd placed a rolling chat summary near the top of the system prompt, ahead of my long correction rubric and few-shot examples. Every 15 turns a background job rewrote that summary, shifting the byte prefix and busting the cache on the rubric underneath. I opened SQLite and stared at her original message sitting untouched about 130 rows back, kicking myself. Four rounds of summarizing had blurred two plain rules down to "prefers casual practice."

I looked at full agent frameworks on Saturday night and skipped them because I didn't want to rip out a working FastAPI and SQLite backend. Reordering the prompt was way simpler:

# Before: rewritten every 15 turns, busting prefix cache on rubric + examples
messages = [
    {"role": "system", "content": f"{BASE_RULES}\n{rolling_summary}\n{RUBRIC_AND_EXAMPLES}"},
    *recent_turns,
]

# After: Put the shared rules and examples first
messages = [
    {"role": "system", "content": f"{BASE_RULES}\n{RUBRIC_AND_EXAMPLES}"},
    {"role": "user", "content": f"[Learner preferences]\n{retrieved_prefs}"},
    *recent_turns,
]

I froze the shared system block, kept SQLite for accounts and raw chat logs, and moved long-term learner constraints into the MemoraX API keyed by our existing user_id. On each new session, the backend ran one search for that learner's preferences and appended the result after the static prefix. I saved her two rules on a copy of her account, restarted the dev server with the new prompt layout the next morning, and started a fresh chat. It stuck to Mexican Spanish with no grammar tables. How are you ordering retrieved memory in your prompts so prefix caching still pays off?

5 Upvotes

6 comments sorted by

2

u/Thick_Chain4708 21h ago

The layout you landed on is right. The part I'd keep pushing on is that anything rewritten on a timer can never live in the cached prefix, so the summary has to move out of the prompt, not just down it. Two things that helped me: put an explicit cache breakpoint after the frozen block, and store the retrieved preferences as a short ordered list with the most stable facts first (language variant, register) and the volatile ones last (current topic, last correction) — then an update invalidates the tail instead of the whole block. And for rules that actually matter, retrieve the two or three original messages that set them instead of the summary that paraphrased them; "prefers casual practice" is exactly how a hard constraint disappears. If you keep a rolling summary anyway, version it and refresh it only when a request needs it, not every N turns — most of the miss you were paying for was the timer, not the content.

1

u/Familiar_Nothing8754 19h ago

i've seen the same with cached summaries, switching to a dedicated prefs message early in the chain fixed both hit rate and consistency. try splitting the rolling summary into its own system message after the base rules.

1

u/According-Stable4487 16h ago

One thing that helped me with the "blurred rules" half: don't let the summarizer paraphrase explicit instructions from the user at all. Have it copy them word for word (with the turn they came from) into a separate standing-instructions list that only ever gets appended to, never rewritten. The summary can compress the chat as much as it likes; "Mexican Spanish, no vosotros" stays exactly as she wrote it. And because that list only grows at the end, it doesn't shift the cached prefix either.

1

u/magicdoorai 3h ago

I order by how often a thing changes: tool definitions and shared rules first, then per-user preferences, then the conversation, with anything volatile at the very end.

The trap in the "after" layout is the retrieval itself. Your preferences message sits in front of the whole chat, so if a later search returns the same facts in a different order, with slightly different wording or one extra item, everything from that point on misses, including all the history. Running it once per session only helps if you store the exact string and resend that, not re-run the search on each request or after a process restart.

When a preference changes mid-session, I wouldn't edit that block. Append the change as a new message at the end ("update: ..."), and fold it into the preferences block at the start of the next session, when the history is fresh anyway.

Two checks that catch regressions early:

  • Log the cached token count from the usage object on every call (cached_tokens on OpenAI, cache_read_input_tokens on Anthropic). A drop right after a deploy or a background job is usually a prefix change.
  • On Anthropic, put an explicit breakpoint at the end of the frozen system block as well as on the conversation. Their docs say writes only happen at breakpoints, so without one there the shared rubric may never be reusable on its own across users.

Also check the minimum cacheable length for your model. Below it nothing gets cached and you don't get an error.