कंटेंट पर जाएँ
Anthropic फ़ॉर्मेट

Anthropic फ़ॉर्मेट

POST /v1/messages Anthropic Messages फ़ॉर्मेट में अनुरोध लेता है और उसी फ़ॉर्मेट में उत्तर देता है, स्ट्रीम के साथ या बिना। इसे Anthropic SDK और उस पर बने टूल्स, जैसे Claude Code, के साथ उपयोग करें: बेस URL सेट करें, अपनी Shannon API key का उपयोग करें और कोई Shannon मॉडल id दें।

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

Anthropic SDK को Shannon के बेस URL पर पॉइंट करें। SDK /v1/messages जोड़ता है और आपकी key 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)

उत्तर एक संदेश है जिसका content ब्लॉक्स की सूची है। ब्लॉक को उनके type से पहचानें: जब मॉडल ने रीज़न किया हो तो पहला ब्लॉक thinking होता है और टेक्स्ट उसके बाद आता है।

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
  }
}

हेडर

हेडर मान विवरण
x-api-key YOUR_API_KEY आपकी API key। इसकी जगह Authorization: Bearer YOUR_API_KEY भी स्वीकार है।
content-type application/json आवश्यक।
anthropic-version 2023-06-01 स्वीकार है, आवश्यक नहीं। Anthropic SDK 2023-06-01 भेजता है; मान चाहे जो हो, उत्तर का फ़ॉर्मेट वही रहता है।
anthropic-beta स्वीकार है, आवश्यक नहीं।
x-request-id वैकल्पिक। अनुरोध के लिए आपका अपना id। यह उत्तर में बिना बदले लौट आता है; इसके बिना उत्तर में 12 हेक्साडेसिमल अक्षरों का id होता है।

अनुरोध फ़ील्ड

केवल messages आवश्यक है। लागू करने वाले कॉलम में उन मॉडल के नाम हैं जिन पर कोई फ़ील्ड उत्तर को बदलता है। होस्टेड ओपन-वेट मॉडल मॉडल सूची के बारह id हैं; Shannon 3 परिवार shannon-3, shannon-3-pro, shannon-3.1 और shannon-3.1-pro है। मॉडल और कीमतें

फ़ील्ड प्रकार डिफ़ॉल्ट विवरण लागू करने वाले
model string shannon-1.6-lite मॉडल सूची में से कोई Shannon मॉडल id। इसे हर अनुरोध के साथ भेजें। मिलान में अक्षरों का केस मायने नहीं रखता। कोई भी दूसरा नाम 400 unknown model लौटाता है, इसलिए जो टूल अपने मॉडल नाम भेजता है उसे Shannon id पर सेट करना होगा। सभी मॉडल
messages array आवश्यक। बातचीत, सबसे पुराना संदेश पहले, user और assistant भूमिकाओं के साथ। नीचे संदेश और कंटेंट ब्लॉक देखें। सभी मॉडल
system string | array मॉडल के लिए निर्देश: एक स्ट्रिंग या टेक्स्ट ब्लॉक्स की ऐरे। नीचे सिस्टम प्रॉम्प्ट देखें। होस्टेड ओपन-वेट मॉडल, shannon-1.6-*, shannon-2-*, shannon-coder-1
max_tokens integer 4096 उत्तर की ऊपरी सीमा, टोकन में। इस endpoint पर वैकल्पिक; Anthropic SDK इसे हमेशा भेजता है। 1 से 65,536 के बाहर का मान उसी सीमा में ला दिया जाता है। अनुरोध चलने के दौरान आपके बैलेंस में से इतनी मात्रा अलग रख ली जाती है। होस्टेड ओपन-वेट मॉडल, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1
stream boolean false true उत्तर को server-sent events के रूप में भेजता है। नीचे स्ट्रीमिंग देखें। सभी मॉडल
temperature number सैंपलिंग तापमान। होस्टेड ओपन-वेट मॉडल पर मान 0 और 2 के बीच रखे जाते हैं। होस्टेड ओपन-वेट मॉडल, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1
top_p number न्यूक्लियस सैंपलिंग। होस्टेड ओपन-वेट मॉडल
stop_sequences array अधिकतम 4 स्ट्रिंग। उत्तर उनमें से पहली के आने से पहले समाप्त हो जाता है। stop_reason end_turn ही रहता है। होस्टेड ओपन-वेट मॉडल
tools array वे टूल जिन्हें मॉडल कॉल कर सकता है, हर एक {"name", "description", "input_schema"} के रूप में। नीचे टूल्स देखें। सभी मॉडल
tool_choice object {"type": "auto"} मॉडल को तय करने देता है, {"type": "any"} उसे कोई टूल कॉल करने पर मजबूर करता है, {"type": "tool", "name": "…"} उसे वही टूल कॉल करने पर मजबूर करता है। होस्टेड ओपन-वेट मॉडल
thinking object उत्तर देने से पहले मॉडल कितना रीज़न करता है। नीचे थिंकिंग देखें। होस्टेड ओपन-वेट मॉडल
web_search boolean false फ़ॉर्मेट में Shannon का जोड़: true मॉडल को उत्तर देने से पहले वेब पर खोजने देता है। shannon-1.6-*, shannon-2-*, Shannon 3 परिवार

