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

# Send chat completions using TokModel's unified LLM API

> Use TokModel's OpenAI-compatible chat endpoint to generate text, hold conversations, and stream responses from 30+ model providers.

The `/v1/chat/completions` endpoint is the primary way to generate text and hold multi-turn conversations with any model available through TokModel. Because it follows the OpenAI Chat Completions API shape, you can drop TokModel into any existing OpenAI client by changing only the `base_url` — no other code changes required.

## Authentication

Every request must include your API key in the `Authorization` header:

```http theme={null}
Authorization: Bearer YOUR_API_KEY
```

You can create and manage API keys in the [TokModel console](https://tokmodel.com/console).

## Send a basic request

The minimum required fields are `model` and `messages`. The `messages` array holds an ordered conversation history, where each entry has a `role` (`system`, `user`, or `assistant`) and a `content` string.

<CodeGroup>
  ```bash curl theme={null}
  curl https://tokmodel.com/v1/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "messages": [
        {"role": "user", "content": "Explain what a transformer model is in two sentences."}
      ]
    }'
  ```

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

  client = OpenAI(
      api_key="YOUR_API_KEY",
      base_url="https://tokmodel.com/v1",
  )

  response = client.chat.completions.create(
      model="openai/gpt-4o",
      messages=[
          {"role": "user", "content": "Explain what a transformer model is in two sentences."}
      ],
  )

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

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

  const client = new OpenAI({
    apiKey: "YOUR_API_KEY",
    baseURL: "https://tokmodel.com/v1",
  });

  const response = await client.chat.completions.create({
    model: "openai/gpt-4o",
    messages: [
      { role: "user", content: "Explain what a transformer model is in two sentences." }
    ],
  });

  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

## Example response

A successful request returns a JSON object. The generated text is in `choices[0].message.content`.

```json theme={null}
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1716489600,
  "model": "openai/gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "A transformer model is a neural network architecture that uses self-attention mechanisms to process sequences of data in parallel. It was introduced in the 2017 paper \"Attention Is All You Need\" and has become the foundation for modern large language models like GPT and BERT."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 52,
    "total_tokens": 70
  }
}
```

## Use a system message

A `system` message sets the behavior and context for the assistant. Place it as the first entry in the `messages` array.

```python python theme={null}
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[
        {
            "role": "system",
            "content": "You are a concise technical assistant. Reply in plain text only, no markdown."
        },
        {
            "role": "user",
            "content": "What is the difference between supervised and unsupervised learning?"
        }
    ],
)

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

## Stream the response

Set `stream: true` to receive tokens as server-sent events (SSE) instead of waiting for the full response. This reduces perceived latency for long outputs.

<CodeGroup>
  ```bash curl theme={null}
  curl https://tokmodel.com/v1/chat/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "stream": true,
      "messages": [
        {"role": "user", "content": "Write a short poem about APIs."}
      ]
    }'
  ```

  ```python python theme={null}
  stream = client.chat.completions.create(
      model="openai/gpt-4o",
      stream=True,
      messages=[
          {"role": "user", "content": "Write a short poem about APIs."}
      ],
  )

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

  ```javascript javascript theme={null}
  const stream = await client.chat.completions.create({
    model: "openai/gpt-4o",
    stream: true,
    messages: [
      { role: "user", content: "Write a short poem about APIs." }
    ],
  });

  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content ?? "";
    process.stdout.write(content);
  }
  ```
</CodeGroup>

## Switch models

Change the `model` parameter to route your request to a different provider. TokModel uses the format `provider/model-name`. No other code changes are needed.

```python python theme={null}
# Use Anthropic Claude
response = client.chat.completions.create(
    model="anthropic/claude-opus-4-5",
    messages=[{"role": "user", "content": "Summarize the water cycle."}],
)

# Use Google Gemini
response = client.chat.completions.create(
    model="google/gemini-2.0-flash",
    messages=[{"role": "user", "content": "Summarize the water cycle."}],
)

# Use Meta Llama
response = client.chat.completions.create(
    model="meta-llama/llama-3.3-70b-instruct",
    messages=[{"role": "user", "content": "Summarize the water cycle."}],
)
```

<Tip>
  Browse all available models and their provider slugs in the [Models reference](/concepts/models).
</Tip>

## Multi-turn conversation

Build a conversation history by appending each assistant reply to the `messages` array before sending the next user message.

```python python theme={null}
messages = [
    {"role": "system", "content": "You are a helpful coding assistant."}
]

# Turn 1
messages.append({"role": "user", "content": "What does the `zip` function do in Python?"})
response = client.chat.completions.create(model="openai/gpt-4o", messages=messages)
reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
print(reply)

# Turn 2
messages.append({"role": "user", "content": "Show me an example with two lists."})
response = client.chat.completions.create(model="openai/gpt-4o", messages=messages)
print(response.choices[0].message.content)
```
