Class: Anthropic::Models::Beta::MessageCountTokensParams

Inherits:
Internal::Type::BaseModel show all
Extended by:
Internal::Type::RequestParameters::Converter
Includes:
Internal::Type::RequestParameters
Defined in:
lib/anthropic/models/beta/message_count_tokens_params.rb,
sig/anthropic/models/beta/message_count_tokens_params.rbs

Overview

See Also:

  • Anthropic::Resources::Beta::Messages#count_tokens

Defined Under Namespace

Modules: Speed, System, Tool

Instance Attribute Summary collapse

Attributes included from Internal::Type::RequestParameters

#request_options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal::Type::RequestParameters::Converter

dump_request

Methods included from Internal::Type::RequestParameters

included

Methods inherited from Internal::Type::BaseModel

==, #==, #[], coerce, #deconstruct_keys, #deep_to_h, dump, fields, hash, #hash, inherited, inspect, #inspect, known_fields, optional, recursively_to_h, required, #to_h, #to_json, #to_s, to_sorbet_type, #to_yaml

Methods included from Internal::Type::Converter

#coerce, coerce, #dump, dump, inspect, #inspect, meta_info, new_coerce_state, type_info

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(messages:, model:, billing_policy: nil, cache_control: nil, compaction: 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: {}) ⇒ MessageCountTokensParams

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

Parameters:



# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 309

Instance Attribute Details

#betas ⇒ Array<Symbol, String, Anthropic::Models::AnthropicBeta>?

Optional header to specify the beta version(s) you want to use.

Parameters:

  • (::Array[Anthropic::Models::anthropic_beta])

Returns:



289
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 289

optional :betas, -> { Anthropic::Internal::Type::ArrayOf[union: Anthropic::AnthropicBeta] }

#billing_policy ⇒ Anthropic::Models::Beta::BetaBillingPolicy?

Declares which organization is billed when this body is sent to /v1/messages. Accepted here so that you can count the tokens of the exact body you will send, and otherwise ignored: counting tokens bills nobody, so no user profile is required.

Parameters:

  • value (Anthropic::Beta::BetaBillingPolicy, nil)

Returns:



101
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 101

optional :billing_policy, -> { Anthropic::Beta::BetaBillingPolicy }, nil?: true

#cache_control ⇒ Anthropic::Models::Beta::BetaCacheControlEphemeral?

Top-level cache control automatically applies a cache_control marker to the last cacheable block in the request.

Parameters:

  • value (Anthropic::Beta::BetaCacheControlEphemeral, nil)

Returns:



108
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 108

optional :cache_control, -> { Anthropic::Beta::BetaCacheControlEphemeral }, nil?: true

#compaction ⇒ Anthropic::Models::Beta::BetaCompactionConfig?

Compaction configuration.

When set on POST /v1/messages, the request is a compaction request: the conversation in messages is summarized and the response holds only the resulting compaction block (stop_reason "compaction"), which later requests send first in messages in place of the messages it summarizes. POST /v1/messages/count_tokens accepts this parameter and ignores it: the count it returns is for the conversation in messages as sent. Cannot be combined with context_management.

Parameters:

  • value (Anthropic::Beta::BetaCompactionConfig, nil)

Returns:



122
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 122

optional :compaction, -> { Anthropic::Beta::BetaCompactionConfig }, nil?: true

#context_management ⇒ Anthropic::Models::Beta::BetaContextManagementConfig?

Context management configuration.

This allows you to control how Claude manages context across multiple requests, such as whether to clear function results or not.

Parameters:

  • value (Anthropic::Beta::BetaContextManagementConfig, nil)

Returns:



131
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 131

optional :context_management, -> { Anthropic::Beta::BetaContextManagementConfig }, nil?: true

#mcp_servers ⇒ Array<Anthropic::Models::Beta::BetaRequestMCPServerURLDefinition>?

MCP servers to be utilized in this request

Parameters:

  • (::Array[Anthropic::Beta::BetaRequestMCPServerURLDefinition])

Returns:



137
138
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 137

optional :mcp_servers,
-> { Anthropic::Internal::Type::ArrayOf[Anthropic::Beta::BetaRequestMCPServerURLDefinition] }

