ટોકન ગણતરી
ટેક્સ્ટના કે આખી રિક્વેસ્ટના ટોકન્સ મોકલતા પહેલાં ગણો.
POST https://api.shannon-ai.com/v1/tokenize
POST https://api.shannon-ai.com/v1/messages/count_tokens
બંને એન્ડપોઇન્ટ્સ તમે જે મોડેલ નામ આપો તેના tokenizer વડે ગણે છે, અને કોઈ મોડેલ ચાલતું નથી. તેઓ હોસ્ટેડ open-weight મોડેલ્સને આવરી લે છે. /v1/tokenize સાદો ટેક્સ્ટ અથવા Chat Completions વાતચીત લે છે. /v1/messages/count_tokens Anthropic Messages ફોર્મેટમાં રિક્વેસ્ટ લે છે, જે કોલ Anthropic SDK અને Claude Code કરે છે.
ગણતરી મફત છે. કોલ માટે તમારી API કી જોઈએ, તે તમારા બેલેન્સમાંથી કંઈ લેતો નથી અને તમારા વપરાશ લોગમાં દેખાતો નથી.
ટેક્સ્ટ ગણો
model અને text મોકલો. ટેક્સ્ટ જેમ છે તેમ ગણાય છે, તેની આસપાસ ચેટ ફોર્મેટિંગ વિના.
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
} આ પેજ પરના જવાબોમાંની સંખ્યાઓ ઉદાહરણો છે. એ જ ટેક્સ્ટ બીજા મોડેલ પર અલગ ગણતરી આપે છે.
ચેટ રિક્વેસ્ટ ગણો
model અને messages મોકલો, રિક્વેસ્ટમાં હોય તો tools સાથે, બરાબર જેમ તમે /v1/chat/completions ને મોકલો. જવાબ આખા ઇનપુટનું કદ છે.
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
} /v1/tokenize ના ફીલ્ડ્સ
| ફીલ્ડ | પ્રકાર | વર્ણન |
|---|---|---|
model | string | ફરજિયાત. હોસ્ટેડ open-weight મોડેલ id. મોટા અને નાના અક્ષરો એકસરખા ગણાય છે. |
text | string | ચેટ ફોર્મેટિંગ વિના, જેમ છે તેમ ગણવાનો ટેક્સ્ટ. 4,000,000 બાઇટ્સ સુધી. text અથવા messages મોકલો; બંને હોય તો text ગણાય છે. |
messages | array | Chat Completions ફોર્મેટમાં ચેટ મેસેજ. તેઓ રિક્વેસ્ટના પૂરા ઇનપુટ તરીકે ગણાય છે: દરેક મેસેજ, તેની આસપાસ મોડેલનું ચેટ ટેમ્પ્લેટ જે ફોર્મેટિંગ મૂકે છે તેની સાથે. |
tools | array | ગણતરીમાં સામેલ કરવાની ટૂલ વ્યાખ્યાઓ. messages સાથે વપરાય છે. |
જવાબ આ ફીલ્ડ્સ સાથેનો JSON ઓબ્જેક્ટ છે:
| ફીલ્ડ | પ્રકાર | વર્ણન |
|---|---|---|
model | string | જે મોડેલ id માટે ગણતરી થઈ, તેની પ્રકાશિત જોડણીમાં. |
tokens | integer | text સાથે: ટેક્સ્ટના ટોકન્સ. messages સાથે: ઇમેજ સહિત આખા ઇનપુટના ટોકન્સ. |
Messages રિક્વેસ્ટ ગણો
તમે /v1/messages ને જે બોડી મોકલો તે મોકલો: model, messages, અને તમે વાપરતા હો તો system અને tools. સત્તાવાર Anthropic SDKs આ એન્ડપોઇન્ટને 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
} /v1/messages/count_tokens ના રિક્વેસ્ટ ફીલ્ડ્સની યાદી
| ફીલ્ડ | પ્રકાર | વર્ણન |
|---|---|---|
model | string | ફરજિયાત. હોસ્ટેડ open-weight મોડેલ id. |
messages | array | ફરજિયાત. Anthropic Messages ફોર્મેટમાં મેસેજ. text, image, tool_use અને tool_result બ્લોક્સ ગણાય છે. |
system | string | array | system પ્રોમ્પ્ટ: સ્ટ્રિંગ અથવા ટેક્સ્ટ બ્લોક્સનો એરે. |
tools | array | name, description અને input_schema સાથે ટૂલ વ્યાખ્યાઓ. |
સુસંગતતા માટે સ્વીકારાય છે, ગણતરી પર કોઈ અસર વિના: tool_choice, max_tokens, temperature, top_p, stop_sequences, stream, thinking. તમે વાસ્તવિક રિક્વેસ્ટની બોડી બદલ્યા વિના આપી શકો.
જવાબ આ ફીલ્ડ્સ સાથેનો JSON ઓબ્જેક્ટ છે:
| ફીલ્ડ | પ્રકાર | વર્ણન |
|---|---|---|
input_tokens | integer | આખા ઇનપુટના ટોકન્સ: system પ્રોમ્પ્ટ, મેસેજ, ટૂલ્સ અને ઇમેજ. |
સપોર્ટેડ મોડેલ્સ
બંને એન્ડપોઇન્ટ્સ હોસ્ટેડ open-weight મોડેલ્સ માટે ગણે છે. GET /v1/models જે મોડેલ્સ તેમને સપોર્ટ કરે છે તેમના endpoints માં /v1/tokenize અને /v1/messages/count_tokens યાદી કરે છે. બીજી કોઈ પણ model કિંમત, Shannon id સહિત, નો જવાબ 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
Shannon મોડેલ માટે, ટોકન ગણતરી જવાબના usage ઓબ્જેક્ટમાંથી વાંચો.
ગણતરી કેવી રીતે થાય છે
દરેક મોડેલ તેના પોતાના tokenizer અને પોતાના ચેટ ટેમ્પ્લેટ સાથે ગણાય છે. અક્ષરો કે શબ્દો પરથી અંદાજ વાપરવામાં આવતો નથી.
| શું ગણાય છે | નિયમ |
|---|---|
| ટેક્સ્ટ | સ્ટ્રિંગ જેમ મોકલાઈ તેમ તેના ટોકન્સ. ખાલી સ્ટ્રિંગ 0 ગણાય છે. |
| મેસેજ | મેસેજ અને ટૂલ્સ મોડેલના પોતાના ચેટ ટેમ્પ્લેટ સાથે ગોઠવાય છે, જ્યાં જવાબ શરૂ થાય ત્યાં સુધી, અને તે આખો પ્રોમ્પ્ટ ગણાય છે. |
| ભૂમિકાઓ | system, user, assistant અને tool મેસેજ ગણાય છે. developer ને system તરીકે ગણવામાં આવે છે. કન્ટેન્ટ અને ટૂલ કોલ વિનાનો મેસેજ કંઈ ઉમેરતો નથી. |
| ટૂલ કોલ્સ અને પરિણામો | અગાઉના assistant ટર્ન્સના ટૂલ કોલ્સ અને તેમના પરિણામો બંને એન્ડપોઇન્ટ્સ પર ગણતરીનો ભાગ છે. |
| ઇમેજ | બોડીની અંદર મોકલેલી ઇમેજ (base64 અથવા data: URL) દરેક 28 × 28 પિક્સેલના પેચ દીઠ એક ટોકન ઉમેરે છે: ceil(width / 28) × ceil(height / 28). http(s) URL તરીકે આપેલી ઇમેજ આ એન્ડપોઇન્ટ્સ ડાઉનલોડ કરતા નથી અને તે 1,024 ગણાય છે. |
ઉદાહરણ: 1,024 × 768 પિક્સેલની ઇમેજ ceil(1024 / 28) × ceil(768 / 28) = 37 × 28 = 1,036 ટોકન્સ ગણાય છે.
ગણતરી અને રિક્વેસ્ટનો ચાર્જ
આખી રિક્વેસ્ટની ગણતરી એ જ મોડેલ, મેસેજ અને ટૂલ્સ સાથેની વાસ્તવિક રિક્વેસ્ટની ઇનપુટ ગણતરી જેવી જ રીતે થાય છે. જવાબ એ સંખ્યા Chat Completions પર usage.prompt_tokens તરીકે, Responses પર usage.input_tokens તરીકે, અને Messages પર usage.input_tokens વત્તા usage.cache_read_input_tokens તરીકે જણાવે છે.
- ગણતરી એ કેશ્ડ-ઇનપુટ ડિસ્કાઉન્ટ પહેલાંનું ઇનપુટ છે. વાસ્તવિક રિક્વેસ્ટ એ ઇનપુટનો કેટલોક ભાગ કેશમાંથી વાંચી શકે છે અને તે ભાગનું બિલ કેશ્ડ દરે બની શકે છે. પ્રોમ્પ્ટ કેશિંગ
http(s)URL તરીકે આપેલી ઇમેજ અહીં 1,024 ગણાય છે. વાસ્તવિક રિક્વેસ્ટ ઇમેજ ડાઉનલોડ કરે છે અને તેને પિક્સેલમાં તેના કદ પરથી ગણે છે, તેથી બંને સંખ્યાઓ અલગ હોઈ શકે. એ જ સંખ્યા મેળવવા ઇમેજને base64 તરીકે મોકલો.- આઉટપુટ ગણતરીનો ભાગ નથી. વાસ્તવિક રિક્વેસ્ટના જવાબનું બિલ ઉપર આઉટપુટ ટોકન્સ તરીકે બને છે, રીઝનિંગ સહિત.
textગણતરીમાં ચેટ ફોર્મેટિંગ હોતું નથી. દસ્તાવેજ કે પ્રોમ્પ્ટનો ભાગ માપવા તેનો ઉપયોગ કરો, અને રિક્વેસ્ટ માપવાmessagesસ્વરૂપનો.
ગણતરીને ખર્ચમાં ફેરવવા, તેને મોડેલના 1M ટોકન્સ દીઠ ઇનપુટ ભાવ વડે ગુણો. મોડેલ્સ અને કિંમત
મર્યાદાઓ
| મર્યાદા | કિંમત | તેની ઉપર |
|---|---|---|
text ની લંબાઈ | 4,000,000 બાઇટ્સ (UTF-8) | 413 અને મેસેજ text too long |
| રિક્વેસ્ટ બોડી | 32 MiB | 413 |
| રિક્વેસ્ટ દીઠ | એક ટેક્સ્ટ અથવા એક વાતચીત | ઘણા ટેક્સ્ટ ગણવા માટે દરેક ટેક્સ્ટ દીઠ એક રિક્વેસ્ટ મોકલો. |
ગણતરીના કોલ્સ દર મિનિટે 120 રિક્વેસ્ટ્સની મર્યાદામાં ગણાતા નથી. મર્યાદાઓ અને બેલેન્સ
એરર્સ
| સ્ટેટસ | પ્રકાર | મેસેજ | ક્યારે |
|---|---|---|---|
400 | invalid_request_error | tokenize is available for the hosted open models; unknown model: <model> | /v1/tokenize એવા model સાથે જે હોસ્ટેડ open-weight id નથી. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <model> | /v1/messages/count_tokens એવા model સાથે જે હોસ્ટેડ open-weight id નથી, અથવા model વિના. |
400 | invalid_request_error | send `text` or `messages` | /v1/tokenize text કે messages બેમાંથી એક પણ વિના. |
401 | authentication_error | Missing authentication / Invalid API key | કોઈ કી મોકલાઈ નથી, અથવા કી માન્ય નથી. |
413 | invalid_request_error | text too long | text 4,000,000 બાઇટ્સથી લાંબો છે. 32 MiB થી મોટી બોડીનો જવાબ પણ 413 થી અપાય છે. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | રિક્વેસ્ટમાં JSON content type નથી. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | જરૂરી ફીલ્ડ ખૂટે છે (/v1/tokenize પર model, /v1/messages/count_tokens પર messages) અથવા ફીલ્ડનો પ્રકાર ખોટો છે. |
503 | api_error | token counting is temporarily unavailable for this model | આ ક્ષણે આ મોડેલ માટે ગણતરી થઈ શકતી નથી. પછીથી ફરી પ્રયાસ કરો. |
/v1/tokenize એરર્સ OpenAI આકારમાં આપે છે. /v1/messages/count_tokens પર એન્ડપોઇન્ટની પોતાની એરર્સ (મોડેલ માટે 400, 503) Anthropic આકારમાં આવે છે, અને 401, 413, 415 અને 422 OpenAI આકારમાં આવે છે. પહેલાં સ્ટેટસ કોડ વાંચો, પછી error.type અને error.message, જે બંને આકારમાં હોય છે.
{
"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"
}
}