Cyfrif tokenau
Cyfrifwch docenau testun neu gais cyfan cyn i chi ei anfon.
POST https://api.shannon-ai.com/v1/tokenize
POST https://api.shannon-ai.com/v1/messages/count_tokens
Mae'r ddau bwynt terfyn yn cyfrif gyda tokenizer y model rydych yn ei enwi, ac nid oes model yn rhedeg. Maent yn cwmpasu'r modelau pwysau agored a gynhelir. Mae /v1/tokenize yn cymryd testun plaen neu sgwrs Chat Completions. Mae /v1/messages/count_tokens yn cymryd cais yn fformat Anthropic Messages, sef yr alwad y mae SDK Anthropic a Claude Code yn ei gwneud.
Mae cyfrif am ddim. Mae galwad angen eich allwedd API, nid yw'n cymryd dim o'ch balans ac nid yw'n ymddangos yn eich log defnydd.
Cyfrif testun
Anfonwch model a text. Cyfrifir y testun fel y mae, heb fformatio sgwrs o'i gwmpas.
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
} Enghreifftiau yw'r rhifau yn yr atebion ar y dudalen hon. Mae'r un testun yn rhoi cyfrif gwahanol ar fodel gwahanol.
Cyfrif cais sgwrs
Anfonwch model a messages, gyda tools pan fo'r cais yn eu cynnwys, yn union fel yr anfonech hwy i /v1/chat/completions. Yr ateb yw maint y mewnbwn cyfan.
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
} Meysydd /v1/tokenize
| Maes | Math | Disgrifiad |
|---|---|---|
model | string | Gofynnol. Id model pwysau agored a gynhelir. Trinnir priflythrennau a llythrennau bach yr un fath. |
text | string | Testun i'w gyfrif fel y mae, heb fformatio sgwrs. Hyd at 4,000,000 beit. Anfonwch text neu messages; pan fo'r ddau'n bresennol, cyfrifir text. |
messages | array | Negeseuon sgwrs yn fformat Chat Completions. Fe'u cyfrifir fel mewnbwn llawn cais: pob neges gyda'r fformatio y mae templed sgwrs y model yn ei roi o'i chwmpas. |
tools | array | Diffiniadau offer i'w cynnwys yn y cyfrif. Defnyddir gyda messages. |
Gwrthrych JSON yw'r ateb gyda'r meysydd hyn:
| Maes | Math | Disgrifiad |
|---|---|---|
model | string | Id y model y gwnaed y cyfrif ar ei gyfer, yn ei sillafiad cyhoeddedig. |
tokens | integer | Gyda text: tokenau'r testun. Gyda messages: tokenau'r mewnbwn cyfan, delweddau yn gynwysedig. |
Cyfrif cais Messages
Anfonwch y corff y byddech yn ei anfon i /v1/messages: model, messages, a system a tools pan fyddwch yn eu defnyddio. Mae SDKs swyddogol Anthropic yn galw'r pwynt terfyn hwn drwy 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
} Meysydd /v1/messages/count_tokens
| Maes | Math | Disgrifiad |
|---|---|---|
model | string | Gofynnol. Id model pwysau agored a gynhelir. |
messages | array | Gofynnol. Negeseuon yn fformat Anthropic Messages. Cyfrifir blociau text, image, tool_use a tool_result. |
system | string | array | Y system prompt: llinyn neu arae o flociau testun. |
tools | array | Diffiniadau offer gyda name, description ac input_schema. |
Derbynnir er mwyn cydnawsedd, heb effaith ar y cyfrif: tool_choice, max_tokens, temperature, top_p, stop_sequences, stream, thinking. Gallwch basio corff cais go iawn heb ei newid.
Gwrthrych JSON yw'r ateb gyda'r meysydd hyn:
| Maes | Math | Disgrifiad |
|---|---|---|
input_tokens | integer | Tokenau'r mewnbwn cyfan: system prompt, negeseuon, offer a delweddau. |
Modelau a gefnogir
Mae'r ddau bwynt terfyn yn cyfrif ar gyfer y modelau pwysau agored a gynhelir. Mae GET /v1/models yn rhestru /v1/tokenize a /v1/messages/count_tokens yn endpoints pob model sy'n eu cefnogi. Atebir unrhyw werth model arall, ids Shannon yn gynwysedig, â 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
Ar gyfer model Shannon, darllenwch y cyfrifon tokenau o wrthrych usage ateb.
Sut mae'r cyfrif yn cael ei wneud
Cyfrifir pob model gyda'i tokenizer ei hun a'i dempled sgwrs ei hun. Ni ddefnyddir unrhyw amcangyfrif o nodau neu eiriau.
| Beth a gyfrifir | Rheol |
|---|---|
| Testun | Tokenau'r llinyn fel yr anfonwyd. Mae llinyn gwag yn cyfrif 0. |
| Negeseuon | Gosodir y negeseuon a'r offer allan gyda thempled sgwrs y model ei hun, hyd at y pwynt lle mae'r ateb yn dechrau, a chyfrifir y prompt cyfan hwnnw. |
| Rolau | Cyfrifir negeseuon system, user, assistant a tool. Cyfrifir developer fel system. Nid yw neges heb gynnwys a heb alwad offeryn yn ychwanegu dim. |
| Galwadau offer a chanlyniadau | Mae galwadau offer troeon assistant cynharach a'u canlyniadau yn rhan o'r cyfrif, ar y ddau bwynt terfyn. |
| Delweddau | Mae delwedd a anfonir y tu mewn i'r corff (base64 neu URL data:) yn ychwanegu un tocen am bob darn picsel 28 × 28: ceil(width / 28) × ceil(height / 28). Nid yw'r pwyntiau terfyn hyn yn lawrlwytho delwedd a roddir fel URL http(s) ac mae'n cyfrif 1,024. |
Enghraifft: mae delwedd 1,024 × 768 picsel yn cyfrif ceil(1024 / 28) × ceil(768 / 28) = 37 × 28 = 1,036 o docenau.
Y cyfrif a beth a godir ar gais
Gwneir cyfrif cais cyfan yn yr un modd â chyfrif mewnbwn cais go iawn gyda'r un model, negeseuon ac offer. Mae ateb yn adrodd y rhif hwnnw fel usage.prompt_tokens ar Chat Completions, fel usage.input_tokens ar Responses, ac fel usage.input_tokens ynghyd â usage.cache_read_input_tokens ar Messages.
- Y cyfrif yw'r mewnbwn cyn y gostyngiad mewnbwn cached. Gall cais go iawn ddarllen rhan o'r mewnbwn hwnnw o'r cache a chodi'r rhan honno ar y gyfradd cached. Caching prompt
- Mae delwedd a roddir fel URL
http(s)yn cyfrif 1,024 yma. Mae cais go iawn yn lawrlwytho'r ddelwedd ac yn ei chyfrif o'i maint mewn picseli, felly gall y ddau rif fod yn wahanol. Anfonwch y ddelwedd fel base64 i gael yr un rhif. - Nid yw allbwn yn rhan o'r cyfrif. Codir ateb cais go iawn fel tokenau allbwn ar ben hynny, rhesymu yn gynwysedig.
- Nid oes gan gyfrif
textfformatio sgwrs. Defnyddiwch ef i fesur dogfen neu ran o brompt, a'r ffurfmessagesi fesur cais.
I droi cyfrif yn gost, lluoswch ef â phris mewnbwn y model am bob 1M o docenau. Modelau a phrisiau
Terfynau
| Terfyn | Gwerth | Uwchlaw hynny |
|---|---|---|
Hyd text | 4,000,000 beit (UTF-8) | 413 gyda'r neges text too long |
| Corff y cais | 32 MiB | 413 |
| Fesul cais | Un testun neu un sgwrs | Anfonwch un cais am bob testun i gyfrif sawl testun. |
Nid yw galwadau cyfrif yn cyfrif tuag at y terfyn o 120 cais y funud. Terfynau a balans
Gwallau
| Statws | Math | Neges | Pryd |
|---|---|---|---|
400 | invalid_request_error | tokenize is available for the hosted open models; unknown model: <model> | /v1/tokenize gyda model nad yw'n id model pwysau agored a gynhelir. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <model> | /v1/messages/count_tokens gyda model nad yw'n id model pwysau agored a gynhelir, neu heb model. |
400 | invalid_request_error | send `text` or `messages` | /v1/tokenize heb text na messages. |
401 | authentication_error | Missing authentication / Invalid API key | Ni anfonwyd allwedd, neu nid yw'r allwedd yn ddilys. |
413 | invalid_request_error | text too long | Mae text yn hirach na 4,000,000 beit. Atebir corff dros 32 MiB â 413 hefyd. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Nid oes gan y cais fath cynnwys JSON. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Mae maes gofynnol ar goll (model ar /v1/tokenize, messages ar /v1/messages/count_tokens) neu mae gan faes y math anghywir. |
503 | api_error | token counting is temporarily unavailable for this model | Ni ellir gwneud y cyfrif ar gyfer y model hwn ar hyn o bryd. Rhowch gynnig arall arni yn nes ymlaen. |
Mae /v1/tokenize yn dychwelyd gwallau yn y siâp OpenAI. Ar /v1/messages/count_tokens mae gwallau'r pwynt terfyn ei hun (400 am y model, 503) yn dod yn y siâp Anthropic, ac mae 401, 413, 415 a 422 yn dod yn y siâp OpenAI. Darllenwch y cod statws yn gyntaf, yna error.type ac error.message, sy'n bresennol yn y ddau siâp.
{
"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"
}
}