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

# Create Chat (OpenAI Compatible)

> OpenAI-compatible chat completions endpoint for drop-in integration.

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.thinnest.ai/v2/chats/openai \
    -H "Authorization: Bearer $THINNESTAI_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "agent_id": "ag_5d2678fd_e556",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      }
    ],
    "stream": false
  }'
  ```

  ```python Python theme={null}
  import requests

  url = "https://api.thinnest.ai/v2/chats/openai"
  headers = {
      "Authorization": "Bearer " + "YOUR_THINNESTAI_API_KEY",
      "Content-Type": "application/json",
  }

  payload = {
    "agent_id": "ag_5d2678fd_e556",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      }
    ],
    "stream": false
  }

  response = requests.post(url, headers=headers, json=payload)
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api.thinnest.ai/v2/chats/openai", {
    method: "POST",
    headers: {
      "Authorization": "Bearer " + "YOUR_THINNESTAI_API_KEY",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
    "agent_id": "ag_5d2678fd_e556",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      }
    ],
    "stream": false
  }),
  });

  const data = await response.json();
  console.log(data);
  ```

  ```go Go theme={null}
  package main

  import (
      "bytes"
      "fmt"
      "io"
      "net/http"
  )

  func main() {
      payload := []byte(`{
    "agent_id": "ag_5d2678fd_e556",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      }
    ],
    "stream": false
  }`)
      req, _ := http.NewRequest("POST", "https://api.thinnest.ai/v2/chats/openai", bytes.NewBuffer(payload))
      req.Header.Set("Authorization", "Bearer YOUR_THINNESTAI_API_KEY")
      req.Header.Set("Content-Type", "application/json")

      resp, err := http.DefaultClient.Do(req)
      if err != nil { panic(err) }
      defer resp.Body.Close()
      body, _ := io.ReadAll(resp.Body)
      fmt.Println(string(body))
  }
  ```
</RequestExample>

***

## Overview

This endpoint provides OpenAI-compatible chat completions, allowing you to use thinnestAI agents as a drop-in replacement for OpenAI's API. It redirects to `POST /v1/chat/completions`.

## Request Body

<ParamField body="agent_id" type="string" required>
  Agent public ID (ag\_\*) or internal ID
</ParamField>

<ParamField body="messages" type="array" required>
  Array of message objects with role and content
</ParamField>

<ParamField body="session_id" type="string">
  Session ID for conversation continuity
</ParamField>

<ParamField body="stream" type="boolean">
  Enable SSE streaming (default: false)
</ParamField>

### Message Object

| Field     | Type   | Description                      |
| --------- | ------ | -------------------------------- |
| `role`    | string | `system`, `user`, or `assistant` |
| `content` | string | Message content                  |

***

## Response `200`

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "chatcmpl-abc123",
    "agent_id": "ag_5d2678fd_e556",
    "session_id": "sess_xyz789",
    "content": "Hello! How can I help you today?",
    "usage": {
      "prompt_tokens": 25,
      "completion_tokens": 10,
      "total_tokens": 35
    }
  }
  ```
</ResponseExample>

### Streaming Response

When `stream: true`, returns Server-Sent Events:

```
data: {"type": "content", "data": "Hello"}
data: {"type": "content", "data": "! How can"}
data: {"type": "content", "data": " I help you?"}
data: {"type": "done", "session_id": "sess_xyz789", "metrics": {...}}
```

***

## SDK Usage

### Python (OpenAI SDK)

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

client = OpenAI(
    base_url="https://api.thinnest.ai/v1",
    api_key="ak_your_agent_api_key"
)

response = client.chat.completions.create(
    model="ag_5d2678fd_e556",  # agent_id as model
    messages=[
        {"role": "user", "content": "Hello!"}
    ]
)

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

### JavaScript (OpenAI SDK)

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

const client = new OpenAI({
  baseURL: 'https://api.thinnest.ai/v1',
  apiKey: 'ak_your_agent_api_key',
});

const response = await client.chat.completions.create({
  model: 'ag_5d2678fd_e556',
  messages: [{ role: 'user', content: 'Hello!' }],
});

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

***

## Errors

| Code  | Description                |
| ----- | -------------------------- |
| `401` | Missing or invalid API key |
| `402` | Insufficient balance       |
| `404` | Agent not found            |
| `429` | Rate limit exceeded        |
