Integration guide
GPT & Claude Prompt Caching | Setup, Usage and Billing
Reusing long instructions, reference material, and conversation history can reduce input charges for GPT and Claude. Learn how to structure requests, enable Claude caching, and verify cache hits in usage history.
Reuse long instructions in GPT Responses
Put reusable instructions and reference material in a developer message at the start of input. Preserve the order of earlier messages and tool definitions across turns, then append new content. Put timestamps, per-turn values, and temporary instructions after the stable prefix.
GPT-6.2 Sol and Astra place top-level instructions after input. Those instructions are charged as uncached input even on a hit. Use that field for turn-specific guidance and use the example below for reusable instructions. Set ZHUAN_GPT_MODEL to a model ID from Pricing and save your long instructions in project-context.txt.
import os
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key=os.environ["ZHUAN_API_KEY"],
base_url="https://key.zhuan.xyz/v1",
)
context = Path("project-context.txt").read_text(encoding="utf-8")
response = client.responses.create(
model=os.environ["ZHUAN_GPT_MODEL"],
input=[
{"role": "developer", "content": context},
{"role": "user", "content": "Summarize the release requirements."},
],
)
print(response.output_text)
Preserve a reusable prefix in Claude
A cache breakpoint marks the whole prefix from the start of the request through that block. Claude processes tool definitions, system instructions, and messages in that order. Changes to content, position, or order can affect cache hits from that point onward.
In a multi-turn conversation, resend earlier messages in order, then append the last assistant reply and the new user question. The next turn may read the previous prefix and write new content in the same call. Cache reuse across independent requests is not guaranteed.
Five-minute automatic caching in Claude Messages
Add cache_control at the top level of a native Messages request. Claude moves the breakpoint to the last cacheable block in the current request and retains the cache for five minutes by default. After the first write, append turns within that window to read the existing prefix and write new content.
The Claude examples below use curl and jq. Set ZHUAN_API_KEY to your ZHUAN key, set ZHUAN_CLAUDE_MODEL to an available Claude model ID, and put a sufficiently long, stable context in project-context.txt.
jq -n --arg model "$ZHUAN_CLAUDE_MODEL" --rawfile context project-context.txt '{
model: $model,
max_tokens: 256,
cache_control: {type: "ephemeral"},
system: [{type: "text", text: $context}],
messages: [{role: "user", content: "Summarize the release requirements."}]
}' | curl --fail-with-body "https://key.zhuan.xyz/v1/messages" \
-H "x-api-key: $ZHUAN_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
--data-binary @-
Set an explicit cache breakpoint
- Put cache_control on the last stable text block, such as the end of a long system prompt. Place timestamps, request IDs, and other per-turn values after that breakpoint.
- For multi-turn conversations, you can add a breakpoint to the latest user message. Move it to the newest user message on the next request, keeping earlier message text and order. A request can have up to four breakpoints, including the one used by automatic caching.
- Set ttl to 5m or omit it to use the default. A cache hit refreshes the lifetime. ZHUAN currently supports five-minute retention.
Explicit breakpoint example for Anthropic Messages
jq -n --arg model "$ZHUAN_CLAUDE_MODEL" --rawfile context project-context.txt '{
model: $model,
max_tokens: 256,
system: [{type: "text", text: $context,
cache_control: {type: "ephemeral", ttl: "5m"}}],
messages: [{role: "user", content: "Summarize the release requirements."}]
}' | curl --fail-with-body "https://key.zhuan.xyz/v1/messages" \
-H "x-api-key: $ZHUAN_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
--data-binary @-
OpenAI-compatible Chat Completions example
For an OpenAI-compatible request to Claude, use a content array for the system or user message you want to cache. Put cache_control on the relevant text part, as shown below.
jq -n --arg model "$ZHUAN_CLAUDE_MODEL" --rawfile context project-context.txt '{
model: $model,
max_tokens: 256,
messages: [
{role: "system", content: [{type: "text", text: $context,
cache_control: {type: "ephemeral", ttl: "5m"}}]},
{role: "user", content: "Summarize the release requirements."}
]
}' | curl --fail-with-body "https://key.zhuan.xyz/v1/chat/completions" \
-H "Authorization: Bearer $ZHUAN_API_KEY" \
-H "Content-Type: application/json" \
--data-binary @-
How do I check hits and charges?
Open Usage history in the console to see Cache read / write and the charge for each request. A read (R) means a previously cached prefix was reused. A write (W) records newly cached content. Both can appear on the same request.
In Anthropic Messages, usage.cache_read_input_tokens counts reads, usage.cache_creation_input_tokens counts writes, and usage.input_tokens counts uncached input. When reads are reported, look under usage.prompt_tokens_details.cached_tokens in Chat Completions or usage.input_tokens_details.cached_tokens in Responses. Compatible responses may omit writes. Check the console for the final breakdown and charge.
Regular input, output, cache reads, and cache writes each have a price per million tokens. To estimate the charge, multiply each count by its rate, divide by one million, and add the four amounts. Pricing shows current rates for new keys; your console shows the rates for your existing key.
Why did the cache miss?
- Check writes on the first Claude request. If W is zero, verify that automatic caching or an explicit breakpoint is enabled and that the prefix meets the model's minimum cacheable token count.
- Append another turn in the same conversation, preserving the tool definitions, system instructions, and message order. Send it within the cache lifetime, after the first response has started.
- When a Claude turn adds more than 20 content blocks, keep an explicit breakpoint near the previous write. For a one-off task, omit cache_control to avoid paying for a write you will not reuse.