> For the complete documentation index, see [llms.txt](https://docs.usedecentral.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.usedecentral.org/api-reference/completions.md).

# Chat Completions

`POST /v1/chat/completions`

Generates a model response for a conversation. The request and response format is identical to the OpenAI Chat Completions API.

***

## Request

```
POST https://api.usedecentral.org/v1/chat/completions
Authorization: Bearer dcntrl_live_your_key_here
Content-Type: application/json
```

### Request body

| Field               | Type            | Required | Description                                                                 |
| ------------------- | --------------- | -------- | --------------------------------------------------------------------------- |
| `model`             | string          | Yes      | Model ID to use. See \[Models]\().                                          |
| `messages`          | array           | Yes      | List of messages in the conversation.                                       |
| `stream`            | boolean         | No       | If `true`, returns a stream of SSE events. Defaults to `false`.             |
| `max_tokens`        | integer         | No       | Maximum number of tokens to generate.                                       |
| `temperature`       | number          | No       | Sampling temperature (0.0 to 2.0).                                          |
| `top_p`             | number          | No       | Nucleus sampling probability.                                               |
| `stop`              | string or array | No       | Stop sequences.                                                             |
| `frequency_penalty` | number          | No       | Penalize repeated tokens (-2.0 to 2.0).                                     |
| `presence_penalty`  | number          | No       | Penalize tokens that have already appeared (-2.0 to 2.0).                   |
| `seed`              | integer         | No       | Seed for deterministic sampling. Not guaranteed with distributed inference. |

### Message object

```json
{
  "role": "user",
  "content": "Explain how Solana achieves 400ms block times."
}
```

Valid roles: `system`, `user`, `assistant`.

### Example request

```json
{
  "model": "qwen3-8b",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant. Keep answers concise."
    },
    {
      "role": "user",
      "content": "What is a Solana Program Derived Address?"
    }
  ],
  "stream": false,
  "max_tokens": 512,
  "temperature": 0.7
}
```

***

## Response (non-streaming)

```json
{
  "id": "chatcmpl-job_8fx2kp3m...",
  "object": "chat.completion",
  "created": 1750000000,
  "model": "qwen3-8b",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "A Program Derived Address (PDA) is a Solana account address that is deterministically derived from a program ID and a set of seeds..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 48,
    "completion_tokens": 214,
    "total_tokens": 262
  }
}
```

### Response headers (non-streaming)

```
x-decentral-job-id: job_8fx2kp3m9qrstvwxyz
x-decentral-tx-signature: 4xKTgFZ...
x-decentral-settlement-tx: 7mNPqRT...
x-decentral-worker: 9WzDXwBcARV1LCkUjzAt7skwM
x-decentral-credits-remaining: 1412
```

***

## Streaming response

When `stream: true`, the response is a stream of Server-Sent Events in the OpenAI format. Each event contains a JSON `delta` with the newly generated content.

```
data: {"id":"chatcmpl-job_8fx2kp3m...","object":"chat.completion.chunk","created":1750000000,"model":"qwen3-8b","choices":[{"index":0,"delta":{"role":"assistant","content":"A "},"finish_reason":null}]}

data: {"id":"chatcmpl-job_8fx2kp3m...","object":"chat.completion.chunk","created":1750000000,"model":"qwen3-8b","choices":[{"index":0,"delta":{"content":"Program "},"finish_reason":null}]}

data: {"id":"chatcmpl-job_8fx2kp3m...","object":"chat.completion.chunk","created":1750000000,"model":"qwen3-8b","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
```

The final event before `[DONE]` includes `finish_reason`. After `[DONE]`, the `x-decentral-settlement-tx` header is available if you inspect the response after the stream closes.

### Streaming example

```bash
curl https://api.usedecentral.org/v1/chat/completions \
  -H "Authorization: Bearer dcntrl_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama-3.3-70b",
    "messages": [{"role": "user", "content": "Write a haiku about Solana."}],
    "stream": true
  }'
```

***

## Credit charges

Credits are locked in escrow when the request is received and released to the worker after the job settles.

| Charge point      | Timing                                              |
| ----------------- | --------------------------------------------------- |
| Escrow lock       | When the request is received, before routing        |
| Escrow release    | After proof-of-completion is verified on-chain      |
| Credit deduction  | On settlement confirmation                          |
| Refund on failure | Automatic if no worker completes within 120 seconds |

For long completions (over approximately 500 output tokens), the charge switches to a per-1,000-output-token rate. The initial escrow locks an estimated amount. If the actual output is shorter, the difference is credited back.

***

## Errors

| Status | Code                   | Meaning                                                                              |
| ------ | ---------------------- | ------------------------------------------------------------------------------------ |
| `400`  | `invalid_request`      | Malformed request body or missing required field                                     |
| `400`  | `model_not_found`      | The requested model ID does not exist                                                |
| `402`  | `insufficient_credits` | Credit balance too low for the requested tier                                        |
| `503`  | `no_workers_available` | No workers currently hosting the requested model. Includes `retry_after` in seconds. |
| `504`  | `job_timeout`          | No worker completed the job within 120 seconds. Credits were refunded.               |

See \[Errors]\(

) for the full error response format.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.usedecentral.org/api-reference/completions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
