For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Contact usJoin the Discord
ManualAPI GuideAPI Reference
  • Overview
    • API Reference
    • Client libraries
  • API Reference
      • POSTMessages
      • POSTCount tokens in a Message
      • POSTValidate Messages Request
LogoLogo
Contact usJoin the Discord
API ReferenceMessages

Count tokens in a Message

POST
https://host.com/v1/messages/count_tokens
POST
/v1/messages/count_tokens
$curl -X POST https://host.com/v1/messages/count_tokens \
> -H "Content-Type: application/json" \
> -d '{
> "model": "default",
> "messages": [
> {
> "role": "user",
> "content": "Count tokens for this input."
> }
> ],
> "system": [
> {
> "text": "You are a tokenizer."
> }
> ],
> "tools": [
> {
> "name": "get_weather",
> "description": "Get current weather for a city.",
> "inputSchema": {
> "properties": {
> "city": {
> "description": "City name.",
> "type": "string"
> }
> },
> "required": [
> "city"
> ],
> "type": "object"
> }
> }
> ],
> "thinking": {
> "enabled": false
> },
> "tool_choice": {
> "type": "auto",
> "disable_parallel_tool_use": false,
> "validation_mode": "lazy"
> }
>}'
1{
2 "input_tokens": 1
3}
Count tokens in a Message
Was this page helpful?
Previous

Validate Messages Request

Next
Built with

Request

This endpoint expects an object.
modelstringRequiredDefaults to default
Model identifier or alias.
messageslist of objectsRequired
Conversation messages for the request.
systemlist of objectsOptional

System prompt input. Accepts str, list[str], System, list[System], or null. It is normalized internally to list[System].

toolslist of objectsOptional
Optional tool definitions.
thinkingobjectOptional
Thinking configuration.
tool_choiceobjectOptional
Tool selection policy.
output_configobjectOptional
Optional output configuration options.
cache_controlobjectOptional

Optional request-level cache control.

Response

This endpoint returns an object.
input_tokensinteger
Estimated number of input tokens for the provided payload.

Errors

401
Unauthorized
422
Unprocessable Entity