Dumphani kupita ku zomwe zili
Anthropic

Anthropic

POST /v1/messages imalandira pempho mu mtundu wa Anthropic Messages ndipo imayankha mu mtundu womwewo, ndi streaming kapena popanda. Igwiritseni ntchito ndi Anthropic SDK ndi zida zomangidwa pa iyo, monga Claude Code: khazikitsani base URL, gwiritsani API key yanu ya Shannon ndipo tchulani model id ya Shannon.

POST https://api.shannon-ai.com/v1/messages

Lozani Anthropic SDK ku base URL ya Shannon. SDK imawonjezera /v1/messages ndipo imatumiza kiyi yanu mu header ya 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)

Yankho ndi message yomwe content yake ndi mndandanda wa ma block. Zindikirani ma block ndi type yawo: pamene model yaganiza, block yoyamba ndi thinking ndipo text imabwera pambuyo pake.

200 JSON
{
  "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
  }
}

Ma header

Header Mtengo Kufotokozera
x-api-key YOUR_API_KEY API key yanu. Authorization: Bearer YOUR_API_KEY imalandiridwa m'malo mwake.
content-type application/json Yofunika.
anthropic-version 2023-06-01 Imalandiridwa, siyofunika. Anthropic SDK imatumiza 2023-06-01; yankho limakhala ndi mtundu womwewo mtengo uliwonse.
anthropic-beta Imalandiridwa, siyofunika.
x-request-id Yosankha. Id yanu ya pempho. Imabwerera yosasinthika mu yankho; popanda iyo, yankho limakhala ndi id ya zilembo 12 za hexadecimal.

Ma field a pempho

messages yokha ndiyo yofunika. Gawo la Imagwira ntchito pa limatchula ma model omwe field imasintha yankho. Ma hosted open-weight model ndi ma id khumi ndi awiri a mndandanda wa ma model; banja la Shannon 3 ndi shannon-3, shannon-3-pro, shannon-3.1 ndi shannon-3.1-pro. Ma model ndi mitengo

Field Mtundu Yokhazikika Kufotokozera Imagwira ntchito pa
model string shannon-1.6-lite Model id ya Shannon yochokera mu mndandanda wa ma model. Itumizeni ndi pempho lililonse. Kufananitsa sikusiyanitsa zilembo zazikulu ndi zazing'ono. Dzina lina lililonse limabweza 400 unknown model, choncho chida chomwe chimatumiza mayina ake a model chiyenera kukhazikitsidwa ku Shannon id. Ma model onse
messages array Yofunika. Kukambirana, uthenga wakale poyamba, ndi ma role user ndi assistant. Onani Messages ndi content blocks pansipa. Ma model onse
system string | array Malangizo a model: string, kapena array ya ma text block. Onani System prompt pansipa. Ma hosted open-weight model, shannon-1.6-*, shannon-2-*, shannon-coder-1
max_tokens integer 4096 Malire apamwamba a yankho, mu ma token. Siyofunika pa endpoint iyi; Anthropic SDK imaitumiza nthawi zonse. Mtengo wopitirira 1 mpaka 65,536 umasunthidwira mkati mwa malirewo. Ndiponso ndiyo ndalama yomwe imasungidwa kuchokera ku balance yanu pamene pempho likuyenda. Ma hosted open-weight model, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1
stream boolean false true imatumiza yankho ngati server-sent events. Onani Streaming pansipa. Ma model onse
temperature number Sampling temperature. Pa ma hosted open-weight model, mitengo imasungidwa pakati pa 0 ndi 2. Ma hosted open-weight model, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1
top_p number Nucleus sampling. Ma hosted open-weight model
stop_sequences array Mpaka ma string 4. Yankho limathera asanafike woyamba amene awonekere. stop_reason imakhalabe end_turn. Ma hosted open-weight model
tools array Ma tool omwe model ingawayitane, iliyonse ngati {"name", "description", "input_schema"}. Onani Tools pansipa. Ma model onse
tool_choice object {"type": "auto"} imalola model kusankha, {"type": "any"} imaipangitsa kuyitana tool, {"type": "tool", "name": "…"} imaipangitsa kuyitana tool imeneyo. Ma hosted open-weight model
thinking object Kuchuluka kwa momwe model imaganizira isanayankhe. Onani Thinking pansipa. Ma hosted open-weight model
web_search boolean false Chowonjezera cha Shannon pa mtunduwu: true imalola model kufufuza pa intaneti isanayankhe. shannon-1.6-*, shannon-2-*, banja la Shannon 3

