Class: Anthropic::Resources::Beta::Messages

Inherits:
Object
  • Object
show all
Defined in:
lib/anthropic/resources/beta/messages.rb,
lib/anthropic/resources/beta/messages/batches.rb,
sig/anthropic/resources/beta/messages.rbs,
sig/anthropic/resources/beta/messages/batches.rbs

Defined Under Namespace

Classes: Batches

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Messages

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Messages.

Parameters:

API:

  • private



460
461
462
463
# File 'lib/anthropic/resources/beta/messages.rb', line 460

def initialize(client:)
  @client = client
  @batches = Anthropic::Resources::Beta::Messages::Batches.new(client: client)
end

Instance Attribute Details

#batchesAnthropic::Resources::Beta::Messages::Batches (readonly)

Returns:



8
9
10
# File 'lib/anthropic/resources/beta/messages.rb', line 8

def batches
  @batches
end

Instance Method Details

#count_tokens(messages:, model:, cache_control: nil, context_management: nil, mcp_servers: nil, output_config: nil, output_format: nil, speed: nil, system_: nil, thinking: nil, tool_choice: nil, tools: nil, betas: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Models::Beta::BetaMessageTokensCount

Count the number of tokens in a Message.

The Token Count API can be used to count the number of tokens in a Message, including tools, images, and documents, without creating it.

Learn more about token counting in our user guide

Some parameter documentations has been truncated, see Models::Beta::MessageCountTokensParams for more details.

Parameters:

  • Body param: Input messages.

  • Body param: The model that will complete your prompt.

  • Body param: Top-level cache control automatically applies a cache_control marker

  • Body param: Context management configuration.

  • Body param: MCP servers to be utilized in this request

  • Body param: Configuration options for the model's output, such as the output for

  • Body param: Deprecated: Use output_config.format instead. See [structured outp

  • Body param: Inference speed mode. fast provides significantly faster output to

  • Body param: System prompt.

  • Body param: Configuration for enabling Claude's extended thinking.

  • Body param: How the model should use the provided tools. The model can use a spe

  • Body param: Definitions of tools that the model may use.

  • Header param: Optional header to specify the beta version(s) you want to use.

  • Header param: The user profile ID to attribute this request to. Use when acting

  • Header param: Optional header to select the Workspace for this request. The valu

Returns:

See Also:



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/anthropic/resources/beta/messages.rb', line 419

def count_tokens(params)
  parsed, options = Anthropic::Beta::MessageCountTokensParams.dump_request(params)
  Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil, is_beta: true)

  header_params =
    {
      betas: "anthropic-beta",
      user_profile_id: "anthropic-user-profile-id",
      workspace_id: "anthropic-workspace-id"
    }
  @client.request(
    method: :post,
    path: "v1/messages/count_tokens?beta=true",
    headers: parsed.slice(*header_params.keys).transform_keys(header_params),
    body: parsed.except(*header_params.keys),
    model: Anthropic::Beta::BetaMessageTokensCount,
    options: options
  )
end

#create(max_tokens:, messages:, model:, cache_control: nil, container: nil, context_management: nil, diagnostics: nil, fallback_credit_token: nil, fallbacks: nil, inference_geo: nil, mcp_servers: nil, metadata: nil, output_config: nil, output_format: nil, service_tier: nil, speed: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, betas: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Models::Beta::BetaMessage

See Anthropic::Resources::Beta::Messages#stream_raw for streaming counterpart.

Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.

The Messages API can be used for either single queries or stateless multi-turn conversations.

Learn more about the Messages API in our user guide

Some parameter documentations has been truncated, see Models::Beta::MessageCreateParams for more details.