#messages ⇒ Array<Anthropic::Models::Beta::BetaUserMessageParam, Anthropic::Models::Beta::BetaAssistantMessageParam, Anthropic::Models::Beta::BetaSystemMessageParam>

Input messages.

Our models are trained to operate on alternating user and assistant conversational turns. When creating a new Message, you specify the prior conversational turns with the messages parameter, and the model then generates the next Message in the conversation. Consecutive user or assistant turns in your request will be combined into a single turn.

Each input message must be an object with a role and content. You can specify a single user-role message, or you can include multiple user and assistant messages.

If the final message uses the assistant role, the response content will continue immediately from the content in that message. This can be used to constrain part of the model's response. This is called prefill. On models that don't support prefill, creating a message that ends with a partial assistant response returns a 400 error. See Prefill not supported.

Example with a single user message:

[{ "role": "user", "content": "Hello, Claude" }]

Example with multiple conversational turns:

[
  { "role": "user", "content": "Hello there." },
  { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" },
  { "role": "user", "content": "Can you explain LLMs in plain English?" }
]

Example with a partially-filled response from Claude, for models that support prefill:

[
  {
    "role": "user",
    "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
  },
  { "role": "assistant", "content": "The best answer is (" }
]

Each input message content may be either a single string or an array of content blocks, where each block has a specific type. Using a string for content is shorthand for an array of one content block of type "text". The following input messages are equivalent:

{ "role": "user", "content": "Hello, Claude" }
{ "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] }

See input examples.

Note that if you want to include a system prompt, you can use the top-level system parameter.

There is a limit of 100,000 messages in a single request.

Parameters:

  • value (::Array[Anthropic::Models::Beta::beta_message_param])

Returns:



83
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 83

required :messages, -> { Anthropic::Internal::Type::ArrayOf[union: Anthropic::Beta::BetaMessageParam] }

#model ⇒ Symbol, ...

The model that will complete your prompt.

See models for additional details and options.

Parameters:

  • value (Anthropic::Models::model)

Returns:



92
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 92

required :model, union: -> { Anthropic::Model }

#output_config ⇒ Anthropic::Models::Beta::BetaOutputConfig?

Configuration options for the model's output, such as the output format.

Parameters:

  • (Anthropic::Beta::BetaOutputConfig)

Returns:



144
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 144

optional :output_config, -> { Anthropic::Beta::BetaOutputConfig }

#output_format ⇒ Anthropic::Models::Beta::BetaJSONOutputFormat?

Deprecated.

Deprecated: Use output_config.format instead. See structured outputs

A schema to specify Claude's output format in responses. This parameter will be removed in a future release.

Parameters:

  • value (Anthropic::Beta::BetaJSONOutputFormat, nil)

Returns:



156
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 156

optional :output_format, -> { Anthropic::Beta::BetaJSONOutputFormat }, nil?: true

#speed ⇒ Symbol, ...

The inference speed mode for this request. "fast" enables high output-tokens-per-second inference.

Some models require the fast-mode-2026-02-01 beta header for any speed value. See fast mode for which models.

Parameters:

  • value (Anthropic::Models::Beta::MessageCountTokensParams::speed, nil)

Returns:



168
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 168

optional :speed, enum: -> { Anthropic::Beta::MessageCountTokensParams::Speed }, nil?: true

#system_ ⇒ String, ...

System prompt.

A system prompt is a way of providing context and instructions to Claude, such as specifying a particular goal or role. See our guide to system prompts.

Parameters:

  • (Anthropic::Models::Beta::MessageCountTokensParams::system_)

Returns:



178
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 178

optional :system_, union: -> { Anthropic::Beta::MessageCountTokensParams::System }, api_name: :system

#thinking ⇒ Anthropic::Models::Beta::BetaThinkingConfigEnabled, ...

Configuration for Claude's thinking.

With {"type": "adaptive"}, Claude decides when and how much to think. With {"type": "enabled"} (manual extended thinking), you set a budget_tokens of at least 1,024. Thinking tokens count toward your max_tokens limit.

Which type values are accepted, and what happens when you omit thinking, depend on the model. See thinking for each model's behavior.



193
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 193

optional :thinking, union: -> { Anthropic::Beta::BetaThinkingConfigParam }

#tool_choice ⇒ Anthropic::Models::Beta::BetaToolChoiceAuto, ...

How the model should use the provided tools. The model can use a specific tool, any available tool, decide by itself, or not use tools at all.



200
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 200

optional :tool_choice, union: -> { Anthropic::Beta::BetaToolChoice }

#tools ⇒ Array<Anthropic::Models::Beta::BetaTool, Anthropic::Models::Beta::BetaToolBash20241022, Anthropic::Models::Beta::BetaToolBash20250124, Anthropic::Models::Beta::BetaCodeExecutionTool20250522, Anthropic::Models::Beta::BetaCodeExecutionTool20250825, Anthropic::Models::Beta::BetaCodeExecutionTool20260120, Anthropic::Models::Beta::BetaCodeExecutionTool20260521, Anthropic::Models::Beta::BetaBrowserToolset20260801, Anthropic::Models::Beta::BetaToolComputerUse20241022, Anthropic::Models::Beta::BetaMemoryTool20250818, Anthropic::Models::Beta::BetaToolComputerUse20250124, Anthropic::Models::Beta::BetaToolTextEditor20241022, Anthropic::Models::Beta::BetaToolComputerUse20251124, Anthropic::Models::Beta::BetaComputerToolset20260801, Anthropic::Models::Beta::BetaToolTextEditor20250124, Anthropic::Models::Beta::BetaToolTextEditor20250429, Anthropic::Models::Beta::BetaToolTextEditor20250728, Anthropic::Models::Beta::BetaWebSearchTool20250305, Anthropic::Models::Beta::BetaWebFetchTool20250910, Anthropic::Models::Beta::BetaWebSearchTool20260209, Anthropic::Models::Beta::BetaWebFetchTool20260209, Anthropic::Models::Beta::BetaWebFetchTool20260309, Anthropic::Models::Beta::BetaWebSearchTool20260318, Anthropic::Models::Beta::BetaWebFetchTool20260318, Anthropic::Models::Beta::BetaAdvisorTool20260301, Anthropic::Models::Beta::BetaToolSearchToolBm25_20251119, Anthropic::Models::Beta::BetaToolSearchToolRegex20251119, Anthropic::Models::Beta::BetaMCPToolset>?

Definitions of tools that the model may use.

If you include tools in your API request, the model may return tool_use content blocks that represent the model's use of those tools. You can then run those tools using the tool input generated by the model and then optionally return results back to the model using tool_result content blocks.

There are two types of tools: client tools and server tools. The behavior described below applies to client tools. For server tools, see their individual documentation as each has its own behavior (e.g., the web search tool).

Each tool definition includes:

  • name: Name of the tool.
  • description: Optional, but strongly-recommended description of the tool.
  • input_schema: JSON schema for the tool input shape that the model will produce in tool_use output content blocks.

For example, if you defined tools as:

[
  {
    "name": "get_stock_price",
    "description": "Get the current stock price for a given ticker symbol.",
    "input_schema": {
      "type": "object",
      "properties": {
        "ticker": {
          "type": "string",
          "description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
        }
      },
      "required": ["ticker"]
    }
  }
]

And then asked the model "What's the S&P 500 at today?", the model might produce tool_use content blocks in the response like this:

[
  {
    "type": "tool_use",
    "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
    "name": "get_stock_price",
    "input": { "ticker": "^GSPC" }
  }
]

You might then run your get_stock_price tool with {"ticker": "^GSPC"} as an input, and return the following back to the model in a subsequent user message:

[
  {
    "type": "tool_result",
    "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
    "content": "259.75 USD"
  }
]

Tools can be used for workflows that include running client-side tools and functions, or more generally whenever you want the model to produce a particular JSON structure of output.

See our guide for more details.

Parameters:

  • (::Array[Anthropic::Models::Beta::MessageCountTokensParams::tool])

Returns:



282
283
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 282

optional :tools,
-> { Anthropic::Internal::Type::ArrayOf[union: Anthropic::Beta::MessageCountTokensParams::Tool] }

#user_profile_id ⇒ String?

The user profile ID to attribute this request to. Use when acting on behalf of a party other than your organization.

Parameters:

  • (String)

Returns:

  • (String, nil)


296
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 296

optional :user_profile_id, String

#workspace_id ⇒ String?

Optional header to select the Workspace for this request. The value is a Workspace ID (for example, wrkspc_011CZkZaBF1tNoB5wlCeusgy).

Only needed for credentials that can act on more than one Workspace. A credential that belongs to a specific Workspace may omit it; if sent, it must match that Workspace.

Parameters:

  • (String)

Returns:

  • (String, nil)


307
# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 307

optional :workspace_id, String

Class Method Details

.values ⇒ Array<Symbol>

Returns:

  • (Array<Symbol>)


# File 'lib/anthropic/models/beta/message_count_tokens_params.rb', line 362

Instance Method Details

#to_hash ⇒ { messages: ::Array[Anthropic::Models::Beta::beta_message_param], model: Anthropic::Models::model, billing_policy: Anthropic::Beta::BetaBillingPolicy?, cache_control: Anthropic::Beta::BetaCacheControlEphemeral?, compaction: Anthropic::Beta::BetaCompactionConfig?, context_management: Anthropic::Beta::BetaContextManagementConfig?, mcp_servers: ::Array[Anthropic::Beta::BetaRequestMCPServerURLDefinition], output_config: Anthropic::Beta::BetaOutputConfig, output_format: Anthropic::Beta::BetaJSONOutputFormat?, speed: Anthropic::Models::Beta::MessageCountTokensParams::speed?, system_: Anthropic::Models::Beta::MessageCountTokensParams::system_, thinking: Anthropic::Models::Beta::beta_thinking_config_param, tool_choice: Anthropic::Models::Beta::beta_tool_choice, tools: ::Array[Anthropic::Models::Beta::MessageCountTokensParams::tool], betas: ::Array[Anthropic::Models::anthropic_beta], user_profile_id: String, workspace_id: String, request_options: Anthropic::RequestOptions }

Returns:

  • ({ messages: ::Array[Anthropic::Models::Beta::beta_message_param], model: Anthropic::Models::model, billing_policy: Anthropic::Beta::BetaBillingPolicy?, cache_control: Anthropic::Beta::BetaCacheControlEphemeral?, compaction: Anthropic::Beta::BetaCompactionConfig?, context_management: Anthropic::Beta::BetaContextManagementConfig?, mcp_servers: ::Array[Anthropic::Beta::BetaRequestMCPServerURLDefinition], output_config: Anthropic::Beta::BetaOutputConfig, output_format: Anthropic::Beta::BetaJSONOutputFormat?, speed: Anthropic::Models::Beta::MessageCountTokensParams::speed?, system_: Anthropic::Models::Beta::MessageCountTokensParams::system_, thinking: Anthropic::Models::Beta::beta_thinking_config_param, tool_choice: Anthropic::Models::Beta::beta_tool_choice, tools: ::Array[Anthropic::Models::Beta::MessageCountTokensParams::tool], betas: ::Array[Anthropic::Models::anthropic_beta], user_profile_id: String, workspace_id: String, request_options: Anthropic::RequestOptions })


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'sig/anthropic/models/beta/message_count_tokens_params.rbs', line 117

def to_hash: -> {
  messages: ::Array[Anthropic::Models::Beta::beta_message_param],
  model: Anthropic::Models::model,
  billing_policy: Anthropic::Beta::BetaBillingPolicy?,
  cache_control: Anthropic::Beta::BetaCacheControlEphemeral?,
  compaction: Anthropic::Beta::BetaCompactionConfig?,
  context_management: Anthropic::Beta::BetaContextManagementConfig?,
  mcp_servers: ::Array[Anthropic::Beta::BetaRequestMCPServerURLDefinition],
  output_config: Anthropic::Beta::BetaOutputConfig,
  output_format: Anthropic::Beta::BetaJSONOutputFormat?,
  speed: Anthropic::Models::Beta::MessageCountTokensParams::speed?,
  system_: Anthropic::Models::Beta::MessageCountTokensParams::system_,
  thinking: Anthropic::Models::Beta::beta_thinking_config_param,
  tool_choice: Anthropic::Models::Beta::beta_tool_choice,
  tools: ::Array[Anthropic::Models::Beta::MessageCountTokensParams::tool],
  betas: ::Array[Anthropic::Models::anthropic_beta],
  user_profile_id: String,
  workspace_id: String,
  request_options: Anthropic::RequestOptions
}