Popanda max_tokens mtengo ndi 4,096, ndipo 65,536 pa shannon-coder-1.

metadata, top_k, service_tier ndi zizindikiro za cache_control pa ma block zimalandiridwa kuti code ya client yomwe ilipo igwire ntchito popanda kusintha. Siziisintha yankho; prompt caching pa ma hosted open-weight model imachitika yokha.

Yankho la JSON motsatira schema limapemphedwa ndi response_format pa Chat Completions komanso ndi text.format pa Responses. Zotuluka zokonzedwa

Pempho lokhala ndi system prompt ndi thinking

Pempho ili limatumiza system prompt, thinking budget, temperature ndi stop sequence. Limagwiritsa ntchito hosted open-weight model, yomwe imagwiritsa ntchito zonsezo.

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)

Pa ma hosted open-weight model, usage imanena ma token owerengedwa kuchokera ku prompt cache mosiyana ndi ma input token ena.

200 JSON
{
  "usage": {
    "input_tokens": 31,
    "cache_read_input_tokens": 0,
    "cache_creation_input_tokens": 0,
    "output_tokens": 62
  }
}

System prompt

system ndi string, kapena array ya ma text block mu mtundu wa [{"type": "text", "text": "…"}]. Ma text a ma block amalumikizidwa ndi kusiya mzere.

Imagwira ntchito pa hosted open-weight model, shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite, shannon-2-pro ndi shannon-coder-1. Ndi id ya banja la Shannon 3, ikani malangizo omwe ayenera kutsatiridwa mu message ya user.

Messages ndi content blocks

Uthenga uli ndi role, user kapena assistant, ndi content. content ndi string, kapena array ya ma block.

Block Kufotokozera Ilipo pa
{"type": "text", "text": "…"} Text. Ma model onse
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "…"}}
{"type": "image", "source": {"type": "url", "url": "…"}}
Chithunzi mu uthenga wa user, ngati base64 kapena ndi URL. Banja la Shannon 3, shannon-1.6-lite, shannon-1.6-pro, ndi ma hosted open-weight model omwe amalemba kuti amalandira zithunzi
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": "…"}} Chikalata (PDF, Word, PowerPoint kapena Excel) mu uthenga wa user, ngati base64 kapena ndi URL. Banja la Shannon 3
{"type": "tool_use", "id": "toolu_…", "name": "…", "input": {…}} Mu uthenga wa assistant: call ya tool yomwe model idapanga mu yankho lakale. Ma model onse
{"type": "tool_result", "tool_use_id": "toolu_…", "content": "…"} Mu uthenga wa user: zotsatira za call imeneyo. content ndi string, kapena mndandanda wa ma text block. Ma model onse

Mutha kutumizanso content ya yankho lakale monga momwe ilili: ma block a text ndi tool_use amawerengedwa, ma block a thinking amadumphidwa.

Pa ma tier a Shannon, zithunzi ndi zikalata zimawerengedwa kuchokera ku uthenga wotsiriza wa user. Kukula ndi malire zili ndi tsamba lawo. Zithunzi ndi ma file

Thinking

Hosted open-weight model imasintha thinking kukhala reasoning effort. Popanda field iyi effort imakhala high.

Mawonekedwe budget_tokens Zotsatira
{"type": "disabled"} Palibe reasoning. Yankho lilibe block ya thinking.
{"type": "enabled", "budget_tokens": 2048} ≤ 2,048 Effort yochepa.
{"type": "enabled", "budget_tokens": 8192} ≤ 8,192 Effort yapakati.
{"type": "enabled", "budget_tokens": 16000} > 8,192 Effort yapamwamba.

Model yomwe imaganiza imabweza reasoning yake ngati block {"type": "thinking", "thinking": "…"} patsogolo pa text. Ma tier a Shannon omwe amaganiza amaitumizanso.

Ma effort amafotokozedwa patsamba lawo. Reasoning effort

Tools

