# Create chat completion

`POST https://my.neuronai.uz/api/v1/chat/completions`

API reference › Language models

Generates the assistant's next message in a conversation.

It works with OpenAI's SDKs: set their base URL to `https://my.neuronai.uz/api/v1` and use your NeuronAI key. Set `stream: true` to receive the reply as it is written. Billed per token, for the prompt and the reply.

**Authentication:** `Authorization: Bearer <API key>`, a key with `llm:write`.

**Pricing:** per 1K tokens: Input 1.5 credits; Output 6 credits.

## Request body (`application/json`)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `model` | string | Yes | At most 128 characters. |
| `max_tokens` | integer \| null | No | Minimum 1. |
| `temperature` | number \| null | No | Minimum 0. Maximum 2. |
| `top_p` | number \| null | No | Minimum 0. Maximum 1. |
| `stop` | object | No | Text at which the model stops: one string, or a list of strings (OpenAI's `stop`). Sent to the model exactly as given, byte for byte, including a string of only whitespace such as `\n`. |
| `stream` | boolean \| null | No |  |
| `messages` | object[] | Yes |  |
| `messages[].role` | string | Yes | One of `system`, `user`, `assistant`, `tool`. |
| `messages[].content` | string | Yes | The message's text, as one string. A list of content parts, as OpenAI also takes, is refused. |

## Response (200, `application/json`, `text/event-stream`)

The reply, and the tokens billed in `usage`. With `stream: true`, server-sent events instead.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | Yes |  |
| `object` | string | Yes |  |
| `created` | integer | Yes |  |
| `model` | string | Yes |  |
| `choices` | object[] | Yes |  |
| `choices[].index` | integer | Yes |  |
| `choices[].message` | object | Yes |  |
| `choices[].message.role` | string | Yes |  |
| `choices[].message.content` | string | Yes |  |
| `choices[].finish_reason` | string | Yes |  |
| `usage` | object | Yes |  |
| `usage.prompt_tokens` | integer | Yes |  |
| `usage.completion_tokens` | integer | Yes |  |
| `usage.total_tokens` | integer | Yes |  |

```json
{
  "id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41",
  "object": "chat.completion",
  "created": 1794912659,
  "model": "gemma-4-26b-a4b",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Salom! Bugun sizga qanday yordam bera olaman?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 24,
    "completion_tokens": 18,
    "total_tokens": 42
  }
}
```

### Headers

| Header | Type | Description |
| --- | --- | --- |
| `X-Cost` | number | The price billed, in credits. Sent with the JSON completion; a stream is billed when it ends, so it carries none. |
| `X-Accel-Buffering` | string | Sent as `no` with a stream: it tells a proxy such as nginx not to hold back the events. |
| `X-Request-Id` | string | Identifies this request. A JSON body carries it as `request_id` too; quote it to support. |
| `X-RateLimit-Limit` | integer | The requests a minute the rate limit on this endpoint allows. Sent once the limit has counted the request, so not when the key itself is refused. |
| `X-RateLimit-Remaining` | integer | How many of them are left this minute. |

## Errors

Every error has the same JSON shape:

```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_request",
    "message": "The request is malformed or failed validation.",
    "param": null
  },
  "request_id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41"
}
```

| Status | Meaning |
| --- | --- |
| 401 | The API key is missing or not recognized (`invalid_api_key`), revoked (`api_key_revoked`), disabled (`api_key_disabled`) or expired (`api_key_expired`). |
| 402 | Not enough balance (`insufficient_balance`), or over the key's monthly spend limit (`spend_limit_exceeded`). Nothing is charged. |
| 403 | The key lacks the permission this endpoint needs (`insufficient_permissions`), or the client IP is not in its allowlist (`ip_not_allowed`). |
| 404 | `model` is not one of the models `GET /api/v1/models` lists: `model_not_found`. |
| 422 | The request failed validation (`invalid_request`), or the prompt with room for `max_tokens` does not fit the model's context (`context_length_exceeded`). |
| 429 | A rate or concurrency limit was reached (`rate_limit_exceeded`, `concurrency_limit_exceeded`). Retry after `Retry-After` seconds. |
| 502 | Without `stream`: the model failed or could not be reached, `upstream_error`. Nothing is charged. A stream reports a failure as an event instead. |
| default | Any other error, in the same shape, such as `server_error` (500) or `service_unavailable` (503). |

## Examples

### cURL

```bash
curl -X POST "https://my.neuronai.uz/api/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <your-api-key>" \
  -d '{
    "model": "gemma-4-26b-a4b",
    "messages": [
      {"role": "user", "content": "Salom! Bugun ob-havo qanday?"}
    ]
  }'
```

### TypeScript SDK

```typescript
import {NeuronAI} from "@naiuz/sdk";

const client = new NeuronAI();

const completion = await client.chat.completions.create({
    model: "gemma-4-26b-a4b",
    messages: [{role: "user", content: "Salom! Bugun ob-havo qanday?"}],
});

console.log(completion.choices[0]?.message.content);
```

### Python SDK

```python
from naiuz import NeuronAI

client = NeuronAI()

completion = client.chat.completions.create(
    model="gemma-4-26b-a4b",
    messages=[{"role": "user", "content": "Salom! Bugun ob-havo qanday?"}],
)

print(completion.choices[0].message.content)
```

### PHP SDK

```php
$client = new \Naiuz\NeuronAI();

$completion = $client->chat->completions->create([
    'model' => 'gemma-4-26b-a4b',
    'messages' => [
        ['role' => 'user', 'content' => 'Salom! Bugun ob-havo qanday?'],
    ],
]);

echo $completion->choices[0]->message->content, PHP_EOL;
```

### Python (OpenAI SDK)

```python
from openai import OpenAI

client = OpenAI(base_url="https://my.neuronai.uz/api/v1", api_key="<your-api-key>")

completion = client.chat.completions.create(
    model="gemma-4-26b-a4b",
    messages=[{"role": "user", "content": "Salom! Bugun ob-havo qanday?"}],
)

print(completion.choices[0].message.content)
```

### JavaScript (OpenAI SDK)

```javascript
import OpenAI from 'openai';

const client = new OpenAI({
    baseURL: 'https://my.neuronai.uz/api/v1',
    apiKey: '<your-api-key>',
});

const completion = await client.chat.completions.create({
    model: 'gemma-4-26b-a4b',
    messages: [{ role: 'user', content: 'Salom! Bugun ob-havo qanday?' }],
});

console.log(completion.choices[0].message.content);
```

### PHP

```php
$ch = curl_init('https://my.neuronai.uz/api/v1/chat/completions');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer <your-api-key>',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'model' => 'gemma-4-26b-a4b',
        'messages' => [
            ['role' => 'user', 'content' => 'Salom! Bugun ob-havo qanday?'],
        ],
    ]),
]);

$response = json_decode(curl_exec($ch), true);
curl_close($ch);

echo $response['choices'][0]['message']['content'];
```

---

Source: https://docs.neuronai.uz/api/reference/language-models/create-chat-completion
