Count message tokens (Anthropic)
curl --request POST \
--url https://grid.x402compute.cc/v1/messages/count_tokens \
--header 'Content-Type: application/json' \
--data '
{
"model": "qwen-2.5-32b",
"messages": [
{
"role": "user",
"content": "Hello"
}
],
"system": "<string>",
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": {}
}
],
"tool_choice": {}
}
'import requests
url = "https://grid.x402compute.cc/v1/messages/count_tokens"
payload = {
"model": "qwen-2.5-32b",
"messages": [
{
"role": "user",
"content": "Hello"
}
],
"system": "<string>",
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": {}
}
],
"tool_choice": {}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'qwen-2.5-32b',
messages: [{role: 'user', content: 'Hello'}],
system: '<string>',
tools: [{name: '<string>', description: '<string>', input_schema: {}}],
tool_choice: {}
})
};
fetch('https://grid.x402compute.cc/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://grid.x402compute.cc/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' => 'qwen-2.5-32b',
'messages' => [
[
'role' => 'user',
'content' => 'Hello'
]
],
'system' => '<string>',
'tools' => [
[
'name' => '<string>',
'description' => '<string>',
'input_schema' => [
]
]
],
'tool_choice' => [
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$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://grid.x402compute.cc/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"qwen-2.5-32b\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello\"\n }\n ],\n \"system\": \"<string>\",\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {}\n }\n ],\n \"tool_choice\": {}\n}")
req, _ := http.NewRequest("POST", url, payload)
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://grid.x402compute.cc/v1/messages/count_tokens")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"qwen-2.5-32b\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello\"\n }\n ],\n \"system\": \"<string>\",\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {}\n }\n ],\n \"tool_choice\": {}\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://grid.x402compute.cc/v1/messages/count_tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"qwen-2.5-32b\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello\"\n }\n ],\n \"system\": \"<string>\",\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {}\n }\n ],\n \"tool_choice\": {}\n}"
response = http.request(request)
puts response.read_body{
"input_tokens": 4
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}SGL Grid
Count message tokens (Anthropic)
Estimate the input tokens of a Messages request, including tool definitions. Uses the same estimator the Grid uses for its context check and price quote, not a model tokenizer. Free, no auth needed, rate-limited per IP.
POST
/
v1
/
messages
/
count_tokens
Count message tokens (Anthropic)
curl --request POST \
--url https://grid.x402compute.cc/v1/messages/count_tokens \
--header 'Content-Type: application/json' \
--data '
{
"model": "qwen-2.5-32b",
"messages": [
{
"role": "user",
"content": "Hello"
}
],
"system": "<string>",
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": {}
}
],
"tool_choice": {}
}
'import requests
url = "https://grid.x402compute.cc/v1/messages/count_tokens"
payload = {
"model": "qwen-2.5-32b",
"messages": [
{
"role": "user",
"content": "Hello"
}
],
"system": "<string>",
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": {}
}
],
"tool_choice": {}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'qwen-2.5-32b',
messages: [{role: 'user', content: 'Hello'}],
system: '<string>',
tools: [{name: '<string>', description: '<string>', input_schema: {}}],
tool_choice: {}
})
};
fetch('https://grid.x402compute.cc/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://grid.x402compute.cc/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' => 'qwen-2.5-32b',
'messages' => [
[
'role' => 'user',
'content' => 'Hello'
]
],
'system' => '<string>',
'tools' => [
[
'name' => '<string>',
'description' => '<string>',
'input_schema' => [
]
]
],
'tool_choice' => [
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$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://grid.x402compute.cc/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"qwen-2.5-32b\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello\"\n }\n ],\n \"system\": \"<string>\",\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {}\n }\n ],\n \"tool_choice\": {}\n}")
req, _ := http.NewRequest("POST", url, payload)
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://grid.x402compute.cc/v1/messages/count_tokens")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"qwen-2.5-32b\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello\"\n }\n ],\n \"system\": \"<string>\",\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {}\n }\n ],\n \"tool_choice\": {}\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://grid.x402compute.cc/v1/messages/count_tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"qwen-2.5-32b\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello\"\n }\n ],\n \"system\": \"<string>\",\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {}\n }\n ],\n \"tool_choice\": {}\n}"
response = http.request(request)
puts response.read_body{
"input_tokens": 4
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}Body
application/json
Was this page helpful?
