Prompt Caching
Enable caching for long prompts that are reused. Later requests can reduce cost by about 90% and latency by about 85%.
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")
with open("long_document.txt") as f:
document = f.read()
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
system=[
{"type": "text", "text": "You are a document analysis assistant."},
{
"type": "text",
"text": document,
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": "Please summarize the main points of this document"}]
)
print(response.content[0].text)
print(f"Cache creation tokens: {response.usage.cache_creation_input_tokens}")
print(f"Cache read tokens: {response.usage.cache_read_input_tokens}")
import Anthropic from "@anthropic-ai/sdk";
import fs from "fs";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://aisupermarket.work" });
const document = fs.readFileSync("long_document.txt", "utf-8");
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
system: [
{ type: "text", text: "You are a document analysis assistant." },
{ type: "text", text: document, cache_control: { type: "ephemeral" } },
],
messages: [{ role: "user", content: "Please summarize the main points of this document" }],
});
console.log(response.content[0].text);
console.log(`Cache creation tokens: ${response.usage.cache_creation_input_tokens}`);
console.log(`Cache read tokens: ${response.usage.cache_read_input_tokens}`);
curl https://aisupermarket.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"system": [
{"type": "text", "text": "You are a document analysis assistant."},
{"type": "text", "text": "<your long document here>", "cache_control": {"type": "ephemeral"}}
],
"messages": [{"role": "user", "content": "Please summarize the main points of this document"}]
}'
Cache Tool Definitions in Multi-Turn Chat
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")
tools = [
{
"name": "search",
"description": "Search the knowledge base",
"input_schema": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"]
},
"cache_control": {"type": "ephemeral"}
}
]
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Search for content about Python"}]
)
print(response.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://aisupermarket.work" });
const tools = [
{
name: "search",
description: "Search the knowledge base",
input_schema: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
},
cache_control: { type: "ephemeral" },
},
];
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
tools,
messages: [{ role: "user", content: "Search for content about Python" }],
});
console.log(response.content[0].text);
curl https://aisupermarket.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [{
"name": "search",
"description": "Search the knowledge base",
"input_schema": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]},
"cache_control": {"type": "ephemeral"}
}],
"messages": [{"role": "user", "content": "Search for content about Python"}]
}'
The minimum cache lifetime is 5 minutes and the maximum is 1 hour. At least 1024 tokens must be cached (2048 for Haiku).