一键导入
prompt-cache-optimization
Use to maximize prompt cache hit rates across LLM providers by enforcing a stable instruction loading order and file size discipline.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use to maximize prompt cache hit rates across LLM providers by enforcing a stable instruction loading order and file size discipline.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use for general product implementation work that is not primarily backend architecture, pure integration wiring, or screenshot-driven design-to-code.
Use when the main deliverable is maintainable documentation such as repository rules, onboarding guides, runbooks, ADRs, or architecture notes.
Use on first entry to a new repository to run environment scanning and ask targeted boundary questions before implementation.
Use after writing or modifying code to enforce the mandatory write → test → fix → repeat validation cycle.
Use before committing to a design or plan to force assumption-surfacing. The agent challenges your design, questions edge cases, and flags gaps — you patch vague decisions. Prevents the failure mode where a design "feels explained" but contains hidden flaws that only appear during implementation.
Use to establish and maintain a shared domain glossary (UBIQUITOUS_LANGUAGE.md). Creates a single source of term definitions that all agents, prompts, and documents must use — preventing semantic drift and repeated re-explanation across sessions.
| name | prompt-cache-optimization |
| description | Use to maximize prompt cache hit rates across LLM providers by enforcing a stable instruction loading order and file size discipline. |
Use this skill to reduce API costs and latency by maximizing prompt cache hits. The techniques here are provider-agnostic — they work with any LLM that uses prefix-based prompt caching (Anthropic, OpenAI, Google, local inference engines like vLLM/SGLang). The key requirement: the beginning of the prompt must be identical byte-for-byte across requests — instruction files must load in a consistent, deterministic order.
Instruction content is classified into four layers by change frequency, loaded from most stable to most volatile:
Layer 1 — Static bootstrap + rules (rarely change)
├── profile-aware bootstrap (`docs/rules-nano.md` or `docs/rules-quickstart.md` or full docs)
├── AGENTS.md (entrypoint when the selected profile uses it)
├── docs/operating-rules.md
└── docs/agent-playbook.md
Layer 2 — Stable skills (content stable; subset varies by task type)
└── skills/*/SKILL.md (selected by demand-triage)
Layer 3 — Semi-stable project state (changes weekly to monthly)
├── DECISIONS.md
└── ARCHITECTURE.md (if present)
Layer 4 — Volatile context (changes every request)
├── Session memory / context anchors
├── User query
└── Current file content / code snippets
DECISIONS.md do not invalidate the Layer 1+2 cache prefix.┌─────────────────────────────────────────────┐
│ Layer 1: Static rules │ ← Cached across ALL requests
│ Layer 2: Stable skills │ ← Cached across same-type tasks
├─────────────────────────────────────────────┤
│ Layer 3: DECISIONS.md, ARCHITECTURE.md │ ← Cached until project state changes
├─────────────────────────────────────────────┤
│ Layer 4: Session context, user query │ ← Never cached (unique per request)
└─────────────────────────────────────────────┘
When multiple skills are loaded for a task, use alphabetical order by skill directory name within the same layer. This ensures the same set of skills always produces the same prefix.
| Task type | Skills loaded (in order) |
|---|---|
| Small task | demand-triage, error-recovery, memory-and-state, repo-exploration, test-and-fix-loop |
| Medium frontend | application-implementation, demand-triage, error-recovery, memory-and-state, repo-exploration, test-and-fix-loop |
| Medium backend | backend-change-planning, demand-triage, error-recovery, memory-and-state, repo-exploration, test-and-fix-loop |
| Large feature | demand-triage, error-recovery, feature-planning, memory-and-state, repo-exploration, test-and-fix-loop |
| Design-to-code | demand-triage, design-to-code, error-recovery, memory-and-state, repo-exploration, test-and-fix-loop |
| Documentation | demand-triage, documentation-architecture, error-recovery, memory-and-state, repo-exploration, test-and-fix-loop |
| Prompt/cache tuning | prompt-cache-optimization (added on-demand; appended after existing skills) |
Always-tier skills remain part of every canonical set. On-demand skills such as prompt-cache-optimization are appended after the canonical set when needed. They never change the order of the canonical set.
Large instruction files push volatile content further from the prefix boundary, increasing the cached portion. But excessively large files waste tokens when their content is not relevant.
| Metric | Target | Action if exceeded |
|---|---|---|
| Single instruction file | ≤ 8 KB (~2000–3000 tokens) | Split into focused files; load only what the task needs |
| Layer 1 total | ≤ 20 KB | Review for redundancy; move examples to templates |
| Layer 2 per-task total | ≤ 15 KB | Ensure demand-triage selects only relevant skills |
| DECISIONS.md (Layer 3) | ≤ 30 KB | Trigger archive per memory-and-state skill |
The memory-and-state skill's archive rules (30 KB / 50 entries threshold for DECISIONS.md) directly support cache optimization by keeping Layer 3 compact. When Layer 3 grows, it pushes Layer 4 further out and reduces the effective cache window. Treat memory lifecycle thresholds as cache-relevant, not just memory-relevant.
| Provider | Cache mechanism | Minimum prefix | TTL | Notes |
|---|---|---|---|---|
| Anthropic (Claude) | Automatic prefix cache | ~1024 tokens | ~5 min | Also supports explicit cache_control breakpoints via API |
| OpenAI (GPT-4o, o1, etc.) | Automatic prefix cache | ~1024 tokens (128-token aligned) | ~5–10 min | Discounts cached input tokens automatically |
| Google (Gemini) | Context Caching API | Configurable | Configurable | Requires explicit API call to create cached content |
| vLLM / SGLang | RadixAttention / prefix cache | Varies | Session-scoped | Automatic; benefits from stable system prompt |
| Tool | How to apply loading order |
|---|---|
| Claude Code | Already has built-in optimization; loading order provides additional prefix stability |
| VS Code Copilot | .github/copilot-instructions.md is auto-injected (Layer 1); skills are read by agent in declared order (Layer 2) |
| Custom API calls | Place Layer 1–2 in system message; Layer 3 at start of user message; Layer 4 as the rest of user message |
| Claude API with cache_control | Set cache breakpoints at Layer 1–2 boundary and Layer 2–3 boundary |
Tool/function schemas are part of the prompt and count toward the prefix. Unstable tool definitions cause cache misses just like unstable instructions.
When building custom agent orchestration on top of LLM APIs, avoid sending full JSON schema on every request. Use a two-part model: a server-side registry that stores the full schema, and a lightweight per-request payload that references it by name and hash.
Registry (stored server-side or in project config):
tools:
create_order_v2:
schema_hash: "abc123"
schema: { ... full JSON schema ... }
Per-request payload (lightweight — references the registered schema by name and version):
{
"tools": ["create_order_v2"],
"tool_schema_version": "abc123"
}
If the LLM provider requires full schemas in each request (most do today), ensure schemas are loaded from a stable source in a deterministic order so the prefix remains consistent. The registry pattern becomes directly useful when providers support schema references or when using self-hosted inference.
| Task type | Typical tools needed |
|---|---|
| Code implementation | file read/write, terminal, search, lint |
| Planning / review | file read, search, memory |
| Documentation | file read/write, search |
Avoid loading code-modification tools for read-only review tasks. This reduces prompt size and keeps the prefix shorter.
Adopting projects can declare a prompt-budget.yml at the repo root to control which skills and roles load per request. This makes token usage visible and auditable.
budget.profile — if set, use the named profile (nano, minimal, standard, full) as the default configuration. If not set, default to standard.skills.* or roles.* entries in the file override the profile defaults.skills.disabled; skip those skills entirely.roles.disabled; do not route to those roles.budget.layer2_max_tokens, load only skills.always_load and defer on-demand skills.trimming.* thresholds instead of defaults from memory-and-state skill.| Profile | Layer 2 ceiling | Skills loaded | Behavior differences |
|---|---|---|---|
nano | 0 tokens | 0 (all behaviors native) | Single-file Small tasks only. Layer 1 = docs/rules-nano.md (~630 tokens). No skill files loaded. Agent escalates immediately for multi-file or complex tasks. |
minimal | ≤ 4,000 tokens | 2 (demand-triage, repo-exploration) | Agent uses native tool capabilities for testing, error handling, and memory. Reflection/trace behavior is abbreviated and usually inline. Small tasks only. |
standard | ≤ 8,000 tokens | 5 (all Always-tier) | Conditional skills activate by trigger. On-demand domain skills require explicit opt-in. |
full | ≤ 15,000 tokens | 5 + all applicable Conditional + On-demand | No restrictions. Full observability, self-reflection, and planning. |
When budget.profile: minimal, agents should:
test-and-fix-loop.error-recovery.DECISIONS.md directly instead of loading memory-and-state.See docs/agent-playbook.md → Budget profiles for the complete specification.
The Layer 1 content varies by budget profile to respect token targets:
| Profile | Layer 1 content | Est. tokens |
|---|---|---|
nano | docs/rules-nano.md only | ~630 |
minimal | docs/rules-quickstart.md only | ~1,200 |
standard | docs/rules-quickstart.md → expand to full docs as needed | ~4,000–5,000 |
full | Full docs/operating-rules.md + docs/agent-playbook.md | ~18,350 |
When budget.profile: minimal:
docs/rules-quickstart.md (enhanced with constitutional principles, checkpoint outcomes, and minimal-profile role definitions).docs/operating-rules.md or docs/agent-playbook.md unless a specific section is needed (deferred loading via "When to open full docs" in rules-quickstart.md).When budget.profile: standard or unset:
docs/rules-quickstart.md first, then expand into the full source docs for task-specific detail.When budget.profile: full:
docs/operating-rules.md and docs/agent-playbook.md immediately.If prompt-budget.yml does not exist, use the standard profile defaults with the full skill sets defined in the Canonical skill sets table above.
# prompt-budget.yml
execution_mode: semi-auto
budget:
profile: standard
layer1_target_tokens: 4000 # Static rules target after profile-aware bootstrap
layer2_max_tokens: 8000 # Skills per request
layer3_max_tokens: 3000 # DECISIONS.md + ARCHITECTURE.md
roles:
enabled: [feature-planner, application-implementer, risk-reviewer, critic]
disabled: [backend-architect, ui-image-implementer, integration-engineer, documentation-architect]
skills:
always_load: [demand-triage, repo-exploration, test-and-fix-loop, error-recovery, memory-and-state]
on_demand: [prompt-cache-optimization]
disabled: [design-to-code, documentation-architecture]
trimming:
decisions_archive_threshold_kb: 30
decisions_archive_threshold_entries: 50
session_memory_max_files: 10
See docs/adoption-guide.md → Prompt budget trimming for step-by-step adoption guidance and impact estimates.
memory-and-state.Avoid these behaviors during an active task or conversation:
When task scope changes enough to require a different skill set, acknowledge that the cache boundary will move and keep the new loading order deterministic from that point onward.
Before completing a task where this skill applies, verify:
prompt-budget.yml for a new or existing project