max_tokens के बिना मान 4,096 होता है, और shannon-coder-1 पर 65,536।

ब्लॉक पर metadata, top_k, service_tier और cache_control मार्कर स्वीकार किए जाते हैं ताकि मौजूदा क्लाइंट कोड बिना बदलाव के चले। ये उत्तर को नहीं बदलते; होस्टेड ओपन-वेट मॉडल पर प्रॉम्प्ट कैशिंग अपने आप होती है।

schema के अनुसार JSON उत्तर Chat Completions में response_format से और Responses में text.format से माँगा जाता है। संरचित आउटपुट

सिस्टम प्रॉम्प्ट और थिंकिंग के साथ अनुरोध

यह अनुरोध एक सिस्टम प्रॉम्प्ट, एक थिंकिंग बजट, एक temperature और एक स्टॉप सीक्वेंस भेजता है। यह एक होस्टेड ओपन-वेट मॉडल का उपयोग करता है, जो इन सभी को लागू करता है।

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)

होस्टेड ओपन-वेट मॉडल पर usage प्रॉम्प्ट कैश से पढ़े गए टोकन को बाकी इनपुट टोकन से अलग बताता है।

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

सिस्टम प्रॉम्प्ट

system एक स्ट्रिंग है, या [{"type": "text", "text": "…"}] रूप में टेक्स्ट ब्लॉक्स की ऐरे। ब्लॉक के टेक्स्ट एक लाइन ब्रेक से जोड़े जाते हैं।

इसे होस्टेड ओपन-वेट मॉडल, shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite, shannon-2-pro और shannon-coder-1 लागू करते हैं। Shannon 3 परिवार के id के साथ, जो निर्देश अनिवार्य हैं उन्हें user संदेश में रखें।

संदेश और कंटेंट ब्लॉक

एक संदेश में role, यानी user या assistant, और एक content होता है। content एक स्ट्रिंग या ब्लॉक्स की ऐरे होता है।

ब्लॉक विवरण इन पर उपलब्ध
{"type": "text", "text": "…"} टेक्स्ट। सभी मॉडल
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "…"}}
{"type": "image", "source": {"type": "url", "url": "…"}}
user संदेश में एक इमेज, base64 के रूप में या URL से। Shannon 3 परिवार, shannon-1.6-lite, shannon-1.6-pro और वे होस्टेड ओपन-वेट मॉडल जो इमेज इनपुट सूचीबद्ध करते हैं
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": "…"}} user संदेश में एक दस्तावेज़ (PDF, Word, PowerPoint या Excel), base64 के रूप में या URL से। Shannon 3 परिवार
{"type": "tool_use", "id": "toolu_…", "name": "…", "input": {…}} assistant संदेश में: मॉडल द्वारा पिछले उत्तर में की गई टूल कॉल। सभी मॉडल
{"type": "tool_result", "tool_use_id": "toolu_…", "content": "…"} user संदेश में: उस कॉल का परिणाम। content एक स्ट्रिंग या टेक्स्ट ब्लॉक्स की सूची होता है। सभी मॉडल

आप पिछले उत्तर का content जैसा है वैसा वापस भेज सकते हैं: text और tool_use ब्लॉक पढ़े जाते हैं, thinking ब्लॉक छोड़ दिए जाते हैं।

Shannon टियर पर इमेज और दस्तावेज़ आखिरी user संदेश से पढ़े जाते हैं। साइज़ और सीमाओं का अपना पेज है। इमेज और फ़ाइलें

थिंकिंग

होस्टेड ओपन-वेट मॉडल thinking को रीज़निंग effort से मैप करते हैं। फ़ील्ड के बिना effort high होता है।

रूप budget_tokens प्रभाव
{"type": "disabled"} कोई रीज़निंग नहीं। उत्तर में thinking ब्लॉक नहीं होता।
{"type": "enabled", "budget_tokens": 2048} ≤ 2,048 कम effort।
{"type": "enabled", "budget_tokens": 8192} ≤ 8,192 मध्यम effort।
{"type": "enabled", "budget_tokens": 16000} > 8,192 उच्च effort।

