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

# Chat completions

> Generate a chat response from a model given a list of messages.

## POST /v1/chat/completions

Creates a chat completion for the given messages and model.

### Request body

<ParamField body="model" type="string" required>
  The model ID to use. See the [models page](https://openinference.xyz/models) for available models.

  Example: `openai/gpt-oss-120b`
</ParamField>

<ParamField body="messages" type="array" required>
  A list of messages comprising the conversation.

  Each message is an object with:

  * `role` — one of `system`, `user`, or `assistant`
  * `content` — the message text
</ParamField>

<ParamField body="max_tokens" type="integer">
  Maximum number of tokens to generate. Defaults to the model's maximum.
</ParamField>

<ParamField body="temperature" type="number">
  Sampling temperature between 0 and 2. Lower values are more deterministic. Defaults to `1`.
</ParamField>

<ParamField body="top_p" type="number">
  Nucleus sampling parameter. Defaults to `1`.
</ParamField>

<ParamField body="stream" type="boolean">
  If `true`, responses are sent as server-sent events. Defaults to `false`.
</ParamField>

<ParamField body="stop" type="string | array">
  Up to 4 sequences where the model will stop generating.
</ParamField>

### Example request

<CodeGroup>
  ```bash curl theme={null}
  curl https://api.openinference.xyz/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENINFERENCE_API_KEY" \
    -d '{
      "model": "openai/gpt-oss-120b",
      "messages": [
        {"role": "user", "content": "How many moons are there?"}
      ],
      "max_tokens": 100
    }'
  ```

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

  client = OpenAI(
      base_url="https://api.openinference.xyz/v1",
      api_key="your-api-key",
  )

  response = client.chat.completions.create(
      model="openai/gpt-oss-120b",
      messages=[
          {"role": "user", "content": "How many moons are there?"}
      ],
      max_tokens=100,
  )
  print(response.choices[0].message.content)
  ```

  ```javascript JavaScript theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    baseURL: "https://api.openinference.xyz/v1",
    apiKey: process.env.OPENINFERENCE_API_KEY,
  });

  const response = await client.chat.completions.create({
    model: "openai/gpt-oss-120b",
    messages: [
      { role: "user", content: "How many moons are there?" }
    ],
    max_tokens: 100,
  });
  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

### Example response

```json theme={null}
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1709000000,
  "model": "openai/gpt-oss-120b",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "There are over 200 known moons in our solar system..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 45,
    "total_tokens": 57
  }
}
```

### Streaming

Set `stream: true` to receive responses as server-sent events. Each event contains a chunk of the response.

```bash theme={null}
curl https://api.openinference.xyz/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENINFERENCE_API_KEY" \
  -d '{
    "model": "openai/gpt-oss-120b",
    "messages": [{"role": "user", "content": "Hello"}],
    "stream": true
  }'
```

Each chunk follows the format:

```
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"delta":{"content":"Hello"},"index":0}]}

data: [DONE]
```

The final chunk includes a `usage` field with token counts.
