用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/UitbreidenOS/UitKit --skill prompt-caching命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Guidelines and instructions for Agent execution state rollback rules
Guidelines and instructions for Agent execution step counters limits
Guidelines and instructions for Agent execution timeout limits setups
正在显示 SKILL.md
基于 SOC 职业分类
| name | prompt-caching |
| description | Optimize Claude API costs and latency with prompt caching strategies |
Claude API usage with repeated large context blocks — system prompts, prepended documents, large tool definitions — or when the user wants to reduce API costs or latency for workloads that reuse the same context across multiple calls.
Cache breakpoints mark content blocks as eligible for caching. When Anthropic's infrastructure sees the same prefix again (up to the breakpoint), it reads from cache instead of reprocessing.
ENABLE_PROMPT_CACHING_1H=1 as an environment variable (beta)Add "cache_control": {"type": "ephemeral"} to the last content block you want included in the cache prefix:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
system=[
{
"type": "text",
"text": "<your large system prompt here — must be >1024 tokens to cache>",
"cache_control": {"type": "ephemeral"},
}
],
messages=[{"role": "user", "content": "What does the system prompt say about X?"}],
)
System prompt (always cache if >1024 tokens):
system=[
{"type": "text", "text": base_instructions}, # not cached
{"type": "text", "text": large_doc, "cache_control": {"type": "ephemeral"}}, # cache up to here
]
Large document prepended to conversation:
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": large_reference_doc, "cache_control": {"type": "ephemeral"}},
{"type": "text", "text": "Now answer this question about the document above: ..."},
],
}
]
Tool definitions (cache when using many tools):
tools=[
# ... all your tool definitions ...
{
"name": "last_tool",
"description": "...",
"input_schema": {...},
"cache_control": {"type": "ephemeral"}, # cache everything up to and including this tool
}
]
In a multi-turn conversation, the messages array grows with each turn. Place the cache breakpoint at the end of the tools array or system prompt — the messages array is handled automatically by Anthropic's caching infrastructure as the conversation grows.
Do not add cache_control to every message — add it only to the large static content at the top of the context. Anthropic caches everything up to the last breakpoint in the prefix.
Check usage in the response:
print(response.usage)
# Usage(
# input_tokens=850,
# cache_creation_input_tokens=12500, # tokens written to cache (first call)
# cache_read_input_tokens=12500, # tokens read from cache (subsequent calls)
# output_tokens=200
# )
A healthy multi-turn session should show cache_creation_input_tokens > 0 only on the first call and cache_read_input_tokens > 0 on all subsequent calls.
System prompt: 15,000 tokens. 50 user messages processed per hour.
| Scenario | Cost per call | Cost/hour (50 calls) |
|---|---|---|
| No caching | 15,000 × $3/MTok | $2.25 |
| With caching (after break-even) | 15,000 × $0.30/MTok | $0.225 |
10× cost reduction for large, frequently-reused context.
A document Q&A system processes 200 questions against the same 80-page PDF per day:
# Load and cache the document once per session
doc_text = extract_text_from_pdf("report.pdf") # ~50,000 tokens
def ask_question(question: str) -> str:
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=500,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": f"<document>\n{doc_text}\n</document>",
"cache_control": {"type": "ephemeral"},
},
{"type": "text", "text": question},
],
}
],
)
return response.content[0].text
# Call 1: cache_creation_input_tokens=50000 (1.25× price)
# Calls 2-200: cache_read_input_tokens=50000 (0.1× price)
# Net savings vs no caching: ~88% on document tokens