Tool ndi function yomwe code yanu imayendetsa. Fotokozani iliyonse ndi name, description ndi input_schema. Model ikafuna tool, yankho limakhala ndi block ya tool_use ndipo stop_reason imakhala tool_use.

Tanthauzo la tool mu tools:

tools
[
  {
    "name": "get_weather",
    "description": "Current weather for a city.",
    "input_schema": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string"
        }
      },
      "required": [
        "city"
      ]
    }
  }
]

content ya yankho lomwe limayitana tool:

200 content
[
  {
    "type": "text",
    "text": ""
  },
  {
    "type": "tool_use",
    "id": "toolu_3d9a7c1e5b2f4a60c8e1d7f09b24a6c5",
    "name": "get_weather",
    "input": {
      "city": "Paris"
    }
  }
]

Yendetsani tool, kenako tumizaninso pempho ndi call ndi block ya tool_result yowonjezedwa ku messages:

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

Mawonekedwe Zotsatira Imagwira ntchito pa
{"type": "auto"} Model imasankha ngati iyitane tool. Ichi ndi default. Ma model onse
{"type": "any"} Model iyenera kuyitana imodzi mwa ma tool. Ma hosted open-weight model
{"type": "tool", "name": "…"} Model iyenera kuyitana tool yotchulidwa. Ma hosted open-weight model

Ma tool okhala ndi input_schema okha ndi omwe amawerengedwa. Web search si tool pa endpoint iyi: khazikitsani field ya web_search.

Tool loop yafotokozedwa mokwanira patsamba lake. Kuyitana mafunkseni

Yankho

Field Mtundu Kufotokozera Yotumizidwa ndi
id string msg_ yotsatiridwa ndi zilembo 32 za hexadecimal. Ma model onse
type string Nthawi zonse message. Ma model onse
role string Nthawi zonse assistant. Ma model onse
model string Id yovomerezeka ya model yomwe yayankha. Ma model onse
content array Ma block a yankho, motsatira izi: thinking pamene model yaganiza, text, kenako tool_use imodzi pa call iliyonse ya tool. Ma model onse
stop_reason string Chifukwa chomwe yankho linatha. Onani zomwe zili pansipa. Ma model onse
usage object input_tokens ndi output_tokens. Ma model onse
sources array Pa pempho lokha lokhala ndi web_search: true lomwe kusaka kwake kwapeza kanthu: zotsatira zomwe model idapatsidwa, chilichonse ndi index, title ndi url. [1] mu yankho ndi cholowa chokhala ndi index 1. Mu stream imabwera ndi event ya message_delta. shannon-1.6-*, shannon-2-*, banja la Shannon 3
usage.cache_read_input_tokens integer cache_read_input_tokens ndi gawo la prompt lowerengedwa kuchokera ku cache; input_tokens ndi zotsalazo. cache_creation_input_tokens nthawi zonse ndi 0. Ma hosted open-weight model

Ma block a yankho

Block Kufotokozera
{"type": "thinking", "thinking": "…"} Reasoning ya model. Palibe member ya signature.
{"type": "text", "text": "…"} Text ya yankho. Ikhoza kukhala yopanda kanthu pamene yankho ndi call ya tool.
{"type": "tool_use", "id": "toolu_…", "name": "…", "input": {…}} Call ya tool. input ndi JSON object. Tumizani zotsatira ndi id yomweyo.

stop_reason

stop_reason Kufotokozera Yotumizidwa ndi
end_turn Model yamaliza yankho lake, kapena imodzi mwa stop_sequences zanu yaoneka. Ma model onse
tool_use Yankho lili ndi call imodzi kapena zingapo za tool. Ma model onse
max_tokens Yankho lidadulidwa pa malire a output. Banja la Shannon 3, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1, mu ma stream

Streaming

Khazikitsani stream kukhala true kuti mulandire server-sent events. Frame iliyonse ili ndi mzere wa event: wokhala ndi dzina la event ndi mzere wa data: wokhala ndi JSON object yobwereza dzinalo mu type. Ma stream helper a Anthropic SDK amawerenga ma event awa m'malo mwanu:

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)

Ma event, motsatira momwe amafikira:

