Messages Count Tokens
curl --request POST \
--url https://nano-gpt.com/api/v1/messages/count_tokensimport requests
url = "https://nano-gpt.com/api/v1/messages/count_tokens"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://nano-gpt.com/api/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://nano-gpt.com/api/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",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://nano-gpt.com/api/v1/messages/count_tokens"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://nano-gpt.com/api/v1/messages/count_tokens")
.asString();require 'uri'
require 'net/http'
url = URI("https://nano-gpt.com/api/v1/messages/count_tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_bodyEndpoint Examples
Messages Count Tokens
Anthropic-compatible token estimate endpoint for planning and cost control
POST
/
api
/
v1
/
messages
/
count_tokens
Messages Count Tokens
curl --request POST \
--url https://nano-gpt.com/api/v1/messages/count_tokensimport requests
url = "https://nano-gpt.com/api/v1/messages/count_tokens"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://nano-gpt.com/api/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://nano-gpt.com/api/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",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://nano-gpt.com/api/v1/messages/count_tokens"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://nano-gpt.com/api/v1/messages/count_tokens")
.asString();require 'uri'
require 'net/http'
url = URI("https://nano-gpt.com/api/v1/messages/count_tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_bodyOverview
UsePOST /api/v1/messages/count_tokens to estimate the number of input tokens for an Anthropic Messages-format request.
This endpoint is useful for request planning, context budgeting, and cost control. The result is an estimate and may not exactly match a provider tokenizer.
Endpoint
POST https://nano-gpt.com/api/v1/messages/count_tokens
Authentication
Use either header:Authorization: Bearer YOUR_API_KEYx-api-key: YOUR_API_KEY
Request Body
{
"model": "claude-3-5-sonnet",
"system": "You are concise.",
"messages": [
{
"role": "user",
"content": "Hello"
}
],
"tools": [],
"tool_choice": null
}
Supported Content
The request uses Anthropic message format and supports:- text blocks
- image and document-like blocks
- tool use blocks
- tool result blocks
systemtoolstool_choice
Response
{
"input_tokens": 123
}
Example
curl -X POST https://nano-gpt.com/api/v1/messages/count_tokens \
-H "Authorization: Bearer $NANOGPT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"system": "You are concise.",
"messages": [
{ "role": "user", "content": "Hello" }
]
}'
Notes
- This is an estimate for planning and cost control.
- It may not exactly match a provider tokenizer.
- For generation, use
POST /api/v1/messages.