Skip to main content

token_counts

Creates, updates, deletes, gets or lists a token_counts resource.

Overview​

Nametoken_counts
TypeResource
Idanthropic.messages.token_counts

Fields​

The following fields are returned by SELECT queries:

NameDatatypeDescription
input_tokensintegerThe total number of tokens across the provided list of messages, system prompt, and tools.

Methods​

The following methods are available for this resource:

NameAccessible byRequired ParamsOptional ParamsDescription
count_tokensselectmessages, modelcache_control, output_config, system, thinking, tool_choice, tools, anthropic-workspace-idCount 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

Parameters​

Parameters can be passed in the WHERE clause of a query. Check the Methods section to see which parameters are required or optional for each operation.

NameDatatypeDescription
anthropic-workspace-idstringOptional 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. (example: wrkspc_011CZkZaBF1tNoB5wlCeusgy)
cache_controlstringTop-level cache control automatically applies a cache_control marker to the last cacheable block in the request.
messagesarrayInput messages.
modelstringThe model that will complete your prompt.
output_configobject
systemstringSystem prompt.
thinkingobjectConfiguration for enabling Claude's extended thinking.
tool_choiceobjectHow 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.
toolsarrayDefinitions of tools that the model may use.

SELECT examples​

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

SELECT
input_tokens
FROM anthropic.messages.token_counts
WHERE messages = '{{ messages }}' -- required
AND model = '{{ model }}' -- required
AND "anthropic-workspace-id" = '{{ anthropic-workspace-id }}'
AND cache_control = '{{ cache_control }}'
AND output_config = '{{ output_config }}'
AND system = '{{ system }}'
AND thinking = '{{ thinking }}'
AND tool_choice = '{{ tool_choice }}'
AND tools = '{{ tools }}'
;