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": [
{
"content": "<string>"
}
],
"system": "<string>"
}
'{
"input_tokens": 123
}LLM API
Count Tokens
Count the number of tokens in a message before sending it. Useful for cost estimation and context window management.
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": [
{
"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
API key authentication via x-api-key header
Headers
Anthropic API version
Body
application/json
Response
200 - application/json
Token count response
Number of input tokens
Was this page helpful?
⌘I

