Skip to main content
POST
/
messages
/
count_tokens
Count tokens for an Anthropic message
curl --request POST \
  --url https://api.sailresearch.com/v1/messages/count_tokens \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "<string>",
  "messages": [
    {
      "content": "<string>"
    }
  ],
  "max_tokens": 2,
  "system": "<string>",
  "temperature": 0.5,
  "top_p": 0.5,
  "output_config": {},
  "tools": [
    {}
  ],
  "tool_choice": {},
  "thinking": {},
  "metadata": {
    "completion_webhook": "<string>",
    "webhook_token": "<string>"
  },
  "context_management": {}
}
'
import requests

url = "https://api.sailresearch.com/v1/messages/count_tokens"

payload = {
"model": "<string>",
"messages": [{ "content": "<string>" }],
"max_tokens": 2,
"system": "<string>",
"temperature": 0.5,
"top_p": 0.5,
"output_config": {},
"tools": [{}],
"tool_choice": {},
"thinking": {},
"metadata": {
"completion_webhook": "<string>",
"webhook_token": "<string>"
},
"context_management": {}
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
messages: [{content: '<string>'}],
max_tokens: 2,
system: '<string>',
temperature: 0.5,
top_p: 0.5,
output_config: {},
tools: [{}],
tool_choice: {},
thinking: {},
metadata: {completion_webhook: '<string>', webhook_token: '<string>'},
context_management: {}
})
};

fetch('https://api.sailresearch.com/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://api.sailresearch.com/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>'
]
],
'max_tokens' => 2,
'system' => '<string>',
'temperature' => 0.5,
'top_p' => 0.5,
'output_config' => [

],
'tools' => [
[

]
],
'tool_choice' => [

],
'thinking' => [

],
'metadata' => [
'completion_webhook' => '<string>',
'webhook_token' => '<string>'
],
'context_management' => [

]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"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://api.sailresearch.com/v1/messages/count_tokens"

payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"max_tokens\": 2,\n \"system\": \"<string>\",\n \"temperature\": 0.5,\n \"top_p\": 0.5,\n \"output_config\": {},\n \"tools\": [\n {}\n ],\n \"tool_choice\": {},\n \"thinking\": {},\n \"metadata\": {\n \"completion_webhook\": \"<string>\",\n \"webhook_token\": \"<string>\"\n },\n \"context_management\": {}\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Authorization", "Bearer <token>")
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://api.sailresearch.com/v1/messages/count_tokens")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"max_tokens\": 2,\n \"system\": \"<string>\",\n \"temperature\": 0.5,\n \"top_p\": 0.5,\n \"output_config\": {},\n \"tools\": [\n {}\n ],\n \"tool_choice\": {},\n \"thinking\": {},\n \"metadata\": {\n \"completion_webhook\": \"<string>\",\n \"webhook_token\": \"<string>\"\n },\n \"context_management\": {}\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://api.sailresearch.com/v1/messages/count_tokens")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"max_tokens\": 2,\n \"system\": \"<string>\",\n \"temperature\": 0.5,\n \"top_p\": 0.5,\n \"output_config\": {},\n \"tools\": [\n {}\n ],\n \"tool_choice\": {},\n \"thinking\": {},\n \"metadata\": {\n \"completion_webhook\": \"<string>\",\n \"webhook_token\": \"<string>\"\n },\n \"context_management\": {}\n}"

response = http.request(request)
puts response.read_body
{
  "input_tokens": 123
}
{
"error": {
"message": "<string>",
"type": "<string>",
"param": "<string>",
"code": "<string>"
}
}
{
"error": {
"message": "<string>",
"type": "<string>",
"param": "<string>",
"code": "<string>"
}
}
{
"error": {
"message": "<string>",
"type": "<string>",
"param": "<string>",
"code": "<string>"
}
}
{
"error": {
"message": "<string>",
"type": "<string>",
"param": "<string>",
"code": "<string>"
}
}

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Body

application/json

Same shape as a create-message request; max_tokens is optional and ignored for counting.

model
string
required
messages
object[]
required
Minimum array length: 1
max_tokens
integer

Optional here; validated if present, not used for counting.

Required range: x >= 1
system

System prompt: a string, or an array of text blocks.

temperature
number
Required range: 0 <= x <= 1
top_p
number
Required range: 0 <= x <= 1
output_config
object
tools
object[]

Tool definitions the model may call. Responses include tool_use blocks; return outcomes as tool_result content blocks in a follow-up message.

tool_choice
object

Controls tool use: auto, any, tool, or none.

thinking
object

Extended thinking configuration; translated to the model's reasoning.

metadata
object

Optional string metadata. completion_window controls scheduling; completion_webhook/webhook_token configure completion webhooks.

context_management
object

Anthropic server-side context-editing config (Claude Code sends it automatically). Accepted for compatibility but ignored — Sail forwards the full context each turn.

Response

Input token count.

input_tokens
integer
required