रीज़न करने वाला मॉडल अपनी रीज़निंग टेक्स्ट के आगे {"type": "thinking", "thinking": "…"} ब्लॉक के रूप में लौटाता है। रीज़न करने वाले Shannon टियर भी इसे भेजते हैं।

effort का वर्णन उनके अपने पेज पर है। रीज़निंग effort

टूल्स

टूल एक फ़ंक्शन है जिसे आपका कोड चलाता है। हर टूल को name, description और input_schema से बताएँ। जब मॉडल को टूल चाहिए, तो उत्तर में tool_use ब्लॉक होता है और stop_reason का मान tool_use होता है।

tools में एक टूल परिभाषा:

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

उसे कॉल करने वाले उत्तर का content:

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

टूल चलाएँ, फिर कॉल और tool_result ब्लॉक को 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

रूप प्रभाव लागू करने वाले
{"type": "auto"} मॉडल तय करता है कि टूल कॉल करना है या नहीं। यह डिफ़ॉल्ट है। सभी मॉडल
{"type": "any"} मॉडल को टूल्स में से किसी एक को कॉल करना होगा। होस्टेड ओपन-वेट मॉडल
{"type": "tool", "name": "…"} मॉडल को नामित टूल को कॉल करना होगा। होस्टेड ओपन-वेट मॉडल

केवल input_schema वाले टूल पढ़े जाते हैं। इस endpoint पर वेब सर्च टूल नहीं है: web_search फ़ील्ड सेट करें।

टूल लूप का पूरा वर्णन उसके अपने पेज पर है। फ़ंक्शन कॉलिंग

उत्तर

फ़ील्ड प्रकार विवरण भेजने वाला
id string msg_ के बाद 32 हेक्साडेसिमल अक्षर। सभी मॉडल
type string हमेशा message। सभी मॉडल
role string हमेशा assistant। सभी मॉडल
model string उत्तर देने वाले मॉडल का कैननिकल id। सभी मॉडल
content array उत्तर के ब्लॉक, इसी क्रम में: मॉडल ने रीज़न किया हो तो thinking, फिर text, फिर हर टूल कॉल के लिए एक tool_use। सभी मॉडल
stop_reason string उत्तर क्यों समाप्त हुआ। नीचे दिए मान देखें। सभी मॉडल
usage object input_tokens और output_tokens। सभी मॉडल
sources array केवल उस अनुरोध पर जिसमें web_search: true हो और जिसकी खोज को कुछ मिला हो: मॉडल को दिए गए परिणाम, हर एक में index, title और url। उत्तर में [1] वह एंट्री है जिसका index 1 है। स्ट्रीम में यह message_delta इवेंट के साथ आता है। shannon-1.6-*, shannon-2-*, Shannon 3 परिवार
usage.cache_read_input_tokens integer cache_read_input_tokens प्रॉम्प्ट का वह हिस्सा है जो कैश से पढ़ा गया; input_tokens बाकी है। cache_creation_input_tokens हमेशा 0 होता है। होस्टेड ओपन-वेट मॉडल

उत्तर के ब्लॉक

ब्लॉक विवरण
{"type": "thinking", "thinking": "…"} मॉडल की रीज़निंग। इसमें signature सदस्य नहीं होता।
{"type": "text", "text": "…"} उत्तर का टेक्स्ट। जब उत्तर एक टूल कॉल हो तो यह खाली हो सकता है।
{"type": "tool_use", "id": "toolu_…", "name": "…", "input": {…}} एक टूल कॉल। input एक JSON ऑब्जेक्ट है। परिणाम उसी id के साथ वापस भेजें।

stop_reason

stop_reason विवरण भेजने वाला
end_turn मॉडल ने अपना उत्तर पूरा किया, या आपका कोई stop_sequences आ गया। सभी मॉडल
tool_use उत्तर में एक या अधिक टूल कॉल हैं। सभी मॉडल
max_tokens उत्तर आउटपुट सीमा पर काट दिया गया। Shannon 3 परिवार, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1, स्ट्रीम में

स्ट्रीमिंग

server-sent events के लिए stream को true पर सेट करें। हर फ़्रेम में इवेंट के नाम वाली एक event: लाइन और एक JSON ऑब्जेक्ट वाली data: लाइन होती है, जो type में वही नाम दोहराती है। Anthropic SDK के स्ट्रीम हेल्पर ये इवेंट आपके लिए पढ़ लेते हैं:

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)

इवेंट, उसी क्रम में जिसमें वे आते हैं:

