# Chat Completions

## Create a chat completion

**POST `/v1/chat/completions`**. The shorter `/v1/chat` path accepts the same body.

~~~json
{
  "model": "deepseek-v4.1-flash-1017",
  "messages": [
    {"role": "system", "content": "Answer concisely."},
    {"role": "user", "content": "How does a queue differ from a stack?"}
  ],
  "max_tokens": 256
}
~~~

| Field | Use |
|---|---|
| `model` | An ID from [your model catalogue](/models/). |
| `messages` | Ordered conversation. Use `user`, `assistant`, `system`, `developer` and matching `tool` messages. |
| `max_tokens` / `max_completion_tokens` | Positive output budget, capped at 16,384 tokens. `max_completion_tokens` takes precedence when supplied. |
| `stream` | Use `true` for [server-sent events](/streaming/). |
| `n` | Generate one to five choices. Each output counts towards the account rate limit. |
| `thinking` / `reasoning_effort` | Enable reasoning and choose a supported level where offered. |
| `tools` / `tool_choice` | Advertise built-in or custom [tools](/tools/) and select auto, none, required or a specific function. |
| `response_format` | Request text, a JSON object or a JSON schema. |
| `system_prompt` / `agents_md` | [Custom instructions](/instructions/) sent with the request. |
| `store` | Use `true` to [store the completion](/responses/#store-chat-completions) for retrieval. |

Sampling options include `temperature`, `top_p`, `stop`, `seed`, `frequency_penalty` and `presence_penalty`. Support can depend on the selected model.

## Read the result

~~~json
{
  "object": "chat.completion",
  "model": "deepseek-v4.1-flash-1017",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "A queue is first in, first out. A stack is last in, first out."},
      "finish_reason": "stop"
    }
  ]
}
~~~

The example omits identifiers, timestamps and usage. `finish_reason` can be `stop`, `length`, or `tool_calls`. A `length` result may be incomplete. Read `message.tool_calls` when the model asks your client to call a function.

If Site6 reports an alternate answer model after an eligible tool recovery, `answer_model` identifies it. `tool_router_model` identifies tool selection when available.

## Request JSON

Ask for JSON in the message as well as setting `response_format`.

~~~json
{
  "model": "deepseek-v4.1-flash-1017",
  "messages": [{"role": "user", "content": "Return JSON with a boolean field named ready."}],
  "max_tokens": 96,
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "ready_result",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {"ready": {"type": "boolean"}},
        "required": ["ready"],
        "additionalProperties": false
      }
    }
  }
}
~~~

Use `{"type":"json_object"}` for a JSON object without a specified schema. Structured output and active tools belong in separate requests.

## Validate structured results

Use a schema when your application needs predictable keys and types. Define the required fields explicitly and set `additionalProperties: false` when you want a closed object. If the model returns a truncated choice, increase the output budget or ask for a smaller object.

~~~javascript
const choice = completion.choices[0];
if (choice.finish_reason !== "stop") {
  throw new Error("Incomplete result: " + choice.finish_reason);
}

const value = JSON.parse(choice.message.content);
if (typeof value?.ready !== "boolean") {
  throw new Error("Expected a boolean ready field");
}
~~~

This check matches the small schema above. Validate the full schema in your application before writing to a database or calling another service. Use [Responses structured output](/responses/#reasoning-and-structured-output) if your client sends `text.format`.
