Skip to main content
Anthropic Claude is a very powerful AI dialogue system that can generate smooth and natural replies in just a few seconds by inputting prompts. The Claude Messages API is the official native API format from Anthropic, which differs from the OpenAI compatible format (Chat Completion) by adopting Anthropic’s own request and response structure, allowing better utilization of Claude’s unique capabilities, such as multimodal content input, tool invocation, and advanced features like Extended Thinking. This document mainly introduces the usage process of the Claude Messages API, allowing us to use the native interface consistent with Anthropic’s official standards to invoke Claude’s dialogue capabilities.

Application Process

To use Claude Messages API, first open the Ace Data Cloud Console and copy your API Token. If you are not logged in, you will be redirected to sign in and brought back to this page automatically. A single API Token works across every service on the platform — no need to subscribe per service. New accounts receive free starter credit; when it runs low you can top up your shared balance in the console.
📘 Full documentation: Claude Messages API →

Basic Usage

The request path for the Claude Messages API is /v1/messages, consistent with the Anthropic official API. We need to provide at least three required parameters:
  • model: Choose the Claude model to use, such as claude-opus-4-20250514, claude-sonnet-4-20250514, etc.
  • messages: An array of input messages, each containing role (role) and content (content), where role supports user and assistant.
  • max_tokens: The maximum number of output tokens, used to limit the length of a single reply.
Common optional parameters:
  • system: System prompt used to set the model’s behavior and role.
  • temperature: Generation randomness, between 0-1, with higher values resulting in more diverse replies.
  • stream: Whether to use streaming responses; set to true for a word-by-word return effect.
  • stop_sequences: Custom stop sequences; the model will stop generating when encountering these texts.
  • top_p: Nucleus sampling parameter, used in conjunction with temperature to control generation randomness.
  • top_k: Sample only from the top K options with the highest probabilities.
  • tools: Tool definitions for allowing the model to invoke external functions.
  • tool_choice: Controls how the model uses the provided tools.

cURL Example

Python Example

After the call, the returned result is as follows:
Returned result field descriptions:
  • id: The unique identifier for this message.
  • type: Always message.
  • role: Always assistant.
  • content: An array of reply content, with each element containing type (e.g., text) and corresponding content.
  • model: The name of the model processing the request.
  • stop_reason: The reason for stopping, possible values include end_turn (normal end), max_tokens (reached maximum length), stop_sequence (encountered stop sequence), tool_use (tool invocation).
  • stop_sequence: If stopped due to a custom stop sequence, displays the matching stop sequence text.
  • usage: Token usage statistics, including input_tokens (number of input tokens) and output_tokens (number of output tokens).

System Prompt

The Claude Messages API supports setting a system prompt through the system field, used to define the model’s behavior, role, and context.

Python Example

By setting the system prompt, you can precisely control Claude’s role and behavior.

Streaming Response

This interface also supports streaming responses; setting the stream parameter to true will provide a step-by-step return effect, which is very suitable for implementing word-by-word display on a webpage.

Python Example

Streaming responses are returned in Server-Sent Events (SSE) format, with each line prefixed by event: and data:. Streaming event types include:
  • message_start: Message start, containing basic information about the message and model name.
  • content_block_start: Content block start.
  • content_block_delta: Content block incremental update, containing newly generated text segments.
  • content_block_stop: Content block end.
  • message_delta: Message-level incremental update, containing stop_reason and final usage information.
  • message_stop: Message end.
The output effect is as follows:
You can see that the content_block_delta events in the streaming response contain the progressively generated text content, and by concatenating all text_delta, you can obtain the complete reply.

JavaScript Example

Multi-turn Conversation

If you want to integrate multi-turn conversation functionality, you need to alternate the messages of the user and assistant roles in the messages array, including the previous conversation history.

Python Example

The response is as follows:
By passing the complete conversation history in messages, Claude can provide accurate answers based on the context.

Deep Thinking Model

Claude supports the Extended Thinking feature, which allows the model to perform internal reasoning before responding, improving the accuracy of handling complex questions. When using this feature, the thinking parameter needs to be passed.

Python Example

The response is as follows:
As you can see, the content array contains two content blocks:
  • type: "thinking": The model’s internal thought process, showing the reasoning steps.
  • type: "text": The final answer result.
Notes:
  • When using thinking, max_tokens needs to be greater than budget_tokens, as budget_tokens is the token budget allocated for the thinking process.
  • The larger the budget_tokens, the more space the model has for deeper reasoning, suitable for handling complex questions.

Visual Model

Claude supports multimodal input, allowing it to process both text and images simultaneously. In the Messages API, you can use visual capabilities by setting content to an array format and passing in image content blocks.

Using Base64 Encoded Images

Using URL Images

cURL Example

Supported image formats include: image/jpeg, image/png, image/gif, image/webp. Example of return result:

Tool Use

The Claude Messages API natively supports tool invocation functionality, allowing the model to call your predefined tools/functions when needed.

Python Example

When the model decides to call a tool, the content in the return result will contain a tool_use type content block:
Note that stop_reason is tool_use, indicating that the model needs to call a tool. Upon receiving this result, you need to execute the tool function and return the result in the form of tool_result to the model:
The model will generate the final natural language reply based on the result returned from the tool.

Differences with Chat Completion API

Ace Data Cloud provides two formats of the Claude API, with the main differences as follows: If your system is already integrated with the OpenAI format API, you can use the Chat Completion API for a seamless switch. If you need to utilize all native capabilities of Claude, it is recommended to use the Messages API.

Error Handling

When calling the API, if an error occurs, the API will return the corresponding error code and message. For example:
  • 400 token_mismatched: Bad request, possibly due to missing or invalid parameters.
  • 400 api_not_implemented: Bad request, possibly due to missing or invalid parameters.
  • 401 invalid_token: Unauthorized, invalid or missing authorization token.
  • 429 too_many_requests: Too many requests, you have exceeded the rate limit.
  • 500 api_error: Internal server error, something went wrong on the server.

Error Response Example

Conclusion

Through this document, you have learned how to use the Claude Messages API to call Claude’s conversational features in Anthropic’s native format. The Messages API supports a rich set of features including basic conversations, system prompts, streaming responses, multi-turn dialogues, deep thinking, visual understanding, and tool calls. If you have any questions, please feel free to contact our technical support team.