Claudio.Messages.Request (Claudio v0.5.0)

View Source

Builder for constructing Messages API requests.

Example

alias Claudio.Messages.Request

Request.new("claude-sonnet-4-5-20250929")
|> Request.add_message(:user, "Hello!")
|> Request.set_system("You are a helpful assistant")
|> Request.set_max_tokens(1024)
|> Request.set_temperature(0.7)
|> Request.to_map()

Summary

Functions

Adds MCP (Model Context Protocol) server definitions.

Adds a message to the conversation.

Adds a text message with a document from the Files API.

Adds a text message with an image from a base64-encoded string.

Adds a text message with an image from a URL.

Adds a tool definition.

Adds a tool definition with prompt caching enabled.

Enables streaming responses.

Enables extended thinking with optional budget.

Creates a new request builder with the specified model.

Sets container identifier for tool reuse.

Sets context management configuration.

Sets the maximum number of tokens to generate.

Sets request metadata.

Sets service tier for capacity selection.

Sets custom stop sequences.

Sets the system prompt.

Sets the system prompt with prompt caching enabled.

Sets the temperature (0.0-1.0).

Sets tool choice strategy.

Sets top_k for sampling from top K options.

Sets top_p for nucleus sampling (0.0-1.0).

Converts the request to a map suitable for the API.

Types

content()

@type content() :: String.t() | [map()]

role()

@type role() :: :user | :assistant

t()

@type t() :: %Claudio.Messages.Request{
  container: String.t() | map() | nil,
  context_management: map() | nil,
  max_tokens: integer() | nil,
  mcp_servers: [map()] | nil,
  messages: [map()],
  metadata: map() | nil,
  model: String.t(),
  service_tier: String.t() | nil,
  stop_sequences: [String.t()] | nil,
  stream: boolean() | nil,
  system: String.t() | list() | nil,
  temperature: float() | nil,
  thinking: map() | nil,
  tool_choice: map() | nil,
  tools: [map()] | nil,
  top_k: integer() | nil,
  top_p: float() | nil
}

tool_choice()

@type tool_choice() :: :auto | :any | {:tool, String.t()} | :none

Functions

add_mcp_server(request, server)

@spec add_mcp_server(t(), Claudio.MCP.ServerConfig.t() | map()) :: t()

Adds MCP (Model Context Protocol) server definitions.

Example

Request.new("claude-sonnet-4-5-20250929")
|> Request.add_mcp_server(%{
  "name" => "my_server",
  "url" => "http://localhost:8080"
})

add_message(request, role, content)

@spec add_message(t(), role(), content()) :: t()

Adds a message to the conversation.

Content can be:

  • A string for simple text messages
  • A list of content blocks for multimodal messages (text, images, documents)

Examples

# Simple text message
Request.new("claude-3-5-sonnet-20241022")
|> Request.add_message(:user, "What is the weather?")

# Multimodal message with image
Request.new("claude-3-5-sonnet-20241022")
|> Request.add_message(:user, [
  %{"type" => "image", "source" => %{
    "type" => "base64",
    "media_type" => "image/jpeg",
    "data" => base64_image
  }},
  %{"type" => "text", "text" => "What's in this image?"}
])

add_message_with_document(request, role, text, file_id)

@spec add_message_with_document(t(), role(), String.t(), String.t()) :: t()

Adds a text message with a document from the Files API.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.add_message_with_document(:user, "Summarize this document", "file_abc123")

add_message_with_image(request, role, text, base64_data, media_type \\ "image/jpeg")

@spec add_message_with_image(t(), role(), String.t(), String.t(), String.t()) :: t()

Adds a text message with an image from a base64-encoded string.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.add_message_with_image(:user, "What's in this image?", base64_data, "image/jpeg")

add_message_with_image_url(request, role, text, image_url)

@spec add_message_with_image_url(t(), role(), String.t(), String.t()) :: t()

Adds a text message with an image from a URL.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.add_message_with_image_url(:user, "What's in this image?", "https://example.com/image.jpg")

add_tool(request, tool)

@spec add_tool(t(), map()) :: t()

Adds a tool definition.

Example

tool = %{
  "name" => "get_weather",
  "description" => "Get weather for a location",
  "input_schema" => %{
    "type" => "object",
    "properties" => %{
      "location" => %{"type" => "string"}
    },
    "required" => ["location"]
  }
}

