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>"
}
'import requests
url = "https://modelslab.com/api/v7/llm/v1/messages/count_tokens"
payload = {
"model": "<string>",
"messages": [{ "content": "<string>" }],
"system": "<string>"
}
headers = {
"x-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({model: '<string>', messages: [{content: '<string>'}], system: '<string>'})
};
fetch('https://modelslab.com/api/v7/llm/v1/messages/count_tokens', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://modelslab.com/api/v7/llm/v1/messages/count_tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => '<string>',
'messages' => [
[
'content' => '<string>'
]
],
'system' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://modelslab.com/api/v7/llm/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"system\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://modelslab.com/api/v7/llm/v1/messages/count_tokens")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"system\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://modelslab.com/api/v7/llm/v1/messages/count_tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"system\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"input_tokens": 123
}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>"
}
'import requests
url = "https://modelslab.com/api/v7/llm/v1/messages/count_tokens"
payload = {
"model": "<string>",
"messages": [{ "content": "<string>" }],
"system": "<string>"
}
headers = {
"x-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({model: '<string>', messages: [{content: '<string>'}], system: '<string>'})
};
fetch('https://modelslab.com/api/v7/llm/v1/messages/count_tokens', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://modelslab.com/api/v7/llm/v1/messages/count_tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => '<string>',
'messages' => [
[
'content' => '<string>'
]
],
'system' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://modelslab.com/api/v7/llm/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"system\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://modelslab.com/api/v7/llm/v1/messages/count_tokens")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"system\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://modelslab.com/api/v7/llm/v1/messages/count_tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"system\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"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

