Applying for and Using the Claude Chat Completion API
Anthropic Claude is a very powerful AI conversation system that can generate fluent, natural responses in just a few seconds with a prompt. Claude stands out in the industry for its outstanding language comprehension and generation capabilities. Today, Claude is widely used across various industries and fields, and its influence is growing increasingly significant. Whether for everyday conversations, creative writing, professional consulting, or coding, Claude can provide amazing intelligent assistance, greatly improving human productivity and creativity.
This document mainly introduces the process of using the Claude Chat Completion API, which allows us to easily use the official Claude conversation functionality.
¶ Claude Fable 5.2
Use the full model name claude-fable-5-2. This model is suitable for complex reasoning and long-running tasks; when native thinking content blocks and tool interactions are needed, prioritize Messages. Adaptive thinking is always enabled, so omit temperature, top_p, and top_k; use auto for tool selection, and do not force a specific tool. max_tokens covers both thinking and final text. The Responses and Count Tokens APIs are not currently available for this model.
Based on the current largest package (142,800 Credits / $13,584), Fable 5.2 standard input costs $3.20 and output costs $16 per million tokens; cache reads cost $0.08, and 5-minute / 1-hour cache writes cost $4 / $6.40 per million tokens, respectively. The above prices are 40% of the official standard API prices (60% cheaper); for other packages and additional account discounts, please refer to the model pricing page and your bill.
¶ Application Process
To use the Claude Chat Completion API, first go to the Ace Data Cloud Console to obtain your API Token for later use.

If you have not yet logged in or registered, you will be automatically redirected to the login page to register and log in, and then automatically returned to the current page.
A general application's API Key can be used for services enabled for the account; a dedicated application's Key is used within that application's service scope. Please check the Key's permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credits are subject to what is shown in your account; if the balance is insufficient, click the "Recharge" button.
📘 Full documentation: Claude Chat Completion API →
¶ Basic Usage
Next, you can fill in the corresponding content in the interface, as shown below:

When using this API for the first time, we need to fill in at least three items. One is authorization, which can be selected directly from the drop-down list. Another parameter is model, for which you can select public models such as claude-opus-5-5 and claude-fable-5-1; the original claude-fable-5 can still be used. The last parameter is messages, an array of prompts in which each element contains role and content; role supports user, assistant, and system, and content is the actual content. When complete Claude thinking content blocks, signatures, and native tool semantics are needed, please continue to use the Messages API.
You can also see that the corresponding invocation code is generated on the right. You can copy the code and run it directly, or click the "Try" button to test it.
Common optional parameters:
max_tokens: Limits the maximum number of tokens in a single response.temperature: Generation randomness, between 0 and 2; the higher the value, the more varied the output.n: The number of candidate responses to generate at once.response_format: Return format settings.
claude-opus-5-5 supports both standard and streaming Chat Completions; this model only allows n=1. Do not set temperature below 1 or use a non-default top_p for it. For tool calls, use tool_choice: "auto" and do not force a specific tool. The Chat-compatible API does not return native thinking content blocks.

After making the call, we find that the returned result is as follows:
{
"id": "msg_bdrk_01Q6WN27v95ypCa1kbanAQ6K",
"model": "claude-opus-4-8",
"object": "chat.completion",
"created": 1768619365,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 8,
"completion_tokens": 12,
"total_tokens": 20,
"prompt_tokens_details": {
"cached_tokens": 0,
"cache_write_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 0
}
}
}
The returned result contains several fields, described as follows:
id, the ID generated for this conversation task, used to uniquely identify it.model, the selected model from the official Claude website.choices, the response information Claude provides for the prompt.usage: Token statistics for this question and answer.
In the Chat Completions format, usage.prompt_tokens is the total input; prompt_tokens_details.cached_tokens and prompt_tokens_details.cache_write_tokens record cache read and write details, respectively. completion_tokens_details.reasoning_tokens is a subset of completion_tokens; do not add it again when calculating totals or costs. A non-streaming response may also return a usage.cost object, in which amount is the actual credit consumption, currency is the unit of measurement, and list_amount is the amount before discounts, if applicable.
The Chat-compatible field name is reasoning_tokens; the native Messages API uses output_tokens_details.thinking_tokens. The wire schemas of the two protocols are different, and clients should not mix the field names.
The choices field contains Claude's response information, and the choices inside it contains the specific details of Claude's response, as shown in the figure.