Request.new("claude-3-5-sonnet-20241022")
|> Request.add_tool(tool)

add_tool_with_cache(request, tool, opts \\ [])

@spec add_tool_with_cache(t(), map(), keyword()) :: t()

Adds a tool definition with prompt caching enabled.

Useful when you have many tool definitions and want to cache them.

Example

tool = %{
  "name" => "get_weather",
  "description" => "Get weather for a location",
  "input_schema" => %{"type" => "object", "properties" => %{}}
}

Request.new("claude-3-5-sonnet-20241022")
|> Request.add_tool_with_cache(tool)

enable_streaming(request)

@spec enable_streaming(t()) :: t()

Enables streaming responses.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.enable_streaming()

enable_thinking(request, config)

@spec enable_thinking(t(), map()) :: t()

Enables extended thinking with optional budget.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.enable_thinking(%{"type" => "enabled", "budget_tokens" => 1000})

new(model)

@spec new(String.t()) :: t()

Creates a new request builder with the specified model.

Example

Request.new("claude-sonnet-4-5-20250929")

set_container(request, container)

@spec set_container(t(), String.t() | map()) :: t()

Sets container identifier for tool reuse.

Allows tools to maintain state across requests.

Example

# String container ID
Request.new("claude-3-5-sonnet-20241022")
|> Request.set_container("my-container-123")

# Container config object
Request.new("claude-3-5-sonnet-20241022")
|> Request.set_container(%{
  "id" => "my-container",
  "ttl" => 3600
})

set_context_management(request, config)

@spec set_context_management(t(), map()) :: t()

Sets context management configuration.

Controls how context is managed across requests.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_context_management(%{
  "strategy" => "auto",
  "max_context_tokens" => 100000
})

set_max_tokens(request, max_tokens)

@spec set_max_tokens(t(), integer()) :: t()

Sets the maximum number of tokens to generate.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_max_tokens(1024)

set_metadata(request, metadata)

@spec set_metadata(t(), map()) :: t()

Sets request metadata.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_metadata(%{"user_id" => "123"})

set_service_tier(request, tier)

@spec set_service_tier(t(), String.t()) :: t()

Sets service tier for capacity selection.

Options:

  • "auto" - Automatically select based on availability
  • "standard_only" - Only use standard tier capacity

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_service_tier("auto")

set_stop_sequences(request, sequences)

@spec set_stop_sequences(t(), [String.t()]) :: t()

Sets custom stop sequences.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_stop_sequences(["END", "STOP"])

set_system(request, system)

@spec set_system(t(), String.t() | list()) :: t()

Sets the system prompt.

Can be a string or a list of content blocks with optional cache_control.

Examples

# Simple string
Request.new("claude-3-5-sonnet-20241022")
|> Request.set_system("You are a helpful assistant")

# With prompt caching
Request.new("claude-3-5-sonnet-20241022")
|> Request.set_system([
  %{
    "type" => "text",
    "text" => "Long system prompt here...",
    "cache_control" => %{"type" => "ephemeral"}
  }
])

set_system_with_cache(request, text, opts \\ [])

@spec set_system_with_cache(t(), String.t(), keyword()) :: t()

Sets the system prompt with prompt caching enabled.

Options

  • :ttl - Cache duration, either "5m" (default) or "1h"

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_system_with_cache("Long system prompt...", ttl: "1h")

set_temperature(request, temperature)

@spec set_temperature(t(), float()) :: t()

Sets the temperature (0.0-1.0).

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_temperature(0.7)

set_tool_choice(request, arg2)

@spec set_tool_choice(t(), tool_choice()) :: t()

Sets tool choice strategy.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_tool_choice(:auto)
|> Request.set_tool_choice(:any)
|> Request.set_tool_choice({:tool, "get_weather"})
|> Request.set_tool_choice(:none)

set_top_k(request, top_k)

@spec set_top_k(t(), integer()) :: t()

Sets top_k for sampling from top K options.

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_top_k(40)

set_top_p(request, top_p)

@spec set_top_p(t(), float()) :: t()

Sets top_p for nucleus sampling (0.0-1.0).

Example

Request.new("claude-3-5-sonnet-20241022")
|> Request.set_top_p(0.9)

to_map(request)

@spec to_map(t()) :: map()

Converts the request to a map suitable for the API.