Anthropic
Mae POST /v1/messages yn cymryd cais yn fformat Anthropic Messages ac yn ateb yn yr un fformat, wedi'i ffrydio ai peidio. Defnyddiwch ef gyda'r SDK Anthropic a chydag offer sydd wedi'u hadeiladu arno, megis Claude Code: gosodwch yr URL sylfaenol, defnyddiwch eich allwedd API Shannon ac enwch id model Shannon.
POST https://api.shannon-ai.com/v1/messages
Pwyntiwch yr SDK Anthropic at URL sylfaenol Shannon. Mae'r SDK yn ychwanegu /v1/messages ac yn anfon eich allwedd yn y pennawd x-api-key.
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com",
)
message = client.messages.create(
model="shannon-3",
max_tokens=1024,
messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
for block in message.content:
if block.type == "text":
print(block.text) import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.shannon-ai.com",
});
const message = await client.messages.create({
model: "shannon-3",
max_tokens: 1024,
messages: [{ role: "user", content: "Say hello in one sentence." }],
});
for (const block of message.content) {
if (block.type === "text") console.log(block.text);
} curl https://api.shannon-ai.com/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "content-type: application/json" \
-d '{
"model": "shannon-3",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Say hello in one sentence."}]
}' Neges yw'r ateb y mae ei content yn rhestr o flociau. Dewch o hyd i'r blociau yn ôl eu type: pan fu'r model yn rhesymu, y bloc cyntaf yw thinking ac mae'r testun yn dod ar ei ôl.
{
"id": "msg_5f0c1e7a9b3d4c62a8e1f07d2b46c9a3",
"type": "message",
"role": "assistant",
"model": "shannon-3",
"content": [
{
"type": "thinking",
"thinking": "The user wants a greeting in one sentence. Keep it short and friendly."
},
{
"type": "text",
"text": "Hello, it is good to meet you."
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 1184,
"output_tokens": 46
}
} Penawdau
| Pennawd | Gwerth | Disgrifiad |
|---|---|---|
x-api-key | YOUR_API_KEY | Eich allwedd API. Derbynnir Authorization: Bearer YOUR_API_KEY yn ei lle. |
content-type | application/json | Gofynnol. |
anthropic-version | 2023-06-01 | Yn cael ei dderbyn, nid yw'n ofynnol. Mae'r SDK Anthropic yn anfon 2023-06-01; mae gan yr ateb yr un fformat beth bynnag fo'r gwerth. |
anthropic-beta | Yn cael ei dderbyn, nid yw'n ofynnol. | |
x-request-id | Dewisol. Eich id eich hun ar gyfer y cais. Daw yn ôl heb ei newid ar yr ateb; hebddo mae gan yr ateb id o 12 nod hecsadegol. |
Meysydd y cais
Dim ond messages sy'n ofynnol. Mae'r golofn Yn cael ei gymhwyso gan yn enwi'r modelau lle mae maes yn newid yr ateb. Y modelau pwysau agored a gynhelir yw deuddeg id y rhestr fodelau; teulu Shannon 3 yw shannon-3, shannon-3-pro, shannon-3.1 a shannon-3.1-pro. Modelau a phrisiau
| Maes | Math | Rhagosodiad | Disgrifiad | Yn cael ei gymhwyso gan |
|---|---|---|---|---|
model | string | shannon-1.6-lite | Id model Shannon o'r rhestr fodelau. Anfonwch ef gyda phob cais. Nid yw'r paru yn sensitif i briflythrennau. Mae unrhyw enw arall yn dychwelyd 400 unknown model, felly mae'n rhaid gosod offeryn sy'n anfon ei enwau model ei hun i id Shannon. | Pob model |
messages | array | Gofynnol. Y sgwrs, y neges hynaf yn gyntaf, gyda'r rolau user ac assistant. Gweler Negeseuon a blociau cynnwys isod. | Pob model | |
system | string | array | Cyfarwyddiadau i'r model: llinyn, neu arae o flociau testun. Gweler Prompt system isod. | Modelau pwysau agored a gynhelir, shannon-1.6-*, shannon-2-*, shannon-coder-1 | |
max_tokens | integer | 4096 | Terfyn uchaf yr ateb, mewn tokenau. Dewisol ar y pwynt terfyn hwn; mae'r SDK Anthropic bob amser yn ei anfon. Symudir gwerth y tu allan i 1 i 65,536 i mewn i'r ystod honno. Hefyd dyma'r swm a neilltuir o'ch balans tra bydd y cais yn rhedeg. | Modelau pwysau agored a gynhelir, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1 |
stream | boolean | false | Mae true yn anfon yr ateb fel digwyddiadau a anfonir gan y gweinydd. Gweler Ffrydio isod. | Pob model |
temperature | number | Tymheredd samplu. Ar y modelau pwysau agored a gynhelir cedwir y gwerthoedd rhwng 0 a 2. | Modelau pwysau agored a gynhelir, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1 | |
top_p | number | Samplu cnewyllyn. | Modelau pwysau agored a gynhelir | |
stop_sequences | array | Hyd at 4 llinyn. Daw'r ateb i ben cyn y cyntaf sy'n ymddangos. Mae stop_reason yn aros yn end_turn. | Modelau pwysau agored a gynhelir | |
tools | array | Yr offer y caiff y model eu galw, pob un fel {"name", "description", "input_schema"}. Gweler Offer isod. | Pob model | |
tool_choice | object | Mae {"type": "auto"} yn gadael i'r model benderfynu, mae {"type": "any"} yn gwneud iddo alw offeryn, mae {"type": "tool", "name": "…"} yn gwneud iddo alw'r offeryn hwnnw. | Modelau pwysau agored a gynhelir | |
thinking | object | Faint mae'r model yn rhesymu cyn iddo ateb. Gweler Meddwl isod. | Modelau pwysau agored a gynhelir | |
web_search | boolean | false | Ychwanegiad gan Shannon at y fformat: mae true yn gadael i'r model chwilio'r we cyn iddo ateb. | shannon-1.6-*, shannon-2-*, teulu Shannon 3 |
Heb max_tokens y gwerth yw 4,096, a 65,536 ar shannon-coder-1.
Derbynnir metadata, top_k, service_tier a marcwyr cache_control ar flociau fel bod cod cleient presennol yn rhedeg heb newid. Nid ydynt yn newid yr ateb; mae caching prompt ar y modelau pwysau agored a gynhelir yn awtomatig.
Gofynnir am ateb JSON yn ôl sgema gyda response_format ar Chat Completions a chyda text.format ar Responses. Allbynnau strwythuredig
Cais gyda prompt system a meddwl
Mae'r cais hwn yn anfon prompt system, cyllideb meddwl, tymheredd a dilyniant stopio. Mae'n defnyddio model pwysau agored a gynhelir, sy'n cymhwyso pob un ohonynt.
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com",
)
message = client.messages.create(
model="DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
max_tokens=4096,
system="You are a physics teacher. Answer in two sentences.",
thinking={"type": "enabled", "budget_tokens": 2048},
temperature=0.3,
stop_sequences=["\n\n"],
messages=[{"role": "user", "content": "Why is the sky blue?"}],
)
for block in message.content:
if block.type == "thinking":
print("reasoning:", block.thinking)
elif block.type == "text":
print("answer:", block.text)
print(message.usage) import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.shannon-ai.com",
});
const message = await client.messages.create({
model: "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
max_tokens: 4096,
system: "You are a physics teacher. Answer in two sentences.",
thinking: { type: "enabled", budget_tokens: 2048 },
temperature: 0.3,
stop_sequences: ["\n\n"],
messages: [{ role: "user", content: "Why is the sky blue?" }],
});
for (const block of message.content) {
if (block.type === "thinking") console.log("reasoning:", block.thinking);
if (block.type === "text") console.log("answer:", block.text);
}
console.log(message.usage); curl https://api.shannon-ai.com/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "content-type: application/json" \
-d '{
"model": "DeepSeek-V4-Flash-0731-W4A16-AUTOROUND-REAP",
"max_tokens": 4096,
"system": "You are a physics teacher. Answer in two sentences.",
"thinking": {"type": "enabled", "budget_tokens": 2048},
"temperature": 0.3,
"stop_sequences": ["\n\n"],
"messages": [{"role": "user", "content": "Why is the sky blue?"}]
}' Ar y modelau pwysau agored a gynhelir mae usage yn adrodd y tokenau a ddarllenwyd o'r cache prompt ar wahân i'r tokenau mewnbwn eraill.
{
"usage": {
"input_tokens": 31,
"cache_read_input_tokens": 0,
"cache_creation_input_tokens": 0,
"output_tokens": 62
}
} Prompt system
Mae system yn llinyn, neu'n arae o flociau testun ar y ffurf [{"type": "text", "text": "…"}]. Cysylltir testunau'r blociau â thoriad llinell.
Fe'i cymhwysir gan y modelau pwysau agored a gynhelir, shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite, shannon-2-pro a shannon-coder-1. Gydag id o deulu Shannon 3, rhowch gyfarwyddiadau y mae'n rhaid iddynt ddal yn y neges defnyddiwr.
Negeseuon a blociau cynnwys
Mae gan neges role, sef user neu assistant, a content. Mae content yn llinyn, neu'n arae o flociau.
| Bloc | Disgrifiad | Ar gael ar |
|---|---|---|
{"type": "text", "text": "…"} | Testun. | Pob model |
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "…"}}{"type": "image", "source": {"type": "url", "url": "…"}} | Delwedd mewn neges user, fel base64 neu drwy URL. | Teulu Shannon 3, shannon-1.6-lite, shannon-1.6-pro, a'r modelau pwysau agored a gynhelir sy'n rhestru mewnbwn delweddau |
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": "…"}} | Dogfen (PDF, Word, PowerPoint neu Excel) mewn neges user, fel base64 neu drwy URL. | Teulu Shannon 3 |
{"type": "tool_use", "id": "toolu_…", "name": "…", "input": {…}} | Mewn neges assistant: galwad offeryn a wnaeth y model mewn ateb cynharach. | Pob model |
{"type": "tool_result", "tool_use_id": "toolu_…", "content": "…"} | Mewn neges user: canlyniad yr alwad honno. Mae content yn llinyn, neu'n rhestr o flociau testun. | Pob model |
Gallwch anfon content ateb cynharach yn ôl fel y mae: darllenir blociau text a tool_use, hepgorir blociau thinking.
Ar haenau Shannon darllenir delweddau a dogfennau o'r neges user olaf. Mae gan feintiau a therfynau eu tudalen eu hunain. Delweddau a ffeiliau
Meddwl
Mae'r modelau pwysau agored a gynhelir yn mapio thinking i ymdrech rhesymu. Heb y maes mae'r ymdrech yn uchel.
| Ffurf | budget_tokens | Effaith |
|---|---|---|
{"type": "disabled"} | Dim rhesymu. Nid oes gan yr ateb floc thinking. | |
{"type": "enabled", "budget_tokens": 2048} | ≤ 2,048 | Ymdrech isel. |
{"type": "enabled", "budget_tokens": 8192} | ≤ 8,192 | Ymdrech ganolig. |
{"type": "enabled", "budget_tokens": 16000} | > 8,192 | Ymdrech uchel. |
Mae model sy'n rhesymu yn dychwelyd ei resymu fel bloc {"type": "thinking", "thinking": "…"} o flaen y testun. Mae'r haenau Shannon sy'n rhesymu yn ei anfon hefyd.
Disgrifir yr ymdrechion ar eu tudalen eu hunain. Ymdrech rhesymu
Offer
Ffwythiant y mae eich cod yn ei redeg yw offeryn. Disgrifiwch bob un gydag name, description ac input_schema. Pan fydd y model eisiau offeryn, mae'r ateb yn cynnwys bloc tool_use ac mae stop_reason yn tool_use.
Diffiniad offeryn yn tools:
[
{
"name": "get_weather",
"description": "Current weather for a city.",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
] content ateb sy'n ei alw:
[
{
"type": "text",
"text": ""
},
{
"type": "tool_use",
"id": "toolu_3d9a7c1e5b2f4a60c8e1d7f09b24a6c5",
"name": "get_weather",
"input": {
"city": "Paris"
}
}
] Rhedwch yr offeryn, yna anfonwch y cais eto gyda'r alwad a bloc tool_result wedi'u hychwanegu at messages:
[
{
"role": "user",
"content": "What is the weather in Paris?"
},
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "toolu_3d9a7c1e5b2f4a60c8e1d7f09b24a6c5",
"name": "get_weather",
"input": {
"city": "Paris"
}
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_3d9a7c1e5b2f4a60c8e1d7f09b24a6c5",
"content": "{\"temperature_c\": 18, \"sky\": \"cloudy\"}"
}
]
}
] tool_choice
| Ffurf | Effaith | Yn cael ei gymhwyso gan |
|---|---|---|
{"type": "auto"} | Mae'r model yn penderfynu a ddylai alw offeryn. Dyma'r rhagosodiad. | Pob model |
{"type": "any"} | Rhaid i'r model alw un o'r offer. | Modelau pwysau agored a gynhelir |
{"type": "tool", "name": "…"} | Rhaid i'r model alw'r offeryn a enwir. | Modelau pwysau agored a gynhelir |
Dim ond offer ag input_schema a ddarllenir. Nid yw chwilio'r we yn offeryn ar y pwynt terfyn hwn: gosodwch y maes web_search.
Disgrifir y ddolen offer yn llawn ar ei thudalen ei hun. Galw swyddogaeth
Yr ateb
| Maes | Math | Disgrifiad | Yn cael ei anfon gan |
|---|---|---|---|
id | string | msg_ ac yna 32 nod hecsadegol. | Pob model |
type | string | Bob amser message. | Pob model |
role | string | Bob amser assistant. | Pob model |
model | string | Id canonaidd y model a atebodd. | Pob model |
content | array | Blociau'r ateb, yn y drefn hon: thinking pan fu'r model yn rhesymu, text, ac yna un tool_use ar gyfer pob galwad offeryn. | Pob model |
stop_reason | string | Pam y daeth yr ateb i ben. Gweler y gwerthoedd isod. | Pob model |
usage | object | input_tokens ac output_tokens. | Pob model |
sources | array | Dim ond ar gais gyda web_search: true y daeth ei chwiliad o hyd i rywbeth: y canlyniadau a roddwyd i'r model, pob un gydag index, title ac url. Mae [1] yn yr ateb yn gofnod gydag index 1. Mewn ffrwd daw gyda'r digwyddiad message_delta. | shannon-1.6-*, shannon-2-*, teulu Shannon 3 |
usage.cache_read_input_tokens | integer | cache_read_input_tokens yw'r rhan o'r prompt a ddarllenwyd o'r cache; input_tokens yw'r gweddill. Mae cache_creation_input_tokens bob amser yn 0. | Modelau pwysau agored a gynhelir |
Blociau'r ateb
| Bloc | Disgrifiad |
|---|---|
{"type": "thinking", "thinking": "…"} | Rhesymu'r model. Nid oes aelod signature. |
{"type": "text", "text": "…"} | Testun yr ateb. Gall fod yn wag pan fo'r ateb yn alwad offeryn. |
{"type": "tool_use", "id": "toolu_…", "name": "…", "input": {…}} | Galwad offeryn. Mae input yn wrthrych JSON. Anfonwch y canlyniad yn ôl gyda'r un id. |
stop_reason
| stop_reason | Disgrifiad | Yn cael ei anfon gan |
|---|---|---|
end_turn | Gorffennodd y model ei ateb, neu ymddangosodd un o'ch stop_sequences. | Pob model |
tool_use | Mae'r ateb yn cynnwys un neu fwy o alwadau offeryn. | Pob model |
max_tokens | Torrwyd yr ateb ar y terfyn allbwn. | Teulu Shannon 3, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1, mewn ffrydiau |
Ffrydio
Gosodwch stream i true ar gyfer digwyddiadau a anfonir gan y gweinydd. Mae gan bob ffrâm linell event: gydag enw'r digwyddiad a llinell data: gyda gwrthrych JSON sy'n ailadrodd yr enw yn type. Mae cynorthwywyr ffrydio'r SDK Anthropic yn darllen y digwyddiadau hyn i chi:
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com",
)
with client.messages.stream(
model="shannon-3",
max_tokens=1024,
messages=[{"role": "user", "content": "Explain server-sent events in three sentences."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
message = stream.get_final_message()
print()
print(message.stop_reason, message.usage) import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.shannon-ai.com",
});
const stream = client.messages.stream({
model: "shannon-3",
max_tokens: 1024,
messages: [{ role: "user", content: "Explain server-sent events in three sentences." }],
});
stream.on("text", (text) => process.stdout.write(text));
const message = await stream.finalMessage();
console.log("\n", message.stop_reason, message.usage); curl -N https://api.shannon-ai.com/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "content-type: application/json" \
-d '{
"model": "shannon-3",
"max_tokens": 1024,
"stream": true,
"messages": [{"role": "user", "content": "Explain server-sent events in three sentences."}]
}' Y digwyddiadau, yn y drefn y maent yn cyrraedd:
| Digwyddiad | Data | Pryd |
|---|---|---|
message_start | message | Bob amser yn gyntaf. Mae message yn cynnwys yr id, y rôl, y model, content gwag a usage sy'n dechrau ar sero. |
content_block_start | index, content_block | Mae bloc yn agor. Mae content_block yn floc text neu thinking gwag, neu'n floc tool_use gyda'i id a'i name. |
content_block_delta | index, delta {"type": "thinking_delta", "thinking"} | Darn o'r rhesymu, ar gyfer bloc thinking. |
content_block_delta | index, delta {"type": "text_delta", "text"} | Darn o'r ateb, ar gyfer bloc text. |
content_block_delta | index, delta {"type": "input_json_delta", "partial_json"} | Dadleuon galwad offeryn, yn gyflawn mewn un delta, ar gyfer bloc tool_use. |
content_block_stop | index | Mae'r bloc gyda'r index hwn yn gyflawn. |
message_delta | delta {stop_reason, stop_sequence}, usage | Diwedd yr ateb: delta.stop_reason, a usage y cais. |
message_stop | Bob amser yn olaf. |
Rhifir blociau yn ôl index yn y drefn y maent yn agor, ac mae un bloc yn gyflawn cyn i'r nesaf agor. Darllenwch bob bloc yn ôl ei fath, nid yn ôl ei rif: gall ffrwd agor bloc text gwag yn gyntaf, yna'r bloc thinking, a chyflwyno'r ateb mewn bloc text diweddarach.
event: message_start
data: {"type":"message_start","message":{"id":"msg_5f0c…","type":"message","role":"assistant","model":"shannon-3","content":[],"usage":{"input_tokens":0,"output_tokens":0}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: content_block_start
data: {"type":"content_block_start","index":1,"content_block":{"type":"thinking","thinking":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"thinking_delta","thinking":"Three sentences: what it is, how it is framed, why it is used."}}
event: ping
data: {"type": "ping"}
event: content_block_stop
data: {"type":"content_block_stop","index":1}
event: content_block_start
data: {"type":"content_block_start","index":2,"content_block":{"type":"text","text":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":2,"delta":{"type":"text_delta","text":"Server-sent events let a server push text over one HTTP reply."}}
event: content_block_stop
data: {"type":"content_block_stop","index":2}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":1190,"output_tokens":84}}
event: message_stop
data: {"type":"message_stop"} Cadw'n fyw
| Yn cael ei anfon gan | Disgrifiad |
|---|---|
Teulu Shannon 3, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1 | Digwyddiad ping gyda'r data {"type": "ping"}, bob 15 eiliad. |
| Modelau pwysau agored a gynhelir | Llinell sylw : keepalive, bob 15 eiliad tra bo'r model yn gweithio. |
Ffrwd sy'n methu
Mae methiant ar ôl i'r ffrwd ddechrau yn ddigwyddiad error gyda'r un corff ag ateb gwall: {"type": "error", "error": {"type", "message"}}. Mae'r digwyddiadau cau, message_delta a message_stop, yn dal i ddilyn. Ystyriwch yr ateb wedi methu ac anfonwch y cais eto.
Gwallau
Mae gwallau'n defnyddio corff gwall Anthropic: mae type yn error, ac mae'r gwrthrych error yn cynnwys type a message. Mae'r tabl yn rhestru'r hyn y mae'r pwynt terfyn hwn yn ei ddychwelyd amlaf. Mae gan y rhestr lawn, gyda'r hyn i'w ailgeisio, ei thudalen ei hun. Rheoli gwallau
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "unknown model: no-such-model"
}
} | Statws | Math | Neges | Pryd |
|---|---|---|---|
401 | authentication_error | Missing authenticationInvalid API key | Ni anfonwyd allwedd API, neu mae'r allwedd yn anhysbys neu wedi'i dirymu. |
400 | invalid_request_error | unknown model: <id> | Nid yw model yn id model Shannon. |
400 | invalid_request_error | No user message provided | Haenau Shannon: nid oes gan y cais destun defnyddiwr na tools. |
400 | invalid_request_error | <id> does not accept image input | Anfonwyd delwedd at fodel pwysau agored a gynhelir heb fewnbwn delweddau. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Mae messages ar goll, neu mae gan faes y math JSON anghywir. |
429 | rate_limit_error | Quota exceeded. Upgrade your plan at shannon-ai.com/plan | Mae max_tokens yn fwy na'r hyn sydd ar ôl o'ch balans. |
429 | rate_limit_error | Too many requests. Retry in <n>s. | Amddiffyniad rhag llifogydd: mwy na 120 o geisiadau mewn un munud ar eich cyfrif. |
429 | rate_limit_error | Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan | shannon-coder-1: mae galwadau Shannon Coder eich cynllun ar gyfer y ffenestr 4 awr gyfredol wedi'u defnyddio. |
500 | server_error | The model backend failed to answer. Please retry. | Ni chynhyrchodd y model ateb. Anfonwch y cais eto. |
502 | api_error | The model backend failed to answer. Please retry. | Ni chynhyrchodd y model ateb. Anfonwch y cais eto. |
Cyfrif tokenau
Mae POST /v1/messages/count_tokens yn cymryd yr un corff â chais neges (model, system, messages, tools) ac yn dychwelyd {"input_tokens": n}: nifer y tokenau mewnbwn y byddai'r cais yn cael ei godi amdanynt. Mae'n cyfrif ar gyfer y modelau pwysau agored a gynhelir, mae'n rhad ac am ddim ac nid yw amddiffyniad rhag llifogydd yn ei gyfrif.
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 physics teacher. Answer in two sentences.",
messages=[{"role": "user", "content": "Why is the sky blue?"}],
)
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 physics teacher. Answer in two sentences.",
messages: [{ role: "user", content: "Why is the sky blue?" }],
});
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 physics teacher. Answer in two sentences.",
"messages": [{"role": "user", "content": "Why is the sky blue?"}]
}'
# {"input_tokens": 31} Mae gan gyfrif tokenau ei dudalen ei hun, gyda'r pwynt terfyn testun plaen a'r terfynau. Cyfrif tokenau