As you can see, the content field inside choices contains the specific content of Claude's response.
¶ Streaming Responses
This API also supports streaming responses, which is very useful for web integration and allows web pages to display text character by character.
If you want to return the response as a stream, you can change the stream parameter in the request header to true.
Make the change as shown below, but the invocation code also needs corresponding changes to support streaming responses.

Python sample call code:
import requests
url = "https://api.acedata.cloud/v1/chat/completions"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-opus-4-20250514",
"messages": [{"role":"user","content":"Hello"}],
"stream": True
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
The output is as follows:
data: {"id": "msg_bdrk_01LPPqDjLKMgfSwTRMRty9VT", "object": "chat.completion.chunk", "created": 1768619445, "model": "claude-opus-4-20250514", "system_fingerprint": null, "choices": [{"delta": {"content": "", "role": "assistant"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "msg_bdrk_01LPPqDjLKMgfSwTRMRty9VT", "object": "chat.completion.chunk", "created": 1768619445, "model": "claude-opus-4-20250514", "system_fingerprint": null, "choices": [{"delta": {"content": ""}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "msg_bdrk_01LPPqDjLKMgfSwTRMRty9VT", "object": "chat.completion.chunk", "created": 1768619445, "model": "claude-opus-4-20250514", "system_fingerprint": null, "choices": [{"delta": {"content": "Hello!"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "msg_bdrk_01LPPqDjLKMgfSwTRMRty9VT", "object": "chat.completion.chunk", "created": 1768619445, "model": "claude-opus-4-20250514", "system_fingerprint": null, "choices": [{"delta": {"content": " How can I help you"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "msg_bdrk_01LPPqDjLKMgfSwTRMRty9VT", "object": "chat.completion.chunk", "created": 1768619445, "model": "claude-opus-4-20250514", "system_fingerprint": null, "choices": [{"delta": {"content": " today?"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "msg_bdrk_01LPPqDjLKMgfSwTRMRty9VT", "object": "chat.completion.chunk", "created": 1768619445, "model": "claude-opus-4-20250514", "system_fingerprint": null, "choices": [{"delta": {}, "logprobs": null, "finish_reason": "stop", "index": 0}], "usage": null}
data: {"id": "msg_bdrk_01LPPqDjLKMgfSwTRMRty9VT", "object": "chat.completion.chunk", "created": 1768619445, "model": "claude-opus-4-20250514", "system_fingerprint": null, "choices": [], "usage": {"prompt_tokens": 8, "completion_tokens": 12, "total_tokens": 20, "prompt_tokens_details": {"cached_tokens": 0, "cache_write_tokens": 0}, "completion_tokens_details": {"reasoning_tokens": 0}}}
data: [DONE]
As you can see, the response contains many data entries, and the choices in each data entry contains the latest response content, consistent with the content described above. choices contains the newly added response content, which you can integrate into your system based on the results. At the same time, the end of the streaming response is determined by the content of data: if the content is [DONE], it indicates that the streaming response has completely ended. The returned data results contain several fields, described as follows:
id, the ID generated for this conversation task, used to uniquely identify it.model, the selected model from the official Claude website.choices, the response information Claude provides for the prompt.
JavaScript is also supported. For example, the Node.js streaming call code is as follows:
const options = {
method: "post",
headers: {
accept: "application/json",
authorization: "Bearer {token}",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-opus-4-20250514",
messages: [{ role: "user", content: "Hello" }],
stream: true,
}),
};
fetch("https://api.acedata.cloud/v1/chat/completions", options)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
Java sample code:
JSONObject jsonObject = new JSONObject();
jsonObject.put("model", "claude-opus-4-20250514");
jsonObject.put("messages", [{"role":"user","content":"Hello"}]);
jsonObject.put("stream", true);
MediaType mediaType = "application/json; charset=utf-8".toMediaType();
RequestBody body = jsonObject.toString().toRequestBody(mediaType);
Request request = new Request.Builder()
.url("https://api.acedata.cloud/v1/chat/completions")
.post(body)
.addHeader("accept", "application/json")
.addHeader("authorization", "Bearer {token}")
.addHeader("content-type", "application/json")
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
System.out.print(response.body!!.string())
You can rewrite the code for other languages yourself; the principle is the same.
¶ Multi-Turn Conversations
If you want to integrate multi-turn conversation functionality, you need to upload multiple prompts in the messages field. A specific example of multiple prompts is shown in the image below:

Python sample call code:
import requests
url = "https://api.acedata.cloud/v1/chat/completions"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-opus-4-20250514",
"messages": [{"role":"user","content":"Hello"},{"role":"assistant","content":"Hello! How can I help you today?"},{"role":"user","content":"What I say just now?"}]
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
By uploading multiple prompts, you can easily implement a multi-turn conversation and obtain the following response:
{
"id": "msg_bdrk_01Y1wfQmd89g968TVbFu57Yc",
"model": "claude-opus-4-20250514",
"object": "chat.completion",
"created": 1768619674,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "You said \"Hello\" - that was your first message to me in our conversation."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 29,
"completion_tokens": 20,
"total_tokens": 49,
"prompt_tokens_details": {
"cached_tokens": 0,
"cache_write_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 0
}
}
}
As you can see, the information contained in choices is consistent with the basic usage above. It contains the specific content of Claude's responses to multiple conversations, making it possible to answer the corresponding questions based on the content of those conversations.
¶ Deep Thinking
Newer-generation Claude models may perform reasoning according to the model's default policy, without requiring the -thinking model suffix to trigger it. Chat-compatible responses use the OpenAI-compatible usage field completion_tokens_details.reasoning_tokens. reasoning_tokens is already included in completion_tokens, so do not add them together again. Reasoning and visible text share the output token limit, so when max_tokens is low, the text may be empty or truncated; if this happens, increase the output budget or choose a lower reasoning effort when supported by the model.
If you need native thinking, output_config.effort, thinking content blocks, and signature return, use /v1/messages. The corresponding usage field for Messages is output_tokens_details.thinking_tokens; do not confuse it with the Chat-compatible completion_tokens_details.reasoning_tokens.
¶ Vision Model
claude-sonnet-4-20250514 is a multimodal large language model developed by Claude that adds visual understanding capabilities on top of claude-4. This model can process both text and image inputs, enabling cross-modal understanding and generation.
Text processing using the claude-sonnet-4-20250514 model is consistent with the basic usage described above. Below is a brief introduction to how to use the model's image processing capabilities.
The image processing capabilities of the claude-sonnet-4-20250514 model are primarily used by adding a type field to the existing content, which indicates whether the uploaded content is text or an image, thereby enabling the model's image processing capabilities. Below, we mainly describe two ways to call this feature: Curl and Python.
- Curl script method
curl -X POST 'https://api.acedata.cloud/v1/chat/completions' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What'\''s in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.acedata.cloud/ueugot.png"
}
}
]
}
]
}'
- Python script method
import requests
url = "https://api.acedata.cloud/v1/chat/completions"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{
"type": "text", "text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.acedata.cloud/ueugot.png"
}
},
],
}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
You can then obtain the following result. The field information in the result is consistent with the above, as shown below:
{
"id": "msg_bdrk_01NCrxpZmV17bhQJJRQEFEb9",
"model": "claude-sonnet-4-20250514",
"object": "chat.completion",
"created": 1768628904,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "This image shows an API request configuration interface for what appears to be an AI chat completion service. Here are the key elements:\n\n**Request Body Parameters:**\n\n1. **model** (required string) - Set to \"claude-opus-4-202505...\" - specifies which AI model to use\n\n2. **messages** (required array) - Contains the conversation history with:\n - **role** (required string) - Set to \"user\" \n - **content** (required string) - Contains \"Hello\" as the message content\n\n3. **stream** (boolean) - Set to \"true\" - enables partial message deltas like in ChatGPT\n\n4. **max_tokens** (number) - Field for setting maximum tokens that can be generated in the response\n\n5. **n** (number) - Specifies how many chat completion choices to generate for each input\n\nThe interface has a dark theme with white text on black/dark gray backgrounds. There's a \"Fill Example\" button at the bottom right and various dropdown menus and input fields for configuring the API request parameters. A red trash/delete icon is visible, likely for removing message entries."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 1570,
"completion_tokens": 252,
"total_tokens": 1822,
"prompt_tokens_details": {
"cached_tokens": 0,
"cache_write_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 0
}
}
}
As you can see, the response is based on the image. Therefore, the above two methods make it easy to use the text and image processing capabilities of the claude-3-7-sonnet-20250219 model.
¶ 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
{
"success": false,
"error": {
"code": "api_error",
"message": "fetch failed"
},
"trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
¶ Conclusion
Through this document, you have learned how to use the Claude Chat Completion API to easily implement the official Claude conversation functionality. We hope this document helps you better integrate and use the API. If you have any questions, please feel free to contact our technical support team.
