Skip to main content
POST
/
v1
/
messages
/
count_tokens
Count Tokens
curl --request POST \
  --url https://modelslab.com/api/v7/llm/v1/messages/count_tokens \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: <api-key>' \
  --data '
{
  "model": "<string>",
  "messages": [
    {
      "role": "user",
      "content": "<string>"
    }
  ],
  "system": "<string>"
}
'
{
  "input_tokens": 123
}

Request

POST https://modelslab.com/api/v7/llm/v1/messages/count_tokens
curl -X POST https://modelslab.com/api/v7/llm/v1/messages/count_tokens \
  -H "x-api-key: $MODELSLAB_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ]
  }'

Body

{
  "model": "Qwen/Qwen2.5-VL-72B-Instruct-together",
  "messages": [
    {"role": "user", "content": "What is the capital of France?"}
  ],
  "system": "You are a helpful assistant."
}

Response

{
  "input_tokens": 15
}

Use Cases

  • Cost estimation: Calculate the cost of a request before sending it
  • Context window management: Ensure your messages fit within the model’s context window
  • Token budgeting: Allocate token budgets across multiple requests

Example

from anthropic import Anthropic

client = Anthropic(
    api_key="YOUR_MODELSLAB_API_KEY",
    base_url="https://modelslab.com/api/v7/llm",
)

# Count tokens before sending
token_count = client.messages.count_tokens(
    model="Qwen/Qwen2.5-VL-72B-Instruct-together",
    messages=[
        {"role": "user", "content": "Write a detailed essay about AI"}
    ],
)

print(f"Input tokens: {token_count.input_tokens}")

Authorizations

x-api-key
string
header
required

API key authentication via x-api-key header

Headers

anthropic-version
string
default:2023-06-01

Anthropic API version

Body

application/json
model
string
required

Model ID to count tokens for

messages
object[]
required

Messages to count tokens for

system
string

System prompt to include in token count

Response

200 - application/json

Token count response

input_tokens
integer

Number of input tokens