इवेंट डेटा कब
message_start message हमेशा पहला। message में id, भूमिका, मॉडल, एक खाली content और शून्य से शुरू होने वाला usage होता है।
content_block_start index, content_block एक ब्लॉक खुलता है। content_block एक खाली text या thinking ब्लॉक है, या अपने id और name के साथ एक tool_use ब्लॉक।
content_block_delta index, delta {"type": "thinking_delta", "thinking"} रीज़निंग का एक टुकड़ा, thinking ब्लॉक के लिए।
content_block_delta index, delta {"type": "text_delta", "text"} उत्तर का एक टुकड़ा, text ब्लॉक के लिए।
content_block_delta index, delta {"type": "input_json_delta", "partial_json"} tool_use ब्लॉक के लिए टूल कॉल के आर्गुमेंट, एक ही डेल्टा में पूरे।
content_block_stop index इस index वाला ब्लॉक पूरा हो गया।
message_delta delta {stop_reason, stop_sequence}, usage उत्तर का अंत: delta.stop_reason, और अनुरोध का usage।
message_stop हमेशा आखिरी।

ब्लॉक index से, उनके खुलने के क्रम में क्रमांकित होते हैं, और एक ब्लॉक अगले के खुलने से पहले पूरा हो जाता है। हर ब्लॉक को उसके प्रकार से पढ़ें, क्रमांक से नहीं: स्ट्रीम पहले एक खाली text ब्लॉक खोल सकती है, फिर thinking ब्लॉक, और उत्तर बाद के किसी text ब्लॉक में दे सकती है।

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

भेजने वाला विवरण
Shannon 3 परिवार, shannon-1.6-lite, shannon-1.6-pro, shannon-coder-1 डेटा {"type": "ping"} वाला एक ping इवेंट, हर 15 सेकंड में।
होस्टेड ओपन-वेट मॉडल एक कमेंट लाइन : keepalive, मॉडल के काम करते समय हर 15 सेकंड में।

विफल होने वाली स्ट्रीम

स्ट्रीम शुरू होने के बाद की विफलता एक error इवेंट होती है, जिसकी बॉडी त्रुटि उत्तर जैसी ही होती है: {"type": "error", "error": {"type", "message"}}। समापन इवेंट, message_delta और message_stop, फिर भी आते हैं। उत्तर को विफल मानें और अनुरोध फिर से भेजें।

त्रुटियाँ

त्रुटियाँ Anthropic की त्रुटि बॉडी का उपयोग करती हैं: type का मान error होता है, और error ऑब्जेक्ट में एक type और एक message होता है। तालिका वह दिखाती है जो यह endpoint सबसे ज़्यादा लौटाता है। पूरी सूची, और किसे दोबारा आज़माना है, इसका अपना पेज है। त्रुटि प्रबंधन

400 JSON
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "unknown model: no-such-model"
  }
}
स्टेटस प्रकार संदेश कब
401 authentication_error Missing authentication
Invalid API key
कोई API key नहीं भेजी गई, या key अज्ञात है या रद्द की जा चुकी है।
400 invalid_request_error unknown model: <id> model कोई Shannon मॉडल id नहीं है।
400 invalid_request_error No user message provided Shannon टियर: अनुरोध में न यूज़र टेक्स्ट है और न tools।
400 invalid_request_error <id> does not accept image input इमेज इनपुट के बिना होस्टेड ओपन-वेट मॉडल को इमेज भेजी गई।
422 invalid_request_error Failed to deserialize the JSON body into the target type: … messages गायब है, या किसी फ़ील्ड का JSON प्रकार गलत है।
429 rate_limit_error Quota exceeded. Upgrade your plan at shannon-ai.com/plan max_tokens आपके बैलेंस में बचे हिस्से से बड़ा है।
429 rate_limit_error Too many requests. Retry in <n>s. Flood protection: आपके अकाउंट पर एक मिनट में 120 से अधिक अनुरोध।
429 rate_limit_error Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan shannon-coder-1: मौजूदा 4 घंटे की विंडो के लिए आपके प्लान की Shannon Coder कॉल समाप्त हो चुकी हैं।
500 server_error The model backend failed to answer. Please retry. मॉडल ने उत्तर नहीं दिया। अनुरोध फिर से भेजें।
502 api_error The model backend failed to answer. Please retry. मॉडल ने उत्तर नहीं दिया। अनुरोध फिर से भेजें।

टोकन गिनना

POST /v1/messages/count_tokens संदेश अनुरोध जैसी ही बॉडी (model, system, messages, tools) लेता है और {"input_tokens": n} लौटाता है: अनुरोध पर जितने इनपुट टोकन का शुल्क लगेगा उनकी संख्या। यह होस्टेड ओपन-वेट मॉडल के लिए गिनता है, मुफ़्त है और 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)

गिनती का अपना पेज है, जिसमें प्लेन-टेक्स्ट endpoint और सीमाएँ हैं। टोकन गिनती