Ukubala ama-token
Bala ama-token ombhalo noma wesicelo sonke ngaphambi kokusithumela.
POST https://api.shannon-ai.com/v1/tokenize
POST https://api.shannon-ai.com/v1/messages/count_tokens
Womabili ama-endpoint abala nge-tokenizer ye-model oyiqambayo, futhi akukho model eqhubekayo. Ahlanganisa ama-model e-open-weight ahlinzekiwe. I-/v1/tokenize yamukela umbhalo olula noma ingxoxo ye-Chat Completions. I-/v1/messages/count_tokens yamukela isicelo ngesimo se-Anthropic Messages, okuyisicelo esenziwa yi-Anthropic SDK ne-Claude Code.
Ukubala kuyamahhala. Isicelo sidinga i-API key yakho, asithathi lutho ku-balance yakho futhi asivezi kulogi yakho yokusetshenziswa.
Bala umbhalo
Thumela i-model ne-text. Umbhalo ubalwa njengoba unjalo, ngaphandle kokufomatha kwengxoxo okuwuzungezile.
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
} Izinombolo ezimpendulweni zalelikhasi yizibonelo. Umbhalo ofanayo unikeza inani elihlukile ku-model ehlukile.
Bala isicelo sengxoxo
Thumela i-model ne-messages, kanye ne-tools uma isicelo sinayo, ngqo njengoba ngabe uzithumela ku-/v1/chat/completions. Impendulo iwubukhulu bayo bonke okufakwayo.
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
} Ama-field e-/v1/tokenize
| I-field | Uhlobo | Incazelo |
|---|---|---|
model | string | Iyadingeka. I-id ye-model e-open-weight ehlinzekiwe. Onkulu namancane aphathwa ngokufanayo. |
text | string | Umbhalo okufanele ubalwe njengoba unjalo, ngaphandle kokufomatha kwengxoxo. Kufika kuma-byte angu-4,000,000. Thumela i-text noma i-messages; uma kukhona zombili, i-text iyabalwa. |
messages | array | Imilayezo yengxoxo ngesimo se-Chat Completions. Ibalwa njengokufakwayo okuphelele kwesicelo: umlayezo ngamunye nokufomatha i-chat template ye-model eliwuzungezisa ngakho. |
tools | array | Izincazelo zamathuluzi okufanele zifakwe ekubaleni. Zisetshenziswa kanye ne-messages. |
Impendulo iyi-JSON object enalawa ma-field:
| I-field | Uhlobo | Incazelo |
|---|---|---|
model | string | I-id ye-model ukubala okwenziwe ngayo, ngendlela eshicilelwe ngayo. |
tokens | integer | Nge-text: ama-token ombhalo. Nge-messages: ama-token wonke okufakwayo, kuhlanganise nezithombe. |
Bala isicelo se-Messages
Thumela umzimba obuzowuthumela ku-/v1/messages: i-model, messages, kanye ne-system ne-tools uma uzisebenzisa. Ama-SDK asemthethweni e-Anthropic abiza le-endpoint nge-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
} Ama-field e-/v1/messages/count_tokens
| I-field | Uhlobo | Incazelo |
|---|---|---|
model | string | Iyadingeka. I-id ye-model e-open-weight ehlinzekiwe. |
messages | array | Iyadingeka. Imilayezo ngesimo se-Anthropic Messages. Amabhlokhi e-text, image, tool_use ne-tool_result ayabalwa. |
system | string | array | I-system prompt: i-string noma i-array yamabhlokhi ombhalo. |
tools | array | Izincazelo zamathuluzi ezine-name, description ne-input_schema. |
Kuyamukelwa ukuze kuhambisane, ngaphandle komthelela ekubaleni: tool_choice, max_tokens, temperature, top_p, stop_sequences, stream, thinking. Ungadlulisa umzimba wesicelo sangempela ungashintshile.
Impendulo iyi-JSON object enalawa ma-field:
| I-field | Uhlobo | Incazelo |
|---|---|---|
input_tokens | integer | Ama-token wonke okufakwayo: i-system prompt, imilayezo, amathuluzi nezithombe. |
Ama-model asekelwayo
Womabili ama-endpoint abala ama-model e-open-weight ahlinzekiwe. I-GET /v1/models ibhala i-/v1/tokenize ne-/v1/messages/count_tokens ku-endpoints ye-model ngayinye eyawasekela. Noma yiliphi elinye inani le-model, kuhlanganise nama-id e-Shannon, liphendulwa nge-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
Ku-model ye-Shannon, funda izibalo zama-token ku-object ye-usage yempendulo.
Ukuthi ukubala kwenziwa kanjani
I-model ngayinye ibalwa nge-tokenizer yayo ne-chat template yayo. Akukho silinganiso esisekelwe ezinhlamvini noma emagameni esisetshenzisiwe.
| Okubalwayo | Umthetho |
|---|---|
| Umbhalo | Ama-token e-string njengoba ithunyelwe. I-string engenalutho ibalwa ngo-0. |
| Imilayezo | Imilayezo namathuluzi kuhlelwa nge-chat template ye-model uqobo, kuze kube yilapho impendulo iqala khona, futhi yonke leyo prompt iyabalwa. |
| Izindima | Imilayezo ye-system, user, assistant ne-tool iyabalwa. I-developer ibalwa njenge-system. Umlayezo ongenakho ukuqukethwe futhi ongenasicelo sethuluzi awungezi lutho. |
| Izicelo zamathuluzi nemiphumela | Izicelo zamathuluzi zezikhathi zangaphambilini ze-assistant nemiphumela yazo ziyingxenye yokubala, kuwo womabili ama-endpoint. |
| Izithombe | Isithombe esithunyelwe ngaphakathi kwomzimba (base64 noma i-data: URL) songeza i-token eyodwa ku-patch ngayinye yamapikseli angu-28 × 28: ceil(width / 28) × ceil(height / 28). Isithombe esinikezwe njenge-URL ye-http(s) asilandwa yilawa ma-endpoint futhi sibalwa ngo-1,024. |
Isibonelo: isithombe esingamapikseli angu-1,024 × 768 sibalwa njenge-ceil(1024 / 28) × ceil(768 / 28) = 37 × 28 = 1,036 wama-token.
Inani nokukhokhiswa kwesicelo
Ukubalwa kwesicelo sonke kwenziwa ngendlela efanayo nokubalwa kokufakwayo kwesicelo sangempela esinama-model, imilayezo namathuluzi afanayo. Impendulo ibika leyo nombolo njenge-usage.prompt_tokens ku-Chat Completions, njenge-usage.input_tokens ku-Responses, nanjenge-usage.input_tokens kanye ne-usage.cache_read_input_tokens ku-Messages.
- Inani yilokho okufakwayo ngaphambi kwesaphulelo se-cached-input. Isicelo sangempela singafunda ingxenye yalokho okufakwayo ku-cache futhi sikhokhise leyo ngxenye ngenani le-cache. I-Prompt caching
- Isithombe esinikezwe njenge-URL ye-
http(s)sibalwa ngo-1,024 lapha. Isicelo sangempela silanda isithombe futhi sisibala ngosayizi waso ngamapikseli, ngakho izinombolo ezimbili zingahluka. Thumela isithombe njenge-base64 ukuze uthole inombolo efanayo. - Okuphumayo akuyona ingxenye yokubala. Impendulo yesicelo sangempela ikhokhiswa njengama-token okuphumayo phezu kwalokho, kuhlanganise nokucabanga.
- Ukubalwa kwe-
textakunakho ukufomatha kwengxoxo. Kusebenzise ukulinganisa idokhumenti noma ingxenye ye-prompt, nesimo se-messagesukulinganisa isicelo.
Ukuguqula inani libe yizindleko, likhuphule ngentengo yokufakwayo ye-model ngama-token angu-1M. Ama-model namanani
Imikhawulo
| Umkhawulo | Inani | Ngaphezu kwawo |
|---|---|---|
Ubude be-text | Ama-byte angu-4,000,000 (UTF-8) | 413 nomlayezo text too long |
| Umzimba wesicelo | 32 MiB | 413 |
| Ngesicelo | Umbhalo owodwa noma ingxoxo eyodwa | Thumela isicelo esisodwa ngombhalo ngamunye ukuze ubale imibhalo eminingi. |
Izicelo zokubala azibalwa emkhawulweni wezicelo ezingu-120 ngomzuzu. Imikhawulo ne-balance
Amaphutha
| Isimo | Uhlobo | Umlayezo | Nini |
|---|---|---|---|
400 | invalid_request_error | tokenize is available for the hosted open models; unknown model: <model> | I-/v1/tokenize ene-model engeyona i-id ye-model e-open-weight ehlinzekiwe. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <model> | I-/v1/messages/count_tokens ene-model engeyona i-id ye-model e-open-weight ehlinzekiwe, noma ngaphandle kwe-model. |
400 | invalid_request_error | send `text` or `messages` | I-/v1/tokenize ngaphandle kwe-text ne-messages. |
401 | authentication_error | Missing authentication / Invalid API key | Akukho key ethunyelwe, noma i-key ayisebenzi. |
413 | invalid_request_error | text too long | I-text mide kunama-byte angu-4,000,000. Umzimba ongaphezu kuka-32 MiB uphendulwa futhi nge-413. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Isicelo asinalo uhlobo lokuqukethwe lwe-JSON. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | I-field edingekayo ayikho (i-model ku-/v1/tokenize, i-messages ku-/v1/messages/count_tokens) noma i-field inohlobo olungalungile. |
503 | api_error | token counting is temporarily unavailable for this model | Ukubala akukwazi ukwenziwa kule-model njengamanje. Zama futhi kamuva. |
I-/v1/tokenize ibuyisa amaphutha ngesimo se-OpenAI. Ku-/v1/messages/count_tokens amaphutha e-endpoint uqobo (i-400 ye-model, i-503) afika ngesimo se-Anthropic, futhi i-401, 413, 415 ne-422 zifika ngesimo se-OpenAI. Funda ikhodi yesimo kuqala, bese i-error.type ne-error.message, ezikhona kuzo zombili izimo.
{
"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"
}
}