prompt-caching-ttl
Use Claude's prompt caching with 5-minute and 1-hour TTLs to slash costs on repeated context — codebases, system prompts, long documents. Covers cache breakpoints, hit-rate optimization, and the common mistakes that silently disable caching. Use this skill when building apps with repeated large context, optimizing LLM spend, or debugging "why are my cache reads zero?" Activate when: prompt caching, cache_control, cache hit rate, 5 minute cache, 1 hour TTL cache, ephemeral cache, reduce Claude cost.
What this skill does
# Prompt Caching — 5min & 1h TTL
**Prompt caching reuses already-processed prefixes. Cache reads cost ~10% of fresh input. For apps with large repeated context, this is the single biggest lever on your bill.**
## When to Use
- Large system prompts reused across many requests
- Long documents/codebases with many follow-up questions
- Multi-turn conversations with growing history
- Tool/function definitions shared across sessions
- Any call where > 1024 tokens would be repeated (2048 for Haiku)
## Two TTLs
| TTL | Use case | Cost of cache write |
|---|---|---|
| **5 min (ephemeral)** | Conversation, active session, interactive tools | ~1.25× input |
| **1 hour** | System prompts, knowledge bases, codebases | ~2× input |
Cache reads are ~0.1× input cost regardless of TTL. The only difference is how long the cache persists and what the write costs.
Rule: use 1h when the cache lives across sessions or independent users; use 5min for in-session reuse.
## Basic Usage
```ts
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 4096,
system: [
{ type: "text", text: "You are a helpful assistant." },
{
type: "text",
text: giantCodebase,
cache_control: { type: "ephemeral", ttl: "1h" },
},
],
messages: [{ role: "user", content: "Where is auth handled?" }],
});
console.log(response.usage);
// { input_tokens: 120, cache_creation_input_tokens: 450000, cache_read_input_tokens: 0, output_tokens: 200 }
```
Next call within 1h:
```
{ input_tokens: 120, cache_creation_input_tokens: 0, cache_read_input_tokens: 450000, output_tokens: 180 }
```
## Cache Breakpoints
You can place up to **4 cache breakpoints** per request. Everything up to a breakpoint is cached as a prefix. Typical pattern:
```ts
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 4096,
system: [
{ type: "text", text: systemPrompt, cache_control: { type: "ephemeral", ttl: "1h" } },
],
tools: [
// all tool definitions
{ ...lastTool, cache_control: { type: "ephemeral", ttl: "1h" } }, // breakpoint at end of tools
],
messages: [
{ role: "user", content: "Long context document..." },
{
role: "assistant",
content: [{ type: "text", text: "Understood.", cache_control: { type: "ephemeral", ttl: "5m" } }],
},
{ role: "user", content: "Current question." }, // NOT cached — this changes every call
],
});
```
Four breakpoints: system, tools, document, conversation history. The current turn stays uncached.
## Prefix Matching Rules
Cache hits require **byte-exact prefix match** up to the breakpoint. Common breakers:
1. **Timestamps / UUIDs in system prompt** — moves every call; kills the cache
2. **Reordering tools** — same tools in different order = cache miss
3. **Changing any earlier message** — even whitespace; cache invalidates
4. **Different `model`** — caches are per-model
5. **Different `max_tokens`** — doesn't break cache, but other param changes might
Keep everything before the breakpoint stable and deterministic.
## Minimum Cacheable Size
- Most models: 1024 tokens
- Haiku: 2048 tokens
Below the minimum, `cache_control` is ignored silently.
## Inspecting Hit Rate
```ts
const usage = response.usage;
const hitRate = usage.cache_read_input_tokens /
(usage.cache_read_input_tokens + usage.cache_creation_input_tokens + usage.input_tokens);
console.log(`Cache hit rate: ${(hitRate * 100).toFixed(1)}%`);
```
Target: > 80% hit rate for production workloads on stable prefixes.
## Multi-Turn Pattern
For conversations, put a 5m breakpoint at the last assistant turn. Each new user turn extends the cache:
```ts
function addCacheBreakpoint(messages: any[]) {
const copy = [...messages];
const lastAssistant = [...copy].reverse().find((m) => m.role === "assistant");
if (lastAssistant && Array.isArray(lastAssistant.content)) {
const lastBlock = lastAssistant.content[lastAssistant.content.length - 1];
if (lastBlock.type === "text") lastBlock.cache_control = { type: "ephemeral", ttl: "5m" };
}
return copy;
}
```
As the conversation grows, each call reuses the accumulated cache.
## Combining with 1M Context
1M context is expensive. Cache it:
```ts
const response = await client.messages.create(
{
model: "claude-sonnet-4-6",
max_tokens: 4096,
system: [
{ type: "text", text: giantCodebase, cache_control: { type: "ephemeral", ttl: "1h" } },
],
messages: [{ role: "user", content: userQuestion }],
},
{ headers: { "anthropic-beta": "context-1m-2025-08-07" } },
);
```
First call: pay ~2× input for cache write. Every subsequent question in the hour: ~10% input. Breaks even after ~2 questions.
## Anti-Patterns
1. **Dynamic content before breakpoint** — timestamp, request ID, rand in system prompt
2. **No breakpoint placed** — `cache_control` missing, nothing cached
3. **Cache smaller than minimum** — silently ignored
4. **Ignoring hit rate metrics** — you think caching is working but it's not
5. **Using 5m for cross-session knowledge bases** — re-paying cache write constantly
## Debugging Zero Hit Rate
1. Log `cache_creation_input_tokens` and `cache_read_input_tokens` every call
2. If `creation > 0` on every call: the prefix is changing. Diff consecutive requests byte by byte
3. If both are 0: prefix is below minimum tokens, or `cache_control` isn't being sent
4. If `creation` on first call but `read` is 0 on second within TTL: different model, different region, or parameter change broke the cache
## Best Practices
1. Place breakpoints at stable boundaries: end of system prompt, end of tools, end of large document
2. Use 1h TTL for knowledge bases reused across requests; 5m for conversations
3. Keep everything before breakpoints byte-stable — no timestamps, no UUIDs
4. Monitor hit rate as a first-class metric
5. For multi-turn, move a 5m breakpoint to the last assistant message each call
6. Combine with 1M context for big corpora — caching makes long-context affordable
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.