token_counts
Creates, updates, deletes, gets or lists a token_counts resource.
Overview
| Name | token_counts |
| Type | Resource |
| Id | anthropic.messages.token_counts |
Fields
The following fields are returned by SELECT queries:
- count_tokens
| Name | Datatype | Description |
|---|---|---|
input_tokens | integer | The total number of tokens across the provided list of messages, system prompt, and tools. |
Methods
The following methods are available for this resource:
| Name | Accessible by | Required Params | Optional Params | Description |
|---|---|---|---|---|
count_tokens | select | messages, model | cache_control, output_config, system, thinking, tool_choice, tools | 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 |
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.
| Name | Datatype | Description |
|---|---|---|
cache_control | string | Top-level cache control automatically applies a cache_control marker to the last cacheable block in the request. |
messages | array | Input messages. |
model | string | The model that will complete your prompt. |
output_config | object | |
system | string | System prompt. |
thinking | object | Configuration for enabling Claude's extended thinking. |
tool_choice | object | 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. |
tools | array | Definitions of tools that the model may use. |
SELECT examples
- count_tokens
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
- Query Shape
- Query Example
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 }}'
;
SELECT
input_tokens
FROM anthropic.messages.token_counts
WHERE model = 'claude-sonnet-5'
AND messages = '[
{
"role": "user",
"content": "how does stackql work?"
}
]'
AND system = 'You are a technical assistant. Answer concisely.'
;