Chat completions
POSThttps://api.aerless.ai/v1/chat/completions
The OpenAI chat-completions format. Send messages, get the reply, whole or streamed.
Build a request#
curl https://api.aerless.ai/v1/chat/completions \
curl -N https://api.aerless.ai/v1/chat/completions \
-H "Authorization: Bearer $AERLESS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4.1-flash-abliterated",
"stream": true,
"stream_options": {"include_usage": true},
"reasoning_effort": "off",
"reasoning_effort": "low",
"reasoning_effort": "high",
"reasoning_effort": "max",
"tools": [{"type": "function", "function": {
"name": "get_weather",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}}
}}],
"messages": [{"role": "user", "content": "Write a two-line poem about the sea."}]
"messages": [{"role": "user", "content": "What is the weather in Lisbon?"}]
"messages": [{"role": "user", "content": [
{"type": "text", "text": "What is in this picture?"},
{"type": "image_url", "image_url": {"url": "‹image URL›"}}
]}]
}'
{
"id": "‹id›",
"object": "chat.completion",
"created": ‹unix time›,
"model": "deepseek-v4.1-flash-abliterated",
"choices": [{"index": 0, "message": {"role": "assistant", "content": "‹the reply›"}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": ‹input tokens›, "completion_tokens": ‹output tokens›}
}
{
"id": "‹id›",
"object": "chat.completion",
"created": ‹unix time›,
"model": "deepseek-v4.1-flash-abliterated",
"choices": [{"index": 0, "message": {"role": "assistant", "reasoning_content": "‹the reasoning›", "content": "‹the reply›"}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": ‹input tokens›, "completion_tokens": ‹output tokens›}
}
{
"id": "‹id›",
"object": "chat.completion",
"created": ‹unix time›,
"model": "deepseek-v4.1-flash-abliterated",
"choices": [{"index": 0, "message": {"role": "assistant", "content":null, "tool_calls": [{"id": "‹call id›", "type": "function", "function": {"name": "get_weather", "arguments": "‹the arguments as a JSON string›"}}]}, "finish_reason": "tool_calls"}],
"usage": {"prompt_tokens": ‹input tokens›, "completion_tokens": ‹output tokens›}
}
{
"id": "‹id›",
"object": "chat.completion",
"created": ‹unix time›,
"model": "deepseek-v4.1-flash-abliterated",
"choices": [{"index": 0, "message": {"role": "assistant", "reasoning_content": "‹the reasoning›", "content":null, "tool_calls": [{"id": "‹call id›", "type": "function", "function": {"name": "get_weather", "arguments": "‹the arguments as a JSON string›"}}]}, "finish_reason": "tool_calls"}],
"usage": {"prompt_tokens": ‹input tokens›, "completion_tokens": ‹output tokens›}
}
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"content":"‹part of the reply›"}}]}
‹more chunks›
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[],"usage":{"prompt_tokens":‹input tokens›,"completion_tokens":‹output tokens›}}
data: [DONE]
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"reasoning_content":"‹part of the reasoning›"}}]}
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"content":"‹part of the reply›"}}]}
‹more chunks›
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[],"usage":{"prompt_tokens":‹input tokens›,"completion_tokens":‹output tokens›}}
data: [DONE]
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"‹call id›","type":"function","function":{"name":"get_weather","arguments":"‹part of the arguments›"}}]}}]}
‹more chunks›
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[],"usage":{"prompt_tokens":‹input tokens›,"completion_tokens":‹output tokens›}}
data: [DONE]
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"reasoning_content":"‹part of the reasoning›"}}]}
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"‹call id›","type":"function","function":{"name":"get_weather","arguments":"‹part of the arguments›"}}]}}]}
‹more chunks›
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[],"usage":{"prompt_tokens":‹input tokens›,"completion_tokens":‹output tokens›}}
data: [DONE]
# 400: the model reads text only on this API.
{
"error": {
"message": "‹what is wrong›",
"type": "invalid_request_error",
"param": "‹the field›",
"code": "invalid_request"
}
}
Reply: choices[0].message.content. Tool calls: choices[0].message.tool_calls. Tokens: usage. Every response carries x-request-id; quote it to support.
Request parameters#
| Field | Type | What it does |
|---|---|---|
model | string, required | E.g. deepseek-v4.1-flash-abliterated. See models. |
messages | array, required | At least one. role: system, developer, user, assistant, tool, function; developer is treated as system. content: a string or an array of {"type": "text", "text": "..."} parts. Other fields: see below. |
stream | boolean | Server-sent events. See streaming. |
stream_options | object | {"include_usage": true} adds a final usage chunk. |
max_tokens | integer ≥ 1 | Output cap, reasoning included. Default 8,192. Above 128,000, lowered to 128,000. max_completion_tokens is the same. |
reasoning_effort | string | off, low, high, max. OpenAI values are translated. See reasoning. |
tools | array | Callable functions. See tool calling. |
n | integer | Must be 1 if sent. |
temperature | number, 0 to 2 | Sampling temperature. |
top_p | number, 0 to 1 | Nucleus sampling. |
presence_penalty | number, −2 to 2 | Presence penalty. |
frequency_penalty | number, −2 to 2 | Frequency penalty. |
stop | string or array of strings | Stop sequences. |
Other fields: passed on, removed or refused
Message fields. Also name, tool_calls, tool_call_id, function_call, reasoning_content, reasoning, refusal, prefix. On a message sent back from an earlier reply, annotations, audio, reasoning_details, cache_control are accepted and removed.
Passed to the model. Effect depends on the model: top_k, min_p, repetition_penalty, seed, tool_choice, parallel_tool_calls, functions, function_call, response_format, user, logprobs, top_logprobs, logit_bias.
Client hints. Accepted and removed early: store, metadata, service_tier, prompt_cache_key, prompt_cache_retention, prompt_cache_options, safety_identifier, verbosity, prediction, transforms, provider, route, models, usage, include_reasoning, modalities. modalities is removed only when it asks for text. Each must have its usual type, or the request is refused. OpenRouter’s reasoning object is folded into reasoning_effort.
Refused. audio, web_search_options, plugins, moderation: 400.
Image parts. Text only: image_url and similar parts get 400.
Anything else. Any other field, or a wrong or out-of-range value, gets 400 invalid_request; param names the field.
Request limits#
- 8 MiB
- 2,097,152 chars
- 1,000,000 tokens
- 20 min
- 64 levels
How each limit applies
- Body: 32 MiB when the key’s tokens-per-minute limit is raised. Over the limit: 413, before reading when
Content-Lengthsays so. Content-Encodinggzip,deflateorbris decompressed and held to the same limit. Others: 415.- Text: counted across all fields (413 beyond).
- Context: prompt and output together.
- Time: body within 60 seconds; request at most 20 minutes.
Streaming#
"stream": true sends chat.completion.chunk objects as server-sent events (text/event-stream), ended by data: [DONE].
Full streaming example in curl, Python and Node
"stream_options": {"include_usage": true} adds a last chunk with an empty choices array and usage. The charge is the same.
curl -N https://api.aerless.ai/v1/chat/completions \
-H "Authorization: Bearer $AERLESS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4.1-flash-abliterated",
"stream": true,
"stream_options": {"include_usage": true},
"messages": [{"role": "user", "content": "Write a two-line poem about the sea."}]
}'import os
from openai import OpenAI
client = OpenAI(base_url="https://api.aerless.ai/v1", api_key=os.environ["AERLESS_API_KEY"])
stream = client.chat.completions.create(
model="deepseek-v4.1-flash-abliterated",
messages=[{"role": "user", "content": "Write a two-line poem about the sea."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.usage:
print("\n", chunk.usage)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.aerless.ai/v1", apiKey: process.env.AERLESS_API_KEY });
const stream = await client.chat.completions.create({
model: "deepseek-v4.1-flash-abliterated",
messages: [{ role: "user", content: "Write a two-line poem about the sea." }],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
if (chunk.usage) console.log("\n", chunk.usage);
}data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"content":"‹part of the reply›"}}]}
‹more chunks›
data: {"id":"‹id›","object":"chat.completion.chunk","created":‹unix time›,"model":"deepseek-v4.1-flash-abliterated","choices":[],"usage":{"prompt_tokens":‹input tokens›,"completion_tokens":‹output tokens›}}
data: [DONE]Errors during a stream#
The status is already 200. An error arrives as an event in the usual format, then data: [DONE].
The error event, and what is charged
data: {"error":{"message":"The model backend returned an error. Retry the request.","type":"api_error","param":null,"code":"upstream_error"}}
data: [DONE]Check each event for error. Tokens delivered before it are charged; no retry happens after output has started.
The stream ends if the client disconnects, reads nothing for 120 seconds, or the request reaches 20 minutes. Everything generated up to then is charged, delivered or not.
Output check#
Answers get the same content check as requests. A stopped answer returns 403 content_blocked, or that error as the last event of a stream.
What is charged, and the hourly suspension
The prompt and everything generated are charged. 3 such answers in an hour suspend the account for an hour (403 account_suspended).
Tool calling#
Supported: OpenAI-style function calling.
- List your functions in
tools. - The reply has
finish_reason: "tool_calls"and atool_callsarray. - Run it, append the reply and a
toolmessage with itstool_call_id, and call again.
Full tool-calling example in Python
import json, os
from openai import OpenAI
client = OpenAI(base_url="https://api.aerless.ai/v1", api_key=os.environ["AERLESS_API_KEY"])
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
messages = [{"role": "user", "content": "What is the weather in Lisbon?"}]
first = client.chat.completions.create(model="deepseek-v4.1-flash-abliterated", messages=messages, tools=tools)
call = first.choices[0].message.tool_calls[0]
args = json.loads(call.function.arguments)
messages.append(first.choices[0].message)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps({"city": args["city"], "temp_c": 19, "sky": "clear"}),
})
final = client.chat.completions.create(model="deepseek-v4.1-flash-abliterated", messages=messages, tools=tools)
print(final.choices[0].message.content){
"id": "‹id›",
"object": "chat.completion",
"created": ‹unix time›,
"model": "deepseek-v4.1-flash-abliterated",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "‹call id›",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "‹the arguments as a JSON string›"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": ‹input tokens›,
"completion_tokens": ‹output tokens›
}
}Reasoning#
reasoning_effort: off for none (lowest latency and cost), low for brief, high for harder problems, max for the most.
OpenAI values, reasoning text and tokens
OpenAI values are translated: none to off, disabled to off, minimal to off, medium to low, default to low, xhigh to max. Anything else: 400.
Reasoning text is in message.reasoning_content (delta.reasoning_content when streaming), apart from content.
Reasoning tokens are output tokens: in completion_tokens, at the output price, counted towards max_tokens. Leave room at high settings or the answer may be cut short.
Full reasoning example in curl, Python and Node
curl https://api.aerless.ai/v1/chat/completions \
-H "Authorization: Bearer $AERLESS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4.1-flash-abliterated",
"reasoning_effort": "high",
"messages": [{"role": "user", "content": "How many weekdays are there in March 2027?"}]
}'import os
from openai import OpenAI
client = OpenAI(base_url="https://api.aerless.ai/v1", api_key=os.environ["AERLESS_API_KEY"])
response = client.chat.completions.create(
model="deepseek-v4.1-flash-abliterated",
messages=[{"role": "user", "content": "How many weekdays are there in March 2027?"}],
# off, low, high or max. Sent through extra_body because the SDK's own
# type for reasoning_effort does not list "off" and "max".
extra_body={"reasoning_effort": "high"},
)
message = response.choices[0].message
print(getattr(message, "reasoning_content", None)) # the model's reasoning, if any
print(message.content)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.aerless.ai/v1", apiKey: process.env.AERLESS_API_KEY });
const response = await client.chat.completions.create({
model: "deepseek-v4.1-flash-abliterated",
messages: [{ role: "user", content: "How many weekdays are there in March 2027?" }],
reasoning_effort: "high", // off, low, high or max
});
const message = response.choices[0].message;
console.log(message.reasoning_content); // the model's reasoning, if any
console.log(message.content);{
"id": "‹id›",
"object": "chat.completion",
"created": ‹unix time›,
"model": "deepseek-v4.1-flash-abliterated",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"reasoning_content": "‹the reasoning›",
"content": "‹the reply›"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": ‹input tokens›,
"completion_tokens": ‹output tokens›
}
}Usage and caching#
| Field | What it counts |
|---|---|
usage.prompt_tokens | Input tokens, cached ones included. |
usage.completion_tokens | Output tokens, reasoning included. |
usage.prompt_tokens_details.cached_tokens | Input tokens served from cache. Only when caching applied. |
How the charge is worked out
Without streaming, closing the connection does not stop the request; it is charged in full.
Charge = (prompt − cached tokens) × input price + cached tokens × cached price + completion tokens × output price, rounded up to a micro-dollar. See pricing. A stream that ends without usage is charged on an estimate.
Caching#
A request that starts like an earlier one can be served from cache, faster and at the cached-input price. Hits are not guaranteed.
How to get more cache hits
- Put stable content first: system prompt, tool definitions, reference documents.
- Append to a conversation rather than editing earlier turns.
- Requests from one key go to the same backend account where possible.