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, toolsCount 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
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 cache_control = '{{ cache_control }}'
AND output_config = '{{ output_config }}'
AND system = '{{ system }}'
AND thinking = '{{ thinking }}'
AND tool_choice = '{{ tool_choice }}'
AND tools = '{{ tools }}'
;