Event Data Liti
message_start message Nthawi zonse imakhala yoyamba. message ili ndi id, role, model, content yopanda kanthu ndi usage yoyambira pa zero.
content_block_start index, content_block Block imatsegulidwa. content_block ndi block yopanda kanthu ya text kapena thinking, kapena block ya tool_use yokhala ndi id ndi name yake.
content_block_delta index, delta {"type": "thinking_delta", "thinking"} Chigawo cha reasoning, pa block ya thinking.
content_block_delta index, delta {"type": "text_delta", "text"} Chigawo cha yankho, pa block ya text.
content_block_delta index, delta {"type": "input_json_delta", "partial_json"} Ma argument a call ya tool, athunthu mu delta imodzi, pa block ya tool_use.
content_block_stop index Block yokhala ndi index iyi yatha.
message_delta delta {stop_reason, stop_sequence}, usage Mapeto a yankho: delta.stop_reason, ndi usage ya pempho.
message_stop Nthawi zonse imakhala yomaliza.

Ma block amawerengedwa ndi index motsatira momwe amatsegulira, ndipo block imodzi imatha isanatsegulidwe yotsatira. Werengani block iliyonse potengera mtundu wake, osati nambala yake: stream ikhoza kutsegula block yopanda kanthu ya text poyamba, kenako block ya thinking, ndi kupereka yankho mu block ina ya text pambuyo pake.

200 text/event-stream
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"}

Keep-alive

Yotumizidwa ndi Kufotokozera
Banja la Shannon 3, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1 Event ya ping yokhala ndi data {"type": "ping"}, masekondi 15 aliwonse.
Ma hosted open-weight model Mzere wa comment : keepalive, masekondi 15 aliwonse pamene model ikugwira ntchito.

Stream yomwe yalephera

Cholakwika chomwe chachitika stream itayamba ndi event ya error yokhala ndi body yofanana ndi yankho la cholakwika: {"type": "error", "error": {"type", "message"}}. Ma event omaliza, message_delta ndi message_stop, amatsatirabe. Ganizirani kuti yankho lalephera ndipo tumizaninso pempho.

Zolakwika

Zolakwika zimagwiritsa ntchito body ya cholakwika ya Anthropic: type ndi error, ndipo object ya error ili ndi type ndi message. Tebulo limalemba zomwe endpoint iyi imabweza kawirikawiri. Mndandanda wathunthu, ndi zoyenera kuyesanso, uli ndi tsamba lake. Zolakwika

400 JSON
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "unknown model: no-such-model"
  }
}
Status Mtundu Uthenga Liti
401 authentication_error Missing authentication
Invalid API key
API key siinatumizidwe, kapena kiyi siidziwika kapena yathetsedwa.
400 invalid_request_error unknown model: <id> model si model id ya Shannon.
400 invalid_request_error No user message provided Ma tier a Shannon: pempho lilibe text ya user komanso lilibe tools.
400 invalid_request_error <id> does not accept image input Chithunzi chatumizidwa ku hosted open-weight model yopanda image input.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … messages ikusowa, kapena field ili ndi mtundu wolakwika wa JSON.
429 rate_limit_error Quota exceeded. Upgrade your plan at shannon-ai.com/plan max_tokens ndi yaikulu kuposa zomwe zatsala mu balance yanu.
429 rate_limit_error Too many requests. Retry in <n>s. Flood protection: mapempho oposa 120 mu mphindi imodzi pa akaunti yanu.
429 rate_limit_error Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan shannon-coder-1: ma call a Shannon Coder a plan yanu pa nthawi ya maola 4 yamakono agwiritsidwa ntchito.
500 server_error The model backend failed to answer. Please retry. Model sinapereke yankho. Tumizaninso pempho.
502 api_error The model backend failed to answer. Please retry. Model sinapereke yankho. Tumizaninso pempho.

Kuwerenga ma token

POST /v1/messages/count_tokens imalandira body yofanana ndi ya pempho la uthenga (model, system, messages, tools) ndipo imabweza {"input_tokens": n}: chiwerengero cha ma input token omwe pempholo lingawerengedwe. Imawerengera ma hosted open-weight model, ndi yaulere ndipo siiwerengedwa ndi flood protection.

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)

Kuwerenga ma token kuli ndi tsamba lake, ndi endpoint ya plain-text ndi malire. Kuwerenga ma token