Fanisana token
Kajio ny token amin'ny soratra na ny fangatahana manontolo alohan'ny handefasanao azy.
POST https://api.shannon-ai.com/v1/tokenize
POST https://api.shannon-ai.com/v1/messages/count_tokens
Ireo endpoint roa ireo dia mikajy amin'ny tokenizer an'ny model tononinao, ary tsy misy model mandeha. Mahasarona ny model open-weight hosted izy ireo. Ny /v1/tokenize dia mandray soratra tsotra na resaka Chat Completions. Ny /v1/messages/count_tokens dia mandray fangatahana amin'ny endrika Anthropic Messages, izay antso ataon'ny SDK Anthropic sy Claude Code.
Maimaim-poana ny fanisana. Ny antso dia mila ny API key-nao, tsy mamoka na inona na inona amin'ny balance-nao ary tsy miseho ao amin'ny usage log-nao.
Mamanisa soratra
Alefaso ny model sy text. Ny soratra dia kajina araka izy, tsy misy chat formatting manodidina azy.
import requests
response = requests.post(
"https://api.shannon-ai.com/v1/tokenize",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"text": "Hello, world",
},
)
print(response.json()["tokens"]) const response = await fetch("https://api.shannon-ai.com/v1/tokenize", {
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
text: "Hello, world",
}),
});
const { tokens } = await response.json();
console.log(tokens); curl https://api.shannon-ai.com/v1/tokenize \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"text": "Hello, world"
}' {
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"tokens": 3
} Ny isa ao amin'ny valiny amin'ity pejy ity dia ohatra. Ny soratra mitovy dia manome isa hafa amin'ny model hafa.
Mamanisa fangatahana chat
Alefaso ny model sy messages, miaraka amin'ny tools raha manana azy ny fangatahana, araka ny fandefasanao ny /v1/chat/completions. Ny valiny dia ny habeny manontolo amin'ny input.
import requests
request = {
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "What is the weather in Paris?"},
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}
],
}
response = requests.post(
"https://api.shannon-ai.com/v1/tokenize",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json=request,
)
print(response.json()["tokens"]) const request = {
model: "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
messages: [
{ role: "system", content: "You are a concise assistant." },
{ role: "user", content: "What is the weather in Paris?" },
],
tools: [
{
type: "function",
function: {
name: "get_weather",
description: "Current weather for a city",
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
},
],
};
const response = await fetch("https://api.shannon-ai.com/v1/tokenize", {
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify(request),
});
const { tokens } = await response.json();
console.log(tokens); curl https://api.shannon-ai.com/v1/tokenize \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "What is the weather in Paris?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}
]
}' {
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"tokens": 164
} Saha ao amin'ny /v1/tokenize
| Saha | Karazana | Famaritana |
|---|---|---|
model | string | Ilaina. Id model open-weight hosted. Tsy mijery ny litera lehibe/kely. |
text | string | Soratra kajina araka izy, tsy misy chat formatting. Hatramin'ny byte 4,000,000. Alefaso ny text na messages; rehefa misy roa dia ny text no kajina. |
messages | array | Hafatra chat amin'ny endrika Chat Completions. Kajina ho input feno amin'ny fangatahana izy ireo: ny hafatra rehetra miaraka amin'ny formatting apetraky ny chat template an'ny model manodidina azy. |
tools | array | Famaritana tool ampidirina ao amin'ny isa. Ampiasaina miaraka amin'ny messages. |
Ny valiny dia object JSON misy ireto saha ireto:
| Saha | Karazana | Famaritana |
|---|---|---|
model | string | Ny id model nanaovana ny fanisana, amin'ny fanoratana navoaka. |
tokens | integer | Miaraka amin'ny text: ny token amin'ny soratra. Miaraka amin'ny messages: ny token amin'ny input manontolo, anisan'izany ny sary. |
Mamanisa fangatahana Messages
Alefaso ny body izay alefanao amin'ny /v1/messages: model, messages, ary system sy tools raha ampiasainao. Ny SDK Anthropic ofisialy dia miantso an'ity endpoint ity amin'ny messages.count_tokens.
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com",
)
count = client.messages.count_tokens(
model="DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
system="You are a concise assistant.",
messages=[
{"role": "user", "content": "Summarise the attached report."}
],
)
print(count.input_tokens) import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.shannon-ai.com",
});
const count = await client.messages.countTokens({
model: "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
system: "You are a concise assistant.",
messages: [
{ role: "user", content: "Summarise the attached report." },
],
});
console.log(count.input_tokens); curl https://api.shannon-ai.com/v1/messages/count_tokens \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"system": "You are a concise assistant.",
"messages": [
{"role": "user", "content": "Summarise the attached report."}
]
}' {
"input_tokens": 21
} Saha ao amin'ny /v1/messages/count_tokens
| Saha | Karazana | Famaritana |
|---|---|---|
model | string | Ilaina. Id model open-weight hosted. |
messages | array | Ilaina. Hafatra amin'ny endrika Anthropic Messages. Kajina ny block text, image, tool_use ary tool_result. |
system | string | array | Ny system prompt: string na array misy text block. |
tools | array | Famaritana tool miaraka amin'ny name, description ary input_schema. |
Raisina ho an'ny compatibility, tsy misy vokany amin'ny isa: tool_choice, max_tokens, temperature, top_p, stop_sequences, stream, thinking. Azonao alefa tsy miova ny body an'ny fangatahana tena izy.
Ny valiny dia object JSON misy ireto saha ireto:
| Saha | Karazana | Famaritana |
|---|---|---|
input_tokens | integer | Ny token amin'ny input manontolo: system prompt, hafatra, tools ary sary. |
Model tohanana
Ireo endpoint roa ireo dia mikajy ho an'ny model open-weight hosted. Ny GET /v1/models dia mitanisa ny /v1/tokenize sy /v1/messages/count_tokens ao amin'ny endpoints an'ny model tsirairay manohana azy ireo. Ny sanda model hafa rehetra, anisan'izany ny id Shannon, dia valiana amin'ny 400.
DeepSeek-V4-Pro-0813-3BIT-REAPGLM-5.2-3BIT-REAPKimi-K3-3BIT-REAPNemotron3Ultra-3BIT-REAPMiniMax-M3-3BIT-REAPDeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAPKimi-K2.6-W4A16-AUTOROUND-REAPLaguna-S-2.1-W4A16-AUTOROUND-REAPinkling-W4A16-AUTOROUND-REAPMiMo-V2.5-Pro-W8A16MiMo-V2.5-W8A16Hy3-W8A16
Ho an'ny model Shannon, vakio ny isan'ny token avy amin'ny object usage an'ny valiny.
Ny fomba fanaovana ny fanisana
Ny model tsirairay dia kajina amin'ny tokenizer sy chat template manokana. Tsy ampiasaina ny tombantombana avy amin'ny tarehintsoratra na teny.
| Izay kajina | Fitsipika |
|---|---|
| Soratra | Ny token amin'ny string araka ny nalefa. Ny string foana dia manisa 0. |
| Hafatra | Ny hafatra sy ny tools dia alahatra araka ny chat template manokana an'ny model, hatramin'ny fotoana manombohan'ny valiny, ary io prompt manontolo io no kajina. |
| Role | Kajina ny hafatra system, user, assistant ary tool. Ny developer dia kajina ho system. Ny hafatra tsy misy content sy tsy misy tool call dia tsy manampy na inona na inona. |
| Tool call sy valiny | Ny tool call amin'ny fihodinan'ny assistant teo aloha sy ny valiny dia anisan'ny isa, amin'ireo endpoint roa. |
| Sary | Ny sary alefa ao anaty body (base64 na URL data:) dia manampy token iray isaky ny patch 28 × 28 pixel: ceil(width / 28) × ceil(height / 28). Ny sary omena ho URL http(s) dia tsy alain'ireo endpoint ireo ary manisa 1,024. |
Ohatra: sary 1,024 × 768 pixel dia manisa ceil(1024 / 28) × ceil(768 / 28) = 37 × 28 = 1,036 token.
Ny isa sy izay vidiana amin'ny fangatahana
Ny fanisana ny fangatahana manontolo dia atao mitovy amin'ny fanisana input an'ny fangatahana tena izy manana model, hafatra ary tools mitovy. Ny valiny dia milaza io isa io ho usage.prompt_tokens amin'ny Chat Completions, ho usage.input_tokens amin'ny Responses, ary ho usage.input_tokens miampy usage.cache_read_input_tokens amin'ny Messages.
- Ny isa dia ny input alohan'ny fihenam-bidin'ny input cached. Ny fangatahana tena izy dia mety mamaky ampahany amin'io input io avy amin'ny cache ary mamidy io ampahany io amin'ny tahan'ny cached. Fitehirizana prompt
- Ny sary omena ho URL
http(s)dia manisa 1,024 eto. Ny fangatahana tena izy dia mampiditra ny sary ary mikajy azy araka ny habeny amin'ny pixel, ka mety tsy hitovy ny isa roa. Alefaso ho base64 ny sary mba hahazoana isa mitovy. - Ny output dia tsy anisan'ny isa. Ny valin'ny fangatahana tena izy dia vidiana ho output token fanampiny, anisan'izany ny reasoning.
- Ny isa
textdia tsy manana chat formatting. Ampiasao handrefesana document na ampahany amin'ny prompt, ary ny endrikamessageshandrefesana fangatahana.
Hanovana isa ho vidiny, ampitomboy amin'ny vidin'ny input isaky ny token 1M an'ny model izy. Model & vidiny
Fetra
| Fetra | Sanda | Mihoatra azy |
|---|---|---|
Halavan'ny text | Byte 4,000,000 (UTF-8) | 413 miaraka amin'ny hafatra text too long |
| Body ny fangatahana | 32 MiB | 413 |
| Isaky ny fangatahana | Soratra iray na resaka iray | Alefaso fangatahana iray isaky ny soratra raha hanisa soratra maromaro. |
Ny antso fanisana dia tsy kajina ao amin'ny fetra fangatahana 120 isa-minitra. Fetra sy balance
Hadisoana
| Status | Type | Hafatra | Rahoviana |
|---|---|---|---|
400 | invalid_request_error | tokenize is available for the hosted open models; unknown model: <model> | /v1/tokenize miaraka amin'ny model izay tsy id open-weight hosted. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <model> | /v1/messages/count_tokens miaraka amin'ny model izay tsy id open-weight hosted, na tsy misy model. |
400 | invalid_request_error | send `text` or `messages` | /v1/tokenize tsy misy text na messages. |
401 | authentication_error | Missing authentication / Invalid API key | Tsy nandefa key, na tsy mety ny key. |
413 | invalid_request_error | text too long | Lava noho ny byte 4,000,000 ny text. Ny body mihoatra ny 32 MiB koa dia valiana amin'ny 413. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Tsy manana content type JSON ny fangatahana. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Tsy misy saha ilaina (model amin'ny /v1/tokenize, messages amin'ny /v1/messages/count_tokens) na diso ny karazana saha iray. |
503 | api_error | token counting is temporarily unavailable for this model | Tsy azo atao amin'izao fotoana izao ny fanisana ho an'ity model ity. Andramo indray any aoriana. |
Ny /v1/tokenize dia mamerina hadisoana amin'ny endrika OpenAI. Amin'ny /v1/messages/count_tokens, ny hadisoan'ny endpoint mihitsy (400 ho an'ny model, 503) dia tonga amin'ny endrika Anthropic, ary ny 401, 413, 415 ary 422 dia tonga amin'ny endrika OpenAI. Vakio aloha ny code status, avy eo ny error.type sy error.message, izay misy amin'ny endrika roa.
{
"error": {
"type": "invalid_request_error",
"message": "tokenize is available for the hosted open models; unknown model: shannon-3"
}
} {
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "count_tokens is available for the hosted open models; unknown model: shannon-3"
}
}