Parameters:

  • Body param: The maximum number of tokens to generate before stopping.

  • Body param: Input messages.

  • Body param: The model that will complete your prompt.

  • Body param: Top-level cache control automatically applies a cache_control marker

  • Body param: Container identifier for reuse across requests.

  • Body param: Context management configuration.

  • Body param: Request-level diagnostics. Currently carries the previous response

  • Body param: The fallback_credit_token from a prior refusal's stop_details.

  • Body param: Opt-in server-side retry on one or more substitute models when the r

  • Body param: Specifies the geographic region for inference processing. If not spe

  • Body param: MCP servers to be utilized in this request

  • Body param: An object describing metadata about the request.

  • Body param: Configuration options for the model's output, such as the output for

  • Body param: Deprecated: Use output_config.format instead. See [structured outp

  • Body param: Determines whether to use priority capacity (if available) or standa

  • Body param: Inference speed mode. fast provides significantly faster output to

  • Body param: Custom text sequences that will cause the model to stop generating.

  • Body param: System prompt.

  • Body param: Amount of randomness injected into the response.

  • Body param: Configuration for enabling Claude's extended thinking.

  • Body param: How the model should use the provided tools. The model can use a spe

  • Body param: Definitions of tools that the model may use.

  • Body param: Only sample from the top K options for each subsequent token.

  • Body param: Use nucleus sampling.

  • Header param: Optional header to specify the beta version(s) you want to use.

  • Header param: The user profile ID to attribute this request to. Use when acting

  • Header param: Optional header to select the Workspace for this request. The valu

Returns:

See Also:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/anthropic/resources/beta/messages.rb', line 104

def create(params)
  parsed, options = Anthropic::Beta::MessageCreateParams.dump_request(params)
  if parsed[:stream]
    message = "Please use `#stream` for the streaming use case."
    raise ArgumentError.new(message)
  end

  warn_thinking_enabled(parsed)

  tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(
    parsed,
    strict: nil,
    is_beta: true
  )

  unwrap = ->(raw) { Anthropic::Helpers::Messages.parse_input_schemas!(raw, tools:, models:) }

  if options.empty? && @client.timeout == Anthropic::Client::DEFAULT_TIMEOUT_IN_SECONDS
    model = parsed[:model]&.to_sym
    max_tokens = parsed[:max_tokens].to_i
    timeout = @client.calculate_nonstreaming_timeout(
      max_tokens,
      Anthropic::Client::MODEL_NONSTREAMING_TOKENS[model]
    )
    options = {timeout: timeout}
  else
    options = {timeout: 600, **options}
  end

  header_params =
    {
      betas: "anthropic-beta",
      user_profile_id: "anthropic-user-profile-id",
      workspace_id: "anthropic-workspace-id"
    }
  @client.request(
    method: :post,
    path: "v1/messages?beta=true",
    headers: parsed.slice(*header_params.keys).transform_keys(header_params),
    body: parsed.except(*header_params.keys),
    model: Anthropic::Beta::BetaMessage,
    unwrap: unwrap,
    options: options
  )
end

#stream(max_tokens:, messages:, model:, cache_control: nil, container: nil, context_management: nil, diagnostics: nil, fallback_credit_token: nil, fallbacks: nil, inference_geo: nil, mcp_servers: nil, metadata: nil, output_config: nil, output_format: nil, service_tier: nil, speed: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, betas: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Streaming::MessageStream

See Anthropic::Resources::Beta::Messages#create for non-streaming counterpart.

Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.

The Messages API can be used for either single queries or stateless multi-turn conversations.

Learn more about the Messages API in our user guide

Some parameter documentations has been truncated, see Models::Beta::MessageCreateParams for more details.

Parameters:

  • Body param: The maximum number of tokens to generate before stopping.

  • Body param: Input messages.

  • Body param: The model that will complete your prompt.

  • Body param: Top-level cache control automatically applies a cache_control marker

  • Body param: Container identifier for reuse across requests.

  • Body param: Context management configuration.

  • Body param: Request-level diagnostics. Currently carries the previous response

  • Body param: The fallback_credit_token from a prior refusal's stop_details.

  • Body param: Opt-in server-side retry on one or more substitute models when the r

  • Body param: Specifies the geographic region for inference processing. If not spe

  • Body param: MCP servers to be utilized in this request

  • Body param: An object describing metadata about the request.

  • Body param: Configuration options for the model's output, such as the output for

  • Body param: Deprecated: Use output_config.format instead. See [structured outp

  • Body param: Determines whether to use priority capacity (if available) or standa

  • Body param: Inference speed mode. fast provides significantly faster output to

  • Body param: Custom text sequences that will cause the model to stop generating.

  • Body param: System prompt.

  • Body param: Amount of randomness injected into the response.

  • Body param: Configuration for enabling Claude's extended thinking.

  • Body param: How the model should use the provided tools. The model can use a spe

  • Body param: Definitions of tools that the model may use.

  • Body param: Only sample from the top K options for each subsequent token.

  • Body param: Use nucleus sampling.

  • Header param: Optional header to specify the beta version(s) you want to use.

  • Header param: The user profile ID to attribute this request to. Use when acting

  • Header param: Optional header to select the Workspace for this request. The valu

Returns:

See Also:



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/anthropic/resources/beta/messages.rb', line 225

def stream(params)
  parsed, options = Anthropic::Models::Beta::MessageCreateParams.dump_request(params)
  unless parsed.fetch(:stream, true)
    message = "Please use `#create` for the non-streaming use case."
    raise ArgumentError.new(message)
  end
  parsed.store(:stream, true)

  warn_thinking_enabled(parsed)

  tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(
    parsed,
    strict: nil,
    is_beta: true
  )

  header_params =
    {
      betas: "anthropic-beta",
      user_profile_id: "anthropic-user-profile-id",
      workspace_id: "anthropic-workspace-id"
    }
  raw_stream = @client.request(
    method: :post,
    path: "v1/messages?beta=true",
    headers: stream_headers(
      "accept" => "text/event-stream",
      "accept-encoding" => "identity",
      **parsed.slice(*header_params.keys)
    ).transform_keys(header_params),
    body: parsed.except(*header_params.keys),
    stream: Anthropic::Internal::Stream,
    model: Anthropic::Beta::BetaRawMessageStreamEvent,
    options: {timeout: 600, **options}
  )
  Anthropic::Streaming::MessageStream.new(raw_stream:, tools:, models:)
end

#stream_raw(max_tokens:, messages:, model:, cache_control: nil, container: nil, context_management: nil, diagnostics: nil, fallback_credit_token: nil, fallbacks: nil, inference_geo: nil, mcp_servers: nil, metadata: nil, output_config: nil, output_format: nil, service_tier: nil, speed: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, betas: nil, user_profile_id: nil, workspace_id: nil, request_options: {}) ⇒ Anthropic::Internal::Stream<Anthropic::Models::Beta::BetaRawMessageStartEvent, Anthropic::Models::Beta::BetaRawMessageDeltaEvent, Anthropic::Models::Beta::BetaRawMessageStopEvent, Anthropic::Models::Beta::BetaRawContentBlockStartEvent, Anthropic::Models::Beta::BetaRawContentBlockDeltaEvent, Anthropic::Models::Beta::BetaRawContentBlockStopEvent>

See Anthropic::Resources::Beta::Messages#create for non-streaming counterpart.

Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.

The Messages API can be used for either single queries or stateless multi-turn conversations.

Learn more about the Messages API in our user guide

Some parameter documentations has been truncated, see Models::Beta::MessageCreateParams for more details.

Parameters:

  • Body param: The maximum number of tokens to generate before stopping.

  • Body param: Input messages.

  • Body param: The model that will complete your prompt.

  • Body param: Top-level cache control automatically applies a cache_control marker

  • Body param: Container identifier for reuse across requests.

  • Body param: Context management configuration.

  • Body param: Request-level diagnostics. Currently carries the previous response

  • Body param: The fallback_credit_token from a prior refusal's stop_details.

  • Body param: Opt-in server-side retry on one or more substitute models when the r

  • Body param: Specifies the geographic region for inference processing. If not spe

  • Body param: MCP servers to be utilized in this request

  • Body param: An object describing metadata about the request.

  • Body param: Configuration options for the model's output, such as the output for

  • Body param: Deprecated: Use output_config.format instead. See [structured outp

  • Body param: Determines whether to use priority capacity (if available) or standa

  • Body param: Inference speed mode. fast provides significantly faster output to

  • Body param: Custom text sequences that will cause the model to stop generating.

  • Body param: System prompt.

  • Body param: Amount of randomness injected into the response.

  • Body param: Configuration for enabling Claude's extended thinking.

  • Body param: How the model should use the provided tools. The model can use a spe

  • Body param: Definitions of tools that the model may use.

  • Body param: Only sample from the top K options for each subsequent token.

  • Body param: Use nucleus sampling.

  • Header param: Optional header to specify the beta version(s) you want to use.

  • Header param: The user profile ID to attribute this request to. Use when acting

  • Header param: Optional header to select the Workspace for this request. The valu

Returns:

See Also:



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/anthropic/resources/beta/messages.rb', line 338

def stream_raw(params)
  parsed, options = Anthropic::Beta::MessageCreateParams.dump_request(params)
  unless parsed.fetch(:stream, true)
    message = "Please use `#create` for the non-streaming use case."
    raise ArgumentError.new(message)
  end
  parsed.store(:stream, true)

  warn_thinking_enabled(parsed)

  Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil, is_beta: true)

  header_params =
    {
      betas: "anthropic-beta",
      user_profile_id: "anthropic-user-profile-id",
      workspace_id: "anthropic-workspace-id"
    }
  @client.request(
    method: :post,
    path: "v1/messages?beta=true",
    headers: stream_headers(
      "accept" => "text/event-stream",
      "accept-encoding" => "identity",
      **parsed.slice(*header_params.keys)
    ).transform_keys(header_params),
    body: parsed.except(*header_params.keys),
    stream: Anthropic::Internal::Stream,
    model: Anthropic::Beta::BetaRawMessageStreamEvent,
    options: {timeout: 600, **options}
  )
end

#tool_runner(params) ⇒ Anthropic::Helpers::Tools::Runner

Parameters:

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/anthropic/resources/beta/messages.rb', line 13

def tool_runner(params)
  params = params.to_h
  warn_thinking_enabled(params)
  max_iterations = params.delete(:max_iterations)
  compaction_control = params.delete(:compaction_control)
  if compaction_control&.dig(:enabled) || compaction_control&.dig("enabled")
    warn(
      "[DEPRECATION] The 'compaction_control' parameter is deprecated and will be removed in a future version. " \
      "Use server-side compaction instead by passing edits: [{ type: 'compact_20260112' }] in the params passed to `tool_runner()`. " \
      "See https://platform.claude.com/docs/en/build-with-claude/compaction",
      category: :deprecated
    )
  end
  Anthropic::Helpers::Tools::Runner.new(@client, params:, max_iterations:, compaction_control:)
end