Light Dark

Functions

beta-messages-post

fn (request: PromptCachingBetaMessagesPostRequest): PromptCachingBetaMessagesPostResponse

POST /v1/messages (prompt caching beta) - Create a message with prompt caching.

Cache portions of your prompt for reuse across requests. Particularly beneficial for long system prompts, reducing latency and costs on repeated calls. Use cache_control: {type: "ephemeral"} on content blocks you want to cache.

Example

// Basic prompt caching with a system prompt
request PromptCachingBetaMessagesPostRequest({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    system: [
        {
            type: "text",
            text: "You are a helpful assistant that responds concisely.",
            cache_control: {type: "ephemeral"}
        }
    ],
    messages: [{role: "user", content: "Say hello!"}]
})
response ::anthropic::prompt::caching/beta-messages-post(request)
response.content[0].text

// Multi-turn conversation with cached system prompt
request PromptCachingBetaMessagesPostRequest({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    system: [
        {
            type: "text",
            text: "You are a math tutor. Be concise.",
            cache_control: {type: "ephemeral"}
        }
    ],
    messages: [
        {role: "user", content: "What is 2+2?"},
        {role: "assistant", content: "4"},
        {role: "user", content: "And 3+3?"}
    ]
})

Types

ContentBlock

ContentBlock type {
    type: Str,
    text: Str?,
    id: Str?,
    name: Str?,
    input: Any?
}

ErrorResponse

ErrorResponse type {
    type: "error",
    error: Any
}

Model

Model type Str

PromptCachingBetaCreateMessageParams

PromptCachingBetaCreateMessageParams type {
    model: Model,
    messages: Vec,
    max_tokens: Int,
    metadata: Any?,
    stop_sequences: Vec?,
    stream: Bool?,
    system: Any?,
    temperature: Dec?,
    tool_choice: ToolChoice?,
    tools: Vec?,
    top_k: Int?,
    top_p: Dec?
}

PromptCachingBetaInputMessage

PromptCachingBetaInputMessage type {
    role: "user" | "assistant",
    content: Any
}

PromptCachingBetaMessage

PromptCachingBetaMessage type {
    id: Str,
    type: "message",
    role: "assistant",
    content: Vec,
    model: Model,
    stop_reason: Any,
    stop_sequence: Any,
    usage: Any
}

PromptCachingBetaMessagesPostRequest

PromptCachingBetaMessagesPostRequest type {
    model: Model,
    messages: Vec,
    max_tokens: Int,
    metadata: Any?,
    stop_sequences: Vec?,
    stream: Bool?,
    system: Any?,
    temperature: Dec?,
    tool_choice: ToolChoice?,
    tools: Vec?,
    top_k: Int?,
    top_p: Dec?
}

PromptCachingBetaMessagesPostResponse

PromptCachingBetaMessagesPostResponse type {
    id: Str,
    type: "message",
    role: "assistant",
    content: Vec,
    model: Model,
    stop_reason: Any,
    stop_sequence: Any,
    usage: Any
}

PromptCachingBetaTool

PromptCachingBetaTool type {
    description: Str?,
    name: Str,
    input_schema: Any,
    cache_control: Any?
}

ToolChoice

ToolChoice type {
    type: Str,
    name: Str?,
    disable_parallel_tool_use: Bool?
}