Documentation

Your familiar SDK. A clear path from API key to first response.

Prompt caching

Choose a guide or find an API method. Copy examples to your project; this page does not execute requests.

Prompt caching

Reuse repeated input where the selected model supports caching.

Claude caching for direct API requests

For POST /v1/messages, put your reusable instructions in a system text block and set cache_control to {"type":"ephemeral"} on the last stable block. Put the changing question in messages after the breakpoint. Keep any tools before the breakpoint unchanged too. The prefix must be long enough for the model to cache it.

Stable sessions for independent questions

If a new question rewrites the same system prefix, reuse a stable metadata.user_id for that series. This option was verified on claude-opus-5-5 and claude-sonnet-5-5. The value is a JSON-encoded string: create it once per logical session, outside the request loop. Use separate IDs for unrelated users or agent runs and never include personal data. This option helps on some routes and is not required by the official Anthropic API.

Python
Show full configuration (26 lines)
import json
import secrets
import uuid

# Create once per logical session; keep it outside the request loop.
def new_session_metadata():
    return {"user_id": json.dumps({
    "device_id": secrets.token_hex(32),
    "account_uuid": "",
    "session_id": str(uuid.uuid4()),
    }, separators=(",", ":"))}

def request_body(stable_instructions, question, session_metadata):
    return {
        "model": "claude-opus-5-5",
        "max_tokens": 512,
        "metadata": session_metadata,
        "system": [{
            "type": "text",
            "text": stable_instructions,
            "cache_control": {"type": "ephemeral"},
        }],
        "messages": [{"role": "user", "content": question}],
    }

# Once per session: metadata = new_session_metadata()

Verify a cache hit

Send the first real request on its own and wait for its response before sending a different question with the same prefix and ID. A positive cache_read_input_tokens confirms a hit; the response can also report cache writes for new blocks. When starting several requests concurrently on a cold prefix, more than one may pay for a write. Cache expiry or a route change can also cause a miss.

Using an existing client

This example is for scripts, SDK integrations and automation that build their own Messages requests. In clients such as OpenCode, Zcode or Claude Code, keep the app's built-in handling of sessions and cache controls. You do not need to inject or overwrite its metadata.

Verify availability

Use caching only when it is listed for the selected model and protocol. A cache hint does not guarantee a cache hit.

Cache usage

In Messages, input_tokens excludes cache_read_input_tokens and cache_creation_input_tokens. Cache creation can be split into cache_creation.ephemeral_5m_input_tokens and ephemeral_1h_input_tokens; the split adds up to the aggregate creation count. Count each category once. Streamed usage updates are cumulative snapshots, not independent token increments.

Cache write pricing

Writing a new cached prefix is billable even when later requests achieve a high cache hit rate. For default Opus 5.5, prices per million tokens are $1 input, $5 output, $0.05 cache read, $1.25 cache write for 5 minutes and $2 cache write for 1 hour. TTL requests and hints do not guarantee cache reuse.

Haiku context pricing

For Haiku 5.5, total input includes ordinary input, cache reads and cache writes. Above 100,000 input tokens, all token rates are multiplied by five for that request, including output. Opus 5.5 has no such threshold in the current Quanteex tariff.

Client cost estimates

Clients may calculate their own cost from local price settings. Match the model, commercial tier, cache-write TTL and any context threshold to the dashboard. Compare the same requests and period when checking a session total against account spending.

Pi 1.3.0 prices and configuration

Repeated prompts

Keep shared prompt prefixes consistent and follow the caching fields documented for your selected model. Cached tokens are visible in usage and priced according to the model catalogue.

Search documentation