Cunntadh tokens
Cunnt tokens teacsa no iarrtais slàin mus cuir thu e.
POST https://api.shannon-ai.com/v1/tokenize
POST https://api.shannon-ai.com/v1/messages/count_tokens
Bidh an dà endpoint a’ cunntadh le tokenizer a’ mhodail a dh’ainmicheas tu, agus chan eil modail sam bith a’ ruith. Còmhdaichidh iad na modailean open-weight hosted. Gabhaidh /v1/tokenize teacsa lom no còmhradh Chat Completions. Gabhaidh /v1/messages/count_tokens iarrtas ann an cruth Anthropic Messages, an gairm a nì an Anthropic SDK agus Claude Code.
Tha cunntadh an-asgaidh. Tha feum aig gairm air d’ iuchair API, chan eil i a’ toirt dad bho do balance agus chan eil i a’ nochdadh san loga cleachdaidh agad.
Cunnt teacsa
Cuir model agus text. Thèid an teacsa a chunntadh mar a tha e, gun fhòrmatadh còmhraidh timcheall air.
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
} Is e eisimpleirean na h-àireamhan anns na freagairtean air an duilleag seo. Bheir an aon teacsa cunntas eadar-dhealaichte air modail eile.
Cunnt iarrtas còmhraidh
Cuir model agus messages, le tools nuair a tha iad san iarrtas, dìreach mar a chuireadh tu gu /v1/chat/completions iad. Is e an freagairt meud a’ chuir-a-steach gu lèir.
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
} Raointean /v1/tokenize
| Raon | Seòrsa | Tuairisgeul |
|---|---|---|
model | string | Riatanach. Id modail open-weight hosted. Thèid litrichean mòra is beaga a làimhseachadh san aon dòigh. |
text | string | Teacsa ri chunntadh mar a tha e, gun fhòrmatadh còmhraidh. Suas gu 4,000,000 bytes. Cuir text no messages; nuair a tha an dà chuid ann, thèid text a chunntadh. |
messages | array | Teachdaireachdan còmhraidh ann an cruth Chat Completions. Thèid an cunntadh mar làn chuir-a-steach iarrtais: gach teachdaireachd leis an fhòrmatadh a chuireas teamplaid chòmhraidh a’ mhodail timcheall air. |
tools | array | Mìneachaidhean innealan ri gabhail a-steach sa chunntas. Air an cleachdadh còmhla ri messages. |
Is e nì JSON a th’ anns an fhreagairt leis na raointean seo:
| Raon | Seòrsa | Tuairisgeul |
|---|---|---|
model | string | Id a’ mhodail dhan deach an cunntas a dhèanamh, anns an litreachadh fhoillsichte aige. |
tokens | integer | Le text: tokens an teacsa. Le messages: tokens a’ chuir-a-steach gu lèir, dealbhan cuimsichte. |
Cunnt iarrtas Messages
Cuir am bodhaig a chuireadh tu gu /v1/messages: model, messages, agus system agus tools nuair a chleachdas tu iad. Gairmidh na Anthropic SDKan oifigeil an endpoint seo tro 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
} Raointean /v1/messages/count_tokens
| Raon | Seòrsa | Tuairisgeul |
|---|---|---|
model | string | Riatanach. Id modail open-weight hosted. |
messages | array | Riatanach. Teachdaireachdan ann an cruth Anthropic Messages. Thèid blocaichean text, image, tool_use agus tool_result a chunntadh. |
system | string | array | An system prompt: sreang no raon de bhlocaichean teacsa. |
tools | array | Mìneachaidhean innealan le name, description agus input_schema. |
Air an gabhail airson co-chòrdalachd, gun bhuaidh air a’ chunntas: tool_choice, max_tokens, temperature, top_p, stop_sequences, stream, thinking. Faodaidh tu bodhaig iarrtais fhìor a chur gun atharrachadh.
Is e nì JSON a th’ anns an fhreagairt leis na raointean seo:
| Raon | Seòrsa | Tuairisgeul |
|---|---|---|
input_tokens | integer | Tokens a’ chuir-a-steach gu lèir: system prompt, teachdaireachdan, innealan agus dealbhan. |
Modailean le taic
Bidh an dà endpoint a’ cunntadh airson na modailean open-weight hosted. Tha GET /v1/models a’ liostadh /v1/tokenize agus /v1/messages/count_tokens ann an endpoints gach modail a tha a’ toirt taic dhaibh. Thèid luach model sam bith eile, ids Shannon cuimsichte, a fhreagairt le 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
Airson modail Shannon, leugh na cunntasan tokens bhon nì usage aig freagairt.
Mar a thèid an cunntas a dhèanamh
Tha gach modail air a chunntadh leis an tokenizer aige fhèin agus an teamplaid chòmhraidh aige fhèin. Chan eilear a’ cleachdadh tuairmse o charactaran no faclan.
| Dè a thèid a chunntadh | Riaghailt |
|---|---|
| Teacsa | Tokens na sreinge mar a chaidh a chur. Tha sreang fhalamh a’ cunntadh 0. |
| Teachdaireachdan | Thèid na teachdaireachdan agus na h-innealan a chur a-mach le teamplaid chòmhraidh a’ mhodail fhèin, gus a’ phuing far an tòisich am freagairt, agus thèid am prompt gu lèir sin a chunntadh. |
| Dreuchdan | Thèid teachdaireachdan system, user, assistant agus tool a chunntadh. Thèid developer a chunntadh mar system. Chan eil teachdaireachd gun susbaint is gun ghairm inneal a’ cur dad ris. |
| Gairmean agus toraidhean innealan | Tha gairmean inneal bho thurns neach-cuideachaidh na bu tràithe agus na toraidhean aca nam pàirt dhen chunntas, air an dà endpoint. |
| Dealbhan | Cuiridh dealbh a thèid a chur am broinn a’ bhodhaig (base64 no URL data:) aon token gach paiste 28 × 28 piogsail: ceil(width / 28) × ceil(height / 28). Chan eil dealbh a thèid a thoirt mar URL http(s) air a luchdadh a-nuas leis na h-endpoints seo agus tha e a’ cunntadh 1,024. |
Eisimpleir: tha dealbh 1,024 × 768 piogsail a’ cunntadh ceil(1024 / 28) × ceil(768 / 28) = 37 × 28 = 1,036 tokens.
An cunntas agus dè a thèid a chosg air iarrtas
Thèid cunntas iarrtais slàn a dhèanamh san aon dòigh ri cunntas cuir-a-steach iarrtais fhìor leis an aon mhodail, teachdaireachdan agus innealan. Bidh freagairt ag aithris an àireamh sin mar usage.prompt_tokens air Chat Completions, mar usage.input_tokens air Responses, agus mar usage.input_tokens plus usage.cache_read_input_tokens air Messages.
- Is e an cunntas an cuir-a-steach ron lasachadh cuir-a-steach cached. Faodaidh iarrtas fìor pàirt dhen chuir-a-steach sin a leughadh o cache agus a bhilleadh aig reat cached. Caching prompt
- Tha dealbh a thèid a thoirt mar URL
http(s)a’ cunntadh 1,024 an seo. Luchdaichidh iarrtas fìor an dealbh a-nuas agus cunntaidh e o mheud ann am piogsailean, agus mar sin faodaidh an dà àireamh a bhith eadar-dhealaichte. Cuir an dealbh mar base64 gus an aon àireamh fhaighinn. - Chan eil cuir-a-mach na phàirt dhen chunntas. Thèid freagairt iarrtais fhìor a bhilleadh mar tokens cuir-a-mach a bharrachd, reasoning cuimsichte.
- Chan eil cruth chòmhraidh aig cunntas
text. Cleachd e gus sgrìobhainn no pàirt de phrompt a thomhas, agus am foirmmessagesgus iarrtas a thomhas.
Gus cunntas atharrachadh gu cosgais, iomadaich e le prìs cuir-a-steach a’ mhodail gach 1M tokens. Modailean & prìsean
Crìochan
| Crìoch | Luach | Os a chionn |
|---|---|---|
Fad text | 4,000,000 bytes (UTF-8) | 413 leis an teachdaireachd text too long |
| Bodhaig an iarrtais | 32 MiB | 413 |
| Gach iarrtas | Aon teacsa no aon chòmhradh | Cuir aon iarrtas gach teacsa gus grunn theacsaichean a chunntadh. |
Chan eil gairmean cunntaidh air an cunntadh a dh’ionnsaigh na crìche de 120 iarrtas gach mionaid. Crìochan agus balance
Mearachdan
| Staid | Seòrsa | Teachdaireachd | Cuin |
|---|---|---|---|
400 | invalid_request_error | tokenize is available for the hosted open models; unknown model: <model> | /v1/tokenize le model nach e id open-weight hosted a th’ ann. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <model> | /v1/messages/count_tokens le model nach e id open-weight hosted a th’ ann, no gun model. |
400 | invalid_request_error | send `text` or `messages` | /v1/tokenize gun text no messages. |
401 | authentication_error | Missing authentication / Invalid API key | Cha deach iuchair a chur, no chan eil an iuchair dligheach. |
413 | invalid_request_error | text too long | Tha text nas fhaide na 4,000,000 bytes. Thèid bodhaig os cionn 32 MiB a fhreagairt le 413 cuideachd. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Chan eil seòrsa susbaint JSON aig an iarrtas. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Tha raon riatanach a dhìth (model air /v1/tokenize, messages air /v1/messages/count_tokens) no tha seòrsa ceàrr air raon. |
503 | api_error | token counting is temporarily unavailable for this model | Chan urrainnear an cunntas a dhèanamh airson a’ mhodail seo an-dràsta. Feuch a-rithist nas fhaide air adhart. |
Tillidh /v1/tokenize mearachdan ann an cruth OpenAI. Air /v1/messages/count_tokens thig mearachdan an endpoint fhèin (400 airson a’ mhodail, 503) ann an cruth Anthropic, agus thig 401, 413, 415 agus 422 ann an cruth OpenAI. Leugh còd na staid an toiseach, an uair sin error.type agus error.message, a tha an dà chuid anns an dà chruth.
{
"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"
}
}