Functions

chat

fn (model: Str, message: Str): Str
fn (model: Str, message: Str, system: Str): Str
fn (model: Str, message: Str, system: Str?, max-tokens: Int?): Str
fn (model: Str, message: Str, opts: Map): Str
fn (model: Str, message: Str, system: Str?, opts: Map): Str
fn (model: Str, message: Str, system: Str?, max-tokens: Int?, opts: Map): Str

Simple chat - send a message and get a response string.

Example

response ::anthropic::messages/chat("claude-sonnet-4-20250514", "What is the capital of France?")
// Returns: "The capital of France is Paris."

// With system prompt
response ::anthropic::messages/chat("claude-sonnet-4-20250514", "Hello!", "You are a helpful assistant.")

// With a per-call API key
response ::anthropic::messages/chat("claude-sonnet-4-20250514", "Hello!", {api-key: "tenant-key"})

post

fn (request: MessagesPostRequest): MessagesPostResponse
fn (request: MessagesPostRequest, opts: Map): MessagesPostResponse

POST /v1/messages - Create a message.

Send a structured list of input messages and receive a model-generated response. Supports system prompts, multi-turn conversations, tool use, temperature control, stop sequences, and token limits.

Example

// Simple message
request MessagesPostRequest({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    messages: [{role: "user", content: "What is the capital of France?"}]
})
response ::anthropic::messages/post(request)
response.content[0].text // => "The capital of France is Paris."

// With system prompt
request MessagesPostRequest({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    system: "You are a helpful assistant that responds in JSON format.",
    messages: [{role: "user", content: "What is 2+2?"}]
})
response ::anthropic::messages/post(request)

// Multi-turn conversation
request MessagesPostRequest({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    messages: [
        {role: "user", content: "My name is Alice."},
        {role: "assistant", content: "Nice to meet you, Alice!"},
        {role: "user", content: "What is my name?"}
    ]
})
response ::anthropic::messages/post(request)

// With tool use
request MessagesPostRequest({
    model: "claude-sonnet-4-20250514",
    max_tokens: 200,
    tools: [
        {
            name: "get_weather",
            description: "Get the current weather for a location",
            input_schema: {
                type: "object",
                properties: {
                    location: {type: "string", description: "City and state"}
                },
                required: ["location"]
            }
        }
    ],
    messages: [{role: "user", content: "What's the weather in San Francisco?"}]
})
response ::anthropic::messages/post(request)
// response.stop_reason => "tool_use" when the model invokes a tool

post-stream

fn (request: MessagesPostRequest): StreamingMessagesResponse
fn (request: MessagesPostRequest, opts: Map): StreamingMessagesResponse

POST /v1/messages with streaming - yields SSE events as an iterator.

Use this for real-time streaming of AI responses. The body is an iterator that yields parsed SSE events from Anthropic's streaming API.

Event Types

  • message_start - Initial message metadata
  • content_block_start - Start of a content block
  • content_block_delta - Text chunk (delta.text contains the text)
  • content_block_stop - End of a content block
  • message_delta - Final message metadata (stop_reason, usage)
  • message_stop - End of message
  • error - The API stopped partway (an overload, say). Nothing follows it

A refused call has no stream, so it returns an Err carrying HttpError, as post does. Its body holds the API's reason.

Example

request ::anthropic::messages/MessagesPostRequest({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    messages: [{role: "user", content: "Hello!"}]
})

response ::anthropic::messages/post-stream(request)
// response.body is an iterator of SSE events
events collect(response.body)
text-events filter(events, eq(%.event, "content_block_delta"))
stop-events filter(events, eq(%.event, "message_stop"))

Types

ContentBlock

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

CreateMessageParams

CreateMessageParams type {
    model: Model,
    messages: Vec<InputMessage>,
    max_tokens: Int,
    metadata: Any?,
    stop_sequences: Vec<Str>?,
    stream: Bool?,
    system: Any?,
    temperature: Dec?,
    tool_choice: ToolChoice?,
    tools: Vec<Tool>?,
    top_k: Int?,
    top_p: Dec?,
    output_config: Map?,
    thinking: Map?
}

Delta

Delta type {
    type: DeltaType?,
    text: Str?,
    partial_json: Str?,
    stop_reason: StopReason?,
    stop_sequence: Str?
}

DeltaType

DeltaType type "text_delta" | "input_json_delta"

ErrorResponse

ErrorResponse type {
    type: Str,
    error: Any
}

InputMessage

InputMessage type {
    role: Role,
    content: Any
}

Message

Message type {
    id: Str,
    type: Str,
    role: Role,
    content: Vec<ContentBlock>,
    model: Model,
    stop_reason: StopReason?,
    stop_sequence: Str?,
    usage: Any
}

MessageStreamEvent

MessageStreamEvent type {
    type: StreamEventType,
    message: Message?,
    index: Int?,
    content_block: ContentBlock?,
    delta: Delta?,
    usage: Any?
}

MessagesPostRequest

MessagesPostRequest type {
    model: Model,
    messages: Vec<InputMessage>,
    max_tokens: Int,
    metadata: Any?,
    stop_sequences: Vec<Str>?,
    stream: Bool?,
    system: Any?,
    temperature: Dec?,
    tool_choice: ToolChoice?,
    tools: Vec<Tool>?,
    top_k: Int?,
    top_p: Dec?,
    output_config: Map?,
    thinking: Map?,
    cache_control: Map?
}

MessagesPostResponse

MessagesPostResponse type {
    id: Str,
    type: Str,
    role: Role,
    content: Vec<ContentBlock>,
    model: Model,
    stop_reason: StopReason?,
    stop_sequence: Str?,
    usage: Any
}

Model

Model type Str

Role

Role type "user" | "assistant"

StopReason

StopReason type "end_turn" | "max_tokens" | "stop_sequence" | "tool_use"

StreamEventType

StreamEventType type "message_start" | "content_block_start" | "content_block_delta" | "content_block_stop" | "message_delta" | "message_stop" | "ping" | "error"

StreamingMessagesResponse

StreamingMessagesResponse type {
    status: Int,
    headers: Map<Str, Str>,
    body: Any
}

Tool

Tool type {
    description: Str?,
    name: Str,
    input_schema: Any
}

ToolChoice

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