import SambaNova from 'sambanova';
const client = new SambaNova({
apiKey: process.env['SAMBANOVA_API_KEY'], // This is the default and can be omitted
});
const messageCountTokensResponse = await client.messages.countTokens({
messages: [{ content: 'Hello, Claude!', role: 'user' }],
model: 'DeepSeek-V3.1',
});
console.log(messageCountTokensResponse.input_tokens);import os
from sambanova import SambaNova
client = SambaNova(
api_key=os.environ.get("SAMBANOVA_API_KEY"), # This is the default and can be omitted
)
message_count_tokens_response = client.messages.count_tokens(
messages=[{
"content": "Hello, Claude!",
"role": "user",
}],
model="DeepSeek-V3.1",
)
print(message_count_tokens_response.input_tokens)curl --request POST \
--url https://api.sambanova.ai/v1/messages/count_tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "DeepSeek-V3.1",
"messages": [
{
"role": "user",
"content": "Hello, Claude!"
}
]
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.sambanova.ai/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' => 'DeepSeek-V3.1',
'messages' => [
[
'role' => 'user',
'content' => 'Hello, Claude!'
]
]
]),
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.sambanova.ai/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"DeepSeek-V3.1\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello, Claude!\"\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.sambanova.ai/v1/messages/count_tokens")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"DeepSeek-V3.1\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello, Claude!\"\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.sambanova.ai/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\": \"DeepSeek-V3.1\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello, Claude!\"\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"input_tokens": 123
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}Count tokens for a message request
Anthropic count_tokens compatible endpoint. Returns the number of input tokens that would be consumed by a POST /messages call with the same prompt content (system, messages, tools, tool_choice). Authentication accepts either the bearer Authorization: Bearer <key> header (SambaNova SDK default) or the x-api-key header (Anthropic SDK default); the same API key is used in both cases.
import SambaNova from 'sambanova';
const client = new SambaNova({
apiKey: process.env['SAMBANOVA_API_KEY'], // This is the default and can be omitted
});
const messageCountTokensResponse = await client.messages.countTokens({
messages: [{ content: 'Hello, Claude!', role: 'user' }],
model: 'DeepSeek-V3.1',
});
console.log(messageCountTokensResponse.input_tokens);import os
from sambanova import SambaNova
client = SambaNova(
api_key=os.environ.get("SAMBANOVA_API_KEY"), # This is the default and can be omitted
)
message_count_tokens_response = client.messages.count_tokens(
messages=[{
"content": "Hello, Claude!",
"role": "user",
}],
model="DeepSeek-V3.1",
)
print(message_count_tokens_response.input_tokens)curl --request POST \
--url https://api.sambanova.ai/v1/messages/count_tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "DeepSeek-V3.1",
"messages": [
{
"role": "user",
"content": "Hello, Claude!"
}
]
}
'<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.sambanova.ai/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' => 'DeepSeek-V3.1',
'messages' => [
[
'role' => 'user',
'content' => 'Hello, Claude!'
]
]
]),
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.sambanova.ai/v1/messages/count_tokens"
payload := strings.NewReader("{\n \"model\": \"DeepSeek-V3.1\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello, Claude!\"\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.sambanova.ai/v1/messages/count_tokens")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"DeepSeek-V3.1\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello, Claude!\"\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.sambanova.ai/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\": \"DeepSeek-V3.1\",\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Hello, Claude!\"\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"input_tokens": 123
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "<string>"
},
"request_id": "<string>"
}Authorizations
SambaNova API key, sent as a bearer token in the Authorization header (Authorization: Bearer <key>). Default authentication scheme used by the SambaNova SDK across every OpenAI compatible endpoint.
Headers
Anthropic API version header sent by the official anthropic SDK. Accepted (any value) but currently has no effect on response shape - included for drop-in SDK compatibility.
"2023-06-01"
Body
Token counting parameters (subset of message creation parameters).
Request body for POST /messages/count_tokens. Returns the input token count for a prompt without generating output. Same prompt shape as MessageCreateRequest minus generation-time parameters (max_tokens, stream, sampling, stop_sequences, metadata, service_tier, etc.).
Model identifier.
"gpt-oss-120b"
Conversation turns.
1Show child attributes
Show child attributes
System prompt for the conversation. Accepts either a single string (most common) or an array of text blocks (used when individual segments need cache_control markers). Multiple text blocks are joined with newlines and prepended to the conversation as a role: system message.
Disables Anthropic-style extended thinking. In v1: silently accepted as a no-op
- Message Thinking Disabled
- Message Thinking Enabled
- Message Thinking Adaptive
Show child attributes
Show child attributes
Tool definitions the model may call.
Show child attributes
Show child attributes
Controls how the model selects from tools.
- Message Tool Choice (Auto)
- Message Tool Choice (Any)
- Message Tool Choice (None)
- Message Tool Choice (Tool)
Show child attributes
Show child attributes
Response
Successful response. Returns the input token count.
Token count for the supplied prompt.
Total tokens in the prompt (system + messages + tools).

