الانتقال إلى المحتوى الرئيسي
POST
استخدم POST /v1/messages لإرسال طلبات Claude بتنسيق Anthropic Messages. تُعِدّ الأمثلة Anthropic SDK الرسمي باستخدام عنوان URL الأساسي لـ CometAPI، ثم تقرأ مفتاح API الخاص بك من $COMETAPI_KEY.
للتعرّف على تعريفات الحقول والخيارات الخاصة بالنماذج، راجع المرجع الرسمي لـ مرجع Messages API. لطلبات المتوافقة مع OpenAI، راجع Chat Completions.
صادِق باستخدام x-api-key أو Authorization: Bearer. يستخدم Anthropic SDK x-api-key. تتضمن أمثلة HTTP ‏anthropic-version: 2023-06-01.

البدء السريع

تطلب الأمثلة التالية ثلاث استعلامات بحث. عيّن $COMETAPI_KEY قبل تشغيلها. ثبّت anthropic لـ Python أو @anthropic-ai/sdk لـ JavaScript:
تحتوي الاستجابة على مصفوفة content. اقرأ الكتل التي تكون قيمة type فيها هي text؛ قد تظهر كتل التفكير والأدوات قبل النص.

التحكم في التفكير التكيفي

اضبط thinking.type على adaptive واختر قيمة لـ output_config.effort. يستخدم المثال التالي xhigh ويقرأ الرسالة المكتملة من تدفق:
يسهم التفكير في حد الإخراج max_tokens. اترك مساحة للإجابة النهائية وللتفكير أيضًا. يمكن أن تحتوي الاستجابة على نص من دون كتلة تفكير مرئية. احتفظ بأي كتل تفكير مُعادة كما هي في سجل المحادثة. للاطلاع على إعداد التفكير الخاص بالطراز، راجع التفكير. تحذف الأمثلة temperature وtop_p وtop_k؛ راجع وثائق معلمات الطراز المحدد قبل إضافة عناصر التحكم في أخذ العينات.

التخزين المؤقت لـ Prompts

ضع نقطة توقف للتخزين المؤقت على المواد المرجعية التي تعيد استخدامها بين الطلبات. احفظ موادك المرجعية في ملف reference.txt بترميز UTF-8 قبل تشغيل هذا المثال. استخدم بادئة تفي بالحد الأدنى للطراز المحدد من الطول الأدنى القابل للتخزين المؤقت:
شغّل الطلب مرة أخرى باستخدام الطراز نفسه والنص المرجعي وإعدادات التفكير نفسها. افحص عدادات الاستخدام لتحديد ما إذا كان الطلب قد أعاد استخدام بادئة:
  • cache_creation_input_tokens يحسب Tokens التي تُكتب في ذاكرة التخزين المؤقت.
  • cache_read_input_tokens يحسب Tokens التي تُقرأ من ذاكرة التخزين المؤقت.
  • input_tokens يحسب Tokens الإدخال المُعالجة خارج عدادات ذاكرة التخزين المؤقت تلك.
في الطلبات المتكررة، يبلّغ cache_read_input_tokens عن عدد Tokens الإدخال التي تمت قراءتها من ذاكرة التخزين المؤقت. يحتوي مثال OpenAPI Prompt Cache على مرجع خيالي كامل يمكنك حفظه باسم reference.txt لتجربة العينة.

استجابة التدفق

اضبط stream: true لاستخدام Server-Sent Events. تعرض SDK أجزاء النص عند وصولها:
يتضمن تدفق الرسائل message_start، وأحداث كتلة المحتوى، message_delta، وmessage_stop. يمكن أن تحتوي كتل المحتوى على نص أو تفكير أو نشاط أدوات. بالنسبة إلى كتلة نصية، يحتوي content_block_delta على text_delta. اقرأ معلومات الاستخدام النهائية وسبب التوقف من message_delta.

التحكم في الجهد

اضبط output_config.effort لتوجيه مقدار التفكير. يستخدم هذا المثال low للحصول على شرح موجز وينتظر اكتمال الرسالة المتدفقة:
استخدم المرجع الرسمي لـ مستوى الجهد لاختيار مستوى جهد. اضبط max_tokens بشكل منفصل للحد من طول الإخراج.

استخدام أدوات الخادم

تُنفَّذ أدوات الخادم أثناء طلب API وتُرجع كتل النتائج إلى جانب إجابة Claude.
اجلب ورقة بحثية واطلب من Claude استخدام المستند المسترجَع في إجابته. يبث هذا المثال الاستجابة ويفحص كلاً من النص النهائي وكتلة web_fetch_tool_result الكتلة:
تقترن الاستجابة بكتلة server_tool_use مع web_fetch_tool_result تحتوي على المستند الذي تم جلبه أو خطأ في الأداة.

إرجاع نتائج أداة العميل

بالنسبة إلى أداة عميل، يعيد Claude كتلة tool_use. شغّل دالة تطبيقك وأرسل نتيجتها في كتلة tool_result ذات tool_use_id المطابق. احتفِظ بمحتوى المساعد الكامل بين الطلبين. يوفّر هذا المثال نتيجة طلب خيالية ويطلب من Claude استخدام تلك النتيجة:

مثال على الاستجابة

يعيد الطلب غير المتدفق كائن رسالة. يوضّح المثال التالي حقول النص والاستخدام الخاصة به مع معرّف رسالة توضيحي:
يصف سبب الإيقاف الإجراء التالي. يُكمل end_turn الإجابة، max_tokens ويعني بلوغ الإخراج الحد، بينما يطلب tool_use نتيجة أداة عميل . بالنسبة إلى دور أداة خادم يعيد pause_turn، تابع باستخدام محتوى المساعد المُعاد دون تغيير.

التفويضات

x-api-key
string
header
مطلوب

Your CometAPI key passed via the x-api-key header. Authorization: Bearer $COMETAPI_KEY is also supported.

الترويسات

anthropic-version
string
افتراضي:2023-06-01

The Anthropic API version to use. Defaults to 2023-06-01.

مثال:

"2023-06-01"

anthropic-beta
string

Comma-separated feature identifiers required by a specific beta API feature. Omit this header for the examples on this page.

الجسم

application/json
model
string
افتراضي:claude-opus-5
مطلوب

The Claude model to use. See the Models page for available Claude model IDs.

مثال:

"claude-opus-5"

messages
object[]
مطلوب

Conversation history. Use user and assistant messages with text strings or content-block arrays. Return complete assistant content blocks when continuing a tool call.

max_tokens
integer
مطلوب

The maximum number of tokens to generate. The model may stop before reaching this limit. When using thinking, the thinking tokens count towards this limit.

النطاق المطلوب: x >= 1
مثال:

1024

system

System prompt providing context and instructions to Claude. Can be a plain string or an array of content blocks (useful for prompt caching).

temperature
number

Sampling temperature. The examples omit sampling overrides.

النطاق المطلوب: 0 <= x <= 1
top_p
number

Nucleus sampling threshold. The examples omit sampling overrides.

النطاق المطلوب: 0 <= x <= 1
top_k
integer

Limits sampling to the k most likely tokens. The examples omit sampling overrides.

النطاق المطلوب: x >= 0
stream
boolean
افتراضي:false

If true, stream the response incrementally using Server-Sent Events (SSE). Events include message_start, content_block_start, content_block_delta, content_block_stop, message_delta, and message_stop.

stop_sequences
string[]

Custom strings that cause the model to stop generating when encountered. The stop sequence is not included in the response.

thinking
object

Thinking configuration. The adaptive example uses type adaptive and sets output_config.effort separately.

tools
object[]

Client tools define a name and input_schema. Server tools use a versioned type and name, such as the web_fetch and web_search examples on this page.

tool_choice
object

Controls how the model uses tools.

metadata
object

Request metadata for tracking and analytics.

output_config
object

Configuration for reasoning effort and structured output.

service_tier
enum<string>

The service tier to use. auto tries priority capacity first, standard_only uses only standard capacity.

الخيارات المتاحة:
auto,
standard_only

الاستجابة

Successful response. When stream is true, the response is a stream of SSE events.

id
string

Message identifier returned by the API.

type
enum<string>

Always message.

الخيارات المتاحة:
message
role
enum<string>

Always assistant.

الخيارات المتاحة:
assistant
content
object[]

The response content blocks. May include text, thinking, tool_use, and other block types.

model
string

Model ID reported by the response.

stop_reason
enum<string>

Why the model stopped generating. refusal can be returned as a successful HTTP response when the model declines a request.

الخيارات المتاحة:
end_turn,
max_tokens,
stop_sequence,
tool_use,
pause_turn,
refusal
stop_sequence
string | null

The stop sequence that caused the model to stop, if applicable.

usage
object

Token usage statistics.

آخر تعديل في ٨ سبتمبر ٢٠٢٦