Prefix Cache Stability
Agent Buildingprefix-cache-stabilityPrefix Cache Stability is an Agent Skill for agent building published in the leookun/cursor-byok repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.
Use when changing prompt compilation, history replay, persisted conversation state, model request construction, or dynamic reminders in this repo; protects prefix-cache hit rate by keeping model-visible history append-only and dynamic attention scoped to the latest request.
QUICK START
How to use this skill
Bring this guide into your coding agent with a prompt tailored to the tool you use.
- Open your project in Codex.
- Copy and paste the prompt below.
- Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/leookun/cursor-byok/blob/HEAD/.agents/skills/prefix-cache-stability/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/prefix-cache-stability, but wait for my approval before writing files or running scripts.
Copying this prompt does not install or run the skill. Review third-party files before use.
Prefix Cache Stability
Use this skill before editing prompt, history replay, persisted conversation state, or provider request code.
Hard Constraints
- Model-visible history is append-only. If a message was sent to the model and is meant to remain historical context, persist it and replay it at the same relative position.
- Do not move previously sent model-visible messages to a new position in later requests.
- Keep the largest stable prefix first: system prompt, imported replay, persisted user/request/tool history, then current-turn suffix context.
- Truly dynamic attention is latest-only. Current state blocks, latest edit guards, and other volatile reminders should be appended near the end of the current request and should not become long-lived prefix content unless they are intentionally persisted as historical facts.
- Persisted prompt context must be worded so it is safe as history. Avoid stale wording like "currently" unless the context is only latest-only.
- Never optimize cache by dropping correctness-critical context.
- Never remove, strip, reorder, or suppress historical
reasoning_contentreplay merely to reduce repetitive thinking. Some providers need prior reasoning for valid continuation; optimize the latest tool guidance or current-turn prompt behavior instead.
Implementation Pattern
- Classify each prompt addition:
- Stable system policy: belongs in the fixed system prompt.
- Historical model-visible context: persist as replayable history.
- Latest-only attention: append as current suffix, do not persist.
- For persisted context, store enough metadata to dedupe the same turn, usually
sourceplus a content hash. - Replay persisted context from history/projector, not by regenerating and inserting it into old positions.
- On provider retries or same-turn follow-up passes, do not duplicate an already persisted prompt context.
- Persisted conversation state must include replayable prompt context so a restarted conversation preserves the same prefix. In this repo, use
context.json.itemsfor replayable semantic history andstate.jsonfor mutable latest state.
Verification
- Compare adjacent provider request artifacts or captured canonical request bodies and compute the longest common prefix.
- Check final raw SSE usage fields before blaming local metrics. Some OpenAI-compatible providers do not return cached-token fields.
- A healthy change should make old request prefixes stable while allowing only the newest suffix to vary.