Skip to main content

Prompt Cache

Prompt caching is a powerful feature that optimizes your API usage by allowing resuming from specific prefixes in your prompts. This approach significantly reduces processing time and costs for repetitive tasks or prompts with consistent elements. Acontext will automatically store the prompt cache flag in the message metadata.
Python
client.sessions.send_message(
    session_id="session-uuid",
    blob={
        "role": "user",
        "content": [
            {
                type": "text",
                "text": "<the entire contents of Pride and Prejudice>",
                "cache_control": {"type": "ephemeral"}
            }
        ]
    },
    format="anthropic"
)
# ...
messages = client.sessions.get_messages(session_id="session-uuid")
# has cache_control flag in the message metadata