class

Anthropic::Messages

Inherits Reference < Object

Messages API resource

Note: Unlike the Ruby SDK which accepts all tools in a single tools array, this SDK uses separate tools and server_tools parameters. This design:

  • Provides better type safety and IDE autocompletion
  • Automatically manages required beta headers for server tools
  • Is more explicit about tool types being used

Constructors

new(client : Client)
Source

Instance methods

batches

Access batches sub-resource

Source
count_tokens(model : String, messages : Array(MessageParam) | Array(NamedTuple(role: String, content: String)), system : String | Array(TextContent) | Nil = nil, tools : Array(Tool) | Nil = nil, server_tools : Array(ServerTool) | Nil = nil, tool_choice : ToolChoice | Nil = nil, thinking : ThinkingConfig | Nil = nil, cache_control : CacheControl | Nil = nil, output_config : OutputConfig | Nil = nil, inference_geo : String | Nil = nil, user_profile_id : String | Nil = nil, diagnostics : DiagnosticsParam | Nil = nil) : TokenCountResponse

Count tokens for a message without sending it

Useful for estimating costs before making a request.

count = client.messages.count_tokens(
  model: Anthropic::Model::CLAUDE_SONNET_4_6,
  messages: [{role: "user", content: "Hello, Claude!"}],
  system: "You are a helpful assistant."
)
puts "Input tokens: #{count.input_tokens}"
Source
create(model : String, max_tokens : Int32, messages : Array(MessageParam) | Array(NamedTuple(role: String, content: String)), system : String | Array(TextContent) | Nil = nil, temperature : Float64 | Nil = nil, top_p : Float64 | Nil = nil, top_k : Int32 | Nil = nil, tools : Array(Tool) | Nil = nil, server_tools : Array(ServerTool) | Nil = nil, tool_choice : ToolChoice | Nil = nil, stop_sequences : Array(String) | Nil = nil, metadata : Metadata | Nil = nil, service_tier : String | Nil = nil, thinking : ThinkingConfig | Nil = nil, cache_control : CacheControl | Nil = nil, container : String | Nil = nil, output_config : OutputConfig | Nil = nil, inference_geo : String | Nil = nil, fallbacks : FallbacksParam | Nil = nil, fallback_credit_token : FallbackCreditToken | Nil = nil, user_profile_id : String | Nil = nil, extra_headers : Hash(String, String) | Nil = nil, diagnostics : DiagnosticsParam | Nil = nil) : Message

Create a message (non-streaming)

# Basic message
message = client.messages.create(
  model: Anthropic::Model::CLAUDE_SONNET_4_6,
  max_tokens: 1024,
  messages: [{role: "user", content: "Hello!"}]
)

# With extended thinking
message = client.messages.create(
  model: Anthropic::Model::CLAUDE_SONNET_4_6,
  max_tokens: 4096,
  thinking: Anthropic::ThinkingConfig.enabled(budget_tokens: 2000),
  messages: [{role: "user", content: "Solve this problem..."}]
)

# With web search
message = client.messages.create(
  model: Anthropic::Model::CLAUDE_SONNET_4_6,
  max_tokens: 1024,
  tools: [Anthropic::WebSearchTool.new],
  messages: [{role: "user", content: "What's the latest AI news?"}]
)
Source
open_stream(model : String, max_tokens : Int32, messages : Array(MessageParam) | Array(NamedTuple(role: String, content: String)), system : String | Array(TextContent) | Nil = nil, temperature : Float64 | Nil = nil, top_p : Float64 | Nil = nil, top_k : Int32 | Nil = nil, tools : Array(Tool) | Nil = nil, server_tools : Array(ServerTool) | Nil = nil, tool_choice : ToolChoice | Nil = nil, stop_sequences : Array(String) | Nil = nil, metadata : Metadata | Nil = nil, service_tier : String | Nil = nil, thinking : ThinkingConfig | Nil = nil, cache_control : CacheControl | Nil = nil, container : String | Nil = nil, output_config : OutputConfig | Nil = nil, inference_geo : String | Nil = nil, fallbacks : FallbacksParam | Nil = nil, fallback_credit_token : FallbackCreditToken | Nil = nil, user_profile_id : String | Nil = nil, extra_headers : Hash(String, String) | Nil = nil, diagnostics : DiagnosticsParam | Nil = nil, &)

Open a streaming response and yield a richer stream helper object.

Source
stream(model : String, max_tokens : Int32, messages : Array(MessageParam) | Array(NamedTuple(role: String, content: String)), system : String | Array(TextContent) | Nil = nil, temperature : Float64 | Nil = nil, top_p : Float64 | Nil = nil, top_k : Int32 | Nil = nil, tools : Array(Tool) | Nil = nil, server_tools : Array(ServerTool) | Nil = nil, tool_choice : ToolChoice | Nil = nil, stop_sequences : Array(String) | Nil = nil, metadata : Metadata | Nil = nil, service_tier : String | Nil = nil, thinking : ThinkingConfig | Nil = nil, cache_control : CacheControl | Nil = nil, container : String | Nil = nil, output_config : OutputConfig | Nil = nil, inference_geo : String | Nil = nil, fallbacks : FallbacksParam | Nil = nil, fallback_credit_token : FallbackCreditToken | Nil = nil, user_profile_id : String | Nil = nil, extra_headers : Hash(String, String) | Nil = nil, diagnostics : DiagnosticsParam | Nil = nil, &)

Stream a message with individual events

client.messages.stream(
  model: Anthropic::Model::CLAUDE_SONNET_4_6,
  max_tokens: 1024,
  messages: [{role: "user", content: "Tell me a story"}]
) do |event|
  case event
  when Anthropic::ContentBlockDeltaEvent
    print event.text
  end
end
Source