Skip to content
Back to skills

Summaries

ASecurity

Prompt caching design + optimization reference inside the `claude-api` skill. **The one invariant**: **prompt caching is a prefix match**. Any byte change anywhere in the prefix invalidates everything after. Cache key = exact bytes up to each `cache_control` breakpoint. Render order: **`tools` → `system` → `messages`**. A breakpoint on the last system block caches tools+system together. **Workflow for optimizing existing code**: 1. Trace prompt assembly path. 2. Classify each input by stabili...

  • 8 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 30, 2026
ai-agentsgogitapi

Works with

  • api

Security analysis

A100/100

Pro scans all 21 files and shows the line behind each finding

Scanned November 30, 2026

npx -y skills add NickCollect/ai-coding-runbook --skill Summaries --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Summaries?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Summaries
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/nickcollect-summaries-ad7d79b4/badge)](https://www.skillsdirectory.com/skills/nickcollect-summaries-ad7d79b4)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
type: summary
source: 01_Raw/github/anthropics/skills/skills/claude-api/shared/prompt-caching.md
title: "claude-api skill: prompt-caching reference"
summarized_at: 2026-05-05
entities_referenced: [Skill]
concepts_referenced: [Prompt-caching]
---

Prompt caching design + optimization reference inside the `claude-api` skill.

**The one invariant**: **prompt caching is a prefix match**. Any byte change anywhere in the prefix invalidates everything after. Cache key = exact bytes up to each `cache_control` breakpoint.

Render order: **`tools` → `system` → `messages`**. A breakpoint on the last system block caches tools+system together.

**Workflow for optimizing existing code**:
1. Trace prompt assembly path.
2. Classify each input by stability: never-changes (early), per-session (after global prefix), per-turn (end), per-request (eliminate or move to very end).
3. Check rendered order matches stability order.
4. Place breakpoints at stability boundaries.
5. Audit for silent invalidators.

**Placement patterns**:
- **Large shared system prompt**: breakpoint on last system text block (caches tools+system).
- **Multi-turn conversations**: breakpoint on last content block of latest user turn.
- **Shared prefix, varying suffix**: breakpoint at end of SHARED portion not whole prompt.
- **Prompts that change from byte 0**: don't cache — pay write premium with no reads.

**Architectural guidance** (matters more than markers):
- Keep system prompt **frozen**. Don't interpolate "current date: X" or "user: Z" — invalidates downstream. Inject dynamic context as later messages.
- Don't change tools or model mid-conversation. Tools at position 0 — add/remove/reorder invalidates entire cache. Caches model-scoped. Serialize tools deterministically (sort by name).
- Fork operations must reuse parent's exact prefix. Sub-agents/summarization must copy `system`/`tools`/`model` verbatim or miss parent's cache.

**Silent invalidators** (grep for these):
- `datetime.now()` / `Date.now()` / `time.time()` in system prompt.
- `uuid4()` / `crypto.randomUUID()` early in content.
- `json.dumps(d)` without `sort_keys=True`; iterating a `set`.
- f-string with session/user ID in system prompt.
- Conditional system sections (`if flag: system += ...`).
- `tools=build_tools(user)` where set varies per user.

**API**:
- `cache_control: {type: "ephemeral"}` — 5-minute TTL default.
- `cache_control: {type: "ephemeral", ttl: "1h"}` — 1-hour TTL.
- Max 4 breakpoints per request.
- Goes on any content block: text/image/tool_use/tool_result/document.
- Top-level `cache_control` on `messages.create()` auto-places on last cacheable.

**Minimum cacheable prefix** (model-dependent — silently won't cache shorter):
| Model | Min |
|---|---|
| Opus 4.7 / 4.6 / 4.5 / Haiku 4.5 | 4096 |
| Sonnet 4.6 / Haiku 3.5 / Haiku 3 | 2048 |
| Sonnet 4.5 / 4.1 / 4 / 3.7 | 1024 |

**Economics**: cache reads ~0.1× base. Writes 1.25× (5min TTL) or 2× (1h TTL). 5min break-even = 2 requests; 1h break-even = 3 requests.

**Verify hits**: `usage.cache_read_input_tokens`, `cache_creation_input_tokens`, `input_tokens` (uncached remainder). Total = sum of all three. If `cache_read_input_tokens` zero across repeated identical prefixes, silent invalidator at work.

**Invalidation hierarchy** (3 cache tiers, changes invalidate own tier + below):
| Change | Tools / System / Messages cache |
|---|---|
| Tool defs (add/remove/reorder) | All invalidated |
| Model switch | All invalidated |
| `speed`, web-search, citations toggle | System+messages invalidated |
| System prompt content | System+messages invalidated |
| `tool_choice`, images, `thinking` toggle | Only messages invalidated |
| Message content | Only messages invalidated |

→ Can change `tool_choice` per request or toggle `thinking` without losing tools+system cache.

**20-block lookback window**: each breakpoint walks backward at most 20 content blocks to find prior cache entry. Long agentic loops (many tool_use/tool_result pairs in one turn) can exceed → silent miss. Fix: place intermediate breakpoint every ~15 blocks.

**Concurrent-request timing**: cache becomes readable only AFTER first response BEGINS streaming. N parallel requests with identical prefixes all pay full price. Fan-out: send 1 → await first streamed token → fire remaining N−1.

Files in this skill

  • 100k-context-windows--anthropic-news.md1.2 KB
  • 2026-w13.md2.5 KB
  • 2026-w14.md2.5 KB
  • 2026-w15.md2.5 KB
  • 2026-w16.md3.3 KB
  • 2026-w17.md2.9 KB
  • 3-5-models-and-computer-use--anthropic-news.md1.8 KB
  • 3p-updates--internal-comms.md1.6 KB
  • 81k-economics--anthropic-research.md2.3 KB
  • AGENTS--codex-openai.md1.8 KB
  • AGENTS--mcp-py-sdk.md3.1 KB
  • AGENTS--mcp-spec.md860 B
  • AI-assistance-coding-skills--anthropic-research.md1.3 KB
  • AI-fluency-index--anthropic-research.md1.2 KB
  • AI-resistant-technical-evaluations--anthropic-eng.md2.5 KB
  • ANTITRUST--mcp-spec.md569 B
  • AmazonBedrock-CONTRIBUTING--prompt-eng-tutorial.md1.3 KB
  • AmazonBedrock-README--prompt-eng-tutorial.md1.8 KB
  • CHANGELOG--agent-sdk-py-repo.md3.2 KB
  • CHANGELOG--anthropic-sdk-py.md1.7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.