Count input tokens for an Anthropic-compatible request
curl --request POST \
--url https://api.usehasp.com/v1/messages/count_tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"messages": [
{
"content": "<string>"
}
],
"tool_choice": "<string>",
"stop_sequences": [
"<string>"
],
"system": [
{
"type": "text",
"text": "<string>",
"cache_control": {
"type": "ephemeral"
}
}
],
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": {
"type": "<string>"
},
"cache_control": {
"type": "ephemeral"
}
}
]
}
'import requests
url = "https://api.usehasp.com/v1/messages/count_tokens"
payload = {
"model": "<string>",
"messages": [{ "content": "<string>" }],
"tool_choice": "<string>",
"stop_sequences": ["<string>"],
"system": [
{
"type": "text",
"text": "<string>",
"cache_control": { "type": "ephemeral" }
}
],
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": { "type": "<string>" },
"cache_control": { "type": "ephemeral" }
}
]
}
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>'}],
tool_choice: '<string>',
stop_sequences: ['<string>'],
system: [{type: 'text', text: '<string>', cache_control: {type: 'ephemeral'}}],
tools: [
{
name: '<string>',
description: '<string>',
input_schema: {type: '<string>'},
cache_control: {type: 'ephemeral'}
}
]
})
};
fetch('https://api.usehasp.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.usehasp.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>'
]
],
'tool_choice' => '<string>',
'stop_sequences' => [
'<string>'
],
'system' => [
[
'type' => 'text',
'text' => '<string>',
'cache_control' => [
'type' => 'ephemeral'
]
]
],
'tools' => [
[
'name' => '<string>',
'description' => '<string>',
'input_schema' => [
'type' => '<string>'
],
'cache_control' => [
'type' => 'ephemeral'
]
]
]
]),
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.usehasp.com/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"tool_choice\": \"<string>\",\n \"stop_sequences\": [\n \"<string>\"\n ],\n \"system\": [\n {\n \"type\": \"text\",\n \"text\": \"<string>\",\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ],\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {\n \"type\": \"<string>\"\n },\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ]\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.usehasp.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 \"tool_choice\": \"<string>\",\n \"stop_sequences\": [\n \"<string>\"\n ],\n \"system\": [\n {\n \"type\": \"text\",\n \"text\": \"<string>\",\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ],\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {\n \"type\": \"<string>\"\n },\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.usehasp.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 \"tool_choice\": \"<string>\",\n \"stop_sequences\": [\n \"<string>\"\n ],\n \"system\": [\n {\n \"type\": \"text\",\n \"text\": \"<string>\",\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ],\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {\n \"type\": \"<string>\"\n },\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"input_tokens": 123
}"<string>"{
"message": "<string>"
}{
"message": "<string>",
"errors": {}
}"<string>"CountTokens
Count input tokens for an Anthropic-compatible request
Does not generate a completion or consume AI credits.
POST
/
messages
/
count_tokens
Count input tokens for an Anthropic-compatible request
curl --request POST \
--url https://api.usehasp.com/v1/messages/count_tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"messages": [
{
"content": "<string>"
}
],
"tool_choice": "<string>",
"stop_sequences": [
"<string>"
],
"system": [
{
"type": "text",
"text": "<string>",
"cache_control": {
"type": "ephemeral"
}
}
],
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": {
"type": "<string>"
},
"cache_control": {
"type": "ephemeral"
}
}
]
}
'import requests
url = "https://api.usehasp.com/v1/messages/count_tokens"
payload = {
"model": "<string>",
"messages": [{ "content": "<string>" }],
"tool_choice": "<string>",
"stop_sequences": ["<string>"],
"system": [
{
"type": "text",
"text": "<string>",
"cache_control": { "type": "ephemeral" }
}
],
"tools": [
{
"name": "<string>",
"description": "<string>",
"input_schema": { "type": "<string>" },
"cache_control": { "type": "ephemeral" }
}
]
}
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>'}],
tool_choice: '<string>',
stop_sequences: ['<string>'],
system: [{type: 'text', text: '<string>', cache_control: {type: 'ephemeral'}}],
tools: [
{
name: '<string>',
description: '<string>',
input_schema: {type: '<string>'},
cache_control: {type: 'ephemeral'}
}
]
})
};
fetch('https://api.usehasp.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.usehasp.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>'
]
],
'tool_choice' => '<string>',
'stop_sequences' => [
'<string>'
],
'system' => [
[
'type' => 'text',
'text' => '<string>',
'cache_control' => [
'type' => 'ephemeral'
]
]
],
'tools' => [
[
'name' => '<string>',
'description' => '<string>',
'input_schema' => [
'type' => '<string>'
],
'cache_control' => [
'type' => 'ephemeral'
]
]
]
]),
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.usehasp.com/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"tool_choice\": \"<string>\",\n \"stop_sequences\": [\n \"<string>\"\n ],\n \"system\": [\n {\n \"type\": \"text\",\n \"text\": \"<string>\",\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ],\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {\n \"type\": \"<string>\"\n },\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ]\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.usehasp.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 \"tool_choice\": \"<string>\",\n \"stop_sequences\": [\n \"<string>\"\n ],\n \"system\": [\n {\n \"type\": \"text\",\n \"text\": \"<string>\",\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ],\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {\n \"type\": \"<string>\"\n },\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.usehasp.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 \"tool_choice\": \"<string>\",\n \"stop_sequences\": [\n \"<string>\"\n ],\n \"system\": [\n {\n \"type\": \"text\",\n \"text\": \"<string>\",\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ],\n \"tools\": [\n {\n \"name\": \"<string>\",\n \"description\": \"<string>\",\n \"input_schema\": {\n \"type\": \"<string>\"\n },\n \"cache_control\": {\n \"type\": \"ephemeral\"\n }\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"input_tokens": 123
}"<string>"{
"message": "<string>"
}{
"message": "<string>",
"errors": {}
}"<string>"Authorizations
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
Body
application/json
Validates POST /v1/messages/count_tokens (Anthropic-compat) per ADR-YGE00M.
Accepts the same request body shape as /v1/messages (minus stream and max_tokens, which don't affect token counting) so a client can count tokens for the exact same payload it's about to send for a real call.
Response
⌘I