Create a Message
Call Claude through the CometAPI Messages endpoint with text and image input, adaptive thinking, prompt caching, streaming, and tools.
POST /v1/messages to send Claude requests in the Anthropic Messages format.
The examples configure the official Anthropic SDK with the CometAPI base URL and
read your API key from $COMETAPI_KEY.
x-api-key or Authorization: Bearer. The Anthropic SDK uses
x-api-key. The HTTP examples include anthropic-version: 2023-06-01.Quick start
The following examples request three search queries. Set$COMETAPI_KEY before
running them. Install anthropic for Python or @anthropic-ai/sdk for JavaScript:
content array. Read blocks whose type is text;
thinking and tool blocks can appear before the text.
Control adaptive thinking
Setthinking.type to adaptive and choose an output_config.effort value.
The following example uses xhigh and reads the completed message from a stream:
max_tokens output limit. Leave room for the final
answer as well as thinking. A response can contain text without a visible
thinking block. Preserve any returned thinking blocks unchanged in conversation
history.
For the model-specific thinking configuration, see
Thinking.
The examples omit temperature, top_p, and top_k; consult the selected
model’s parameter documentation before adding sampling controls.
Cache prompts
Place a cache breakpoint on reference material that you reuse between requests. Save your reference material in a UTF-8reference.txt file before running this
example. Use a prefix that meets the selected model’s
minimum cacheable length:
cache_creation_input_tokenscounts tokens written to the cache.cache_read_input_tokenscounts tokens read from the cache.input_tokenscounts input processed outside those cache counters.
cache_read_input_tokens reports how many input tokens
were read from the cache. The OpenAPI Prompt Cache
example contains a complete fictional reference that you can save as
reference.txt to try the sample.
Stream responses
Setstream: true for Server-Sent Events. The SDK exposes text fragments as they
arrive:
message_start, content block events, message_delta,
and message_stop. Content blocks can contain text, thinking, or tool activity.
For a text block, content_block_delta contains a text_delta. Read final usage
and the stop reason from message_delta.
Control effort
Setoutput_config.effort to guide the amount of reasoning. This example uses
low for a short explanation and waits for the completed streamed message:
max_tokens separately to limit output length.
Use server tools
Server tools execute during the API request and return result blocks alongside Claude’s answer.- Web Fetch
- Web Search
web_fetch_tool_result block:server_tool_use block with a web_fetch_tool_result
containing the fetched document or a tool error.Return client tool results
For a client tool, Claude returns atool_use block. Run your application function
and send its result in a tool_result block with the matching tool_use_id.
Preserve the complete assistant content between the two requests.
This example supplies a fictional order result and asks Claude to use that result:
Response example
A non-streaming request returns a message object. The following example shows its text and usage fields with an illustrative message identifier:end_turn completes the answer,
max_tokens means output reached the limit, and tool_use requests a client tool
result. For a server-tool turn that returns pause_turn, continue with the
returned assistant content unchanged.Authorizations
Your CometAPI key passed via the x-api-key header. Authorization: Bearer $COMETAPI_KEY is also supported.
Headers
The Anthropic API version to use. Defaults to 2023-06-01.
"2023-06-01"
Comma-separated feature identifiers required by a specific beta API feature. Omit this header for the examples on this page.
Body
The Claude model to use. See the Models page for available Claude model IDs.
"claude-opus-5"
Conversation history. Use user and assistant messages with text strings or content-block arrays. Return complete assistant content blocks when continuing a tool call.
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 >= 11024
System prompt providing context and instructions to Claude. Can be a plain string or an array of content blocks (useful for prompt caching).
Sampling temperature. The examples omit sampling overrides.
0 <= x <= 1Nucleus sampling threshold. The examples omit sampling overrides.
0 <= x <= 1Limits sampling to the k most likely tokens. The examples omit sampling overrides.
x >= 0If 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.
Custom strings that cause the model to stop generating when encountered. The stop sequence is not included in the response.
Thinking configuration. The adaptive example uses type adaptive and sets output_config.effort separately.
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.
Controls how the model uses tools.
Request metadata for tracking and analytics.
Configuration for reasoning effort and structured output.
The service tier to use. auto tries priority capacity first, standard_only uses only standard capacity.
auto, standard_only Response
Successful response. When stream is true, the response is a stream of SSE events.
Message identifier returned by the API.
Always message.
message Always assistant.
assistant The response content blocks. May include text, thinking, tool_use, and other block types.
Model ID reported by the response.
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 The stop sequence that caused the model to stop, if applicable.
Token usage statistics.