> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nexllm.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat Completions API: Generate AI Responses via NexLLM

> Send messages to POST /v1/chat/completions to generate conversational AI responses from GPT, Claude, or Gemini models with optional streaming.

The Chat Completions endpoint is the primary way to interact with AI models through NexLLM. It follows the OpenAI Chat Completions format exactly, so any code or library already written for the OpenAI API works out of the box — just point your client at `https://www.nexllm.ai/v1` and swap in your NexLLM key.

## Endpoint

```
POST https://www.nexllm.ai/v1/chat/completions
```

## Request Parameters

<ParamField body="model" type="string" required>
  The ID of the model to use. NexLLM routes your request to the correct provider automatically. Examples: `gpt-4o`, `aws/claude-haiku-4-5`, `gemini-2.5-flash`.
</ParamField>

<ParamField body="messages" type="array" required>
  An array of message objects that make up the conversation history. Each object must include a `role` (`system`, `user`, or `assistant`) and a `content` string.

  ```json theme={null}
  [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Write a short welcome message for a new user." }
  ]
  ```
</ParamField>

<ParamField body="max_tokens" type="integer">
  The maximum number of tokens the model should generate in its response. Defaults to the model's configured maximum if omitted.
</ParamField>

<ParamField body="stream" type="boolean">
  When set to `true`, the API streams the response as [Server-Sent Events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) instead of returning a single JSON response. Defaults to `false`.
</ParamField>

<ParamField body="temperature" type="number">
  Controls the randomness of the output. Accepts a value between `0` and `2`. Lower values (e.g. `0.2`) produce more deterministic responses; higher values (e.g. `1.5`) produce more varied output. Defaults to `1`.
</ParamField>

## Response Fields

<ResponseField name="id" type="string">
  A unique identifier for this completion request, useful for logging and debugging.
</ResponseField>

<ResponseField name="choices" type="array">
  An array of generated response objects. Most requests return a single choice.
</ResponseField>

<ResponseField name="choices[].message.content" type="string">
  The text generated by the model for this choice.
</ResponseField>

<ResponseField name="usage.prompt_tokens" type="integer">
  The number of tokens consumed by the input messages.
</ResponseField>

<ResponseField name="usage.completion_tokens" type="integer">
  The number of tokens generated in the model's response.
</ResponseField>

## Code Examples

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="sk-xxxxxxxxxxxxxxxx",
      base_url="https://www.nexllm.ai/v1"
  )

  response = client.chat.completions.create(
      model="aws/claude-haiku-4-5",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Write a short welcome message for a new user."}
      ],
      max_tokens=100
  )

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

  ```bash curl theme={null}
  curl https://www.nexllm.ai/v1/chat/completions \
    -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "aws/claude-haiku-4-5",
      "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Write a short welcome message for a new user."}
      ],
      "max_tokens": 100
    }'
  ```
</CodeGroup>

## Streaming Responses

<Note>
  Set `stream: true` in your request body to receive the response as a stream of Server-Sent Events. Each event contains a partial delta of the generated text. This is useful for displaying output to users in real time as the model generates it. The OpenAI Python SDK handles SSE streaming automatically when you pass `stream=True` to the `create` call.
</Note>

```python theme={null}
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Tell me a short story."}],
    stream=True
)

for chunk in response:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
```
