⚡ NEW: Flux Klein 9B — Faster inference, stunning quality · Try Now
cURL
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 }
Count the number of tokens in a message before sending it. Useful for cost estimation and context window management.
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?"} ] }'
{ "model": "Qwen/Qwen2.5-VL-72B-Instruct-together", "messages": [ {"role": "user", "content": "What is the capital of France?"} ], "system": "You are a helpful assistant." }
{ "input_tokens": 15 }
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}")
API key authentication via x-api-key header
Anthropic API version
Model ID to count tokens for
Messages to count tokens for
Show child attributes
System prompt to include in token count
Token count response
Number of input tokens
Was this page helpful?