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

# Create Chat Completion

> Creates a completion for the chat message

## Request Body

<ParamField body="model" type="string" required>
  ID of the model to use. See [Models](https://tokenlab.sh/en/models) for available options.
</ParamField>

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

  Each message object contains:

  * `role` (string): `system`, `user`, or `assistant`
  * `content` (string | array): The message content

  When `content` is an array, TokenLab supports structured multimodal blocks for compatible models:

  * text: `{ "type": "text", "text": "..." }`
  * image: `{ "type": "image_url", "image_url": { "url": "https://..." } }`
  * video: `{ "type": "video_url", "video_url": { "url": "https://..." } }`
  * audio: `{ "type": "audio_url", "audio_url": { "url": "https://..." } }`

  For multimodal production traffic, prefer public `https` URLs. TokenLab will translate these media blocks into the provider-specific request shape required by the selected model.
</ParamField>

<ParamField body="temperature" type="number" default="1">
  Sampling temperature between 0 and 2. Higher values make output more random.
</ParamField>

<ParamField body="max_tokens" type="integer">
  Maximum number of tokens to generate.
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  If true, partial message deltas will be sent as SSE events.
</ParamField>

<ParamField body="stream_options" type="object">
  Options for streaming. Set `include_usage: true` to receive token usage in stream chunks.
</ParamField>

<ParamField body="top_p" type="number" default="1">
  Nucleus sampling parameter. We recommend altering this or temperature, not both.
</ParamField>

<ParamField body="frequency_penalty" type="number" default="0">
  Number between -2.0 and 2.0. Positive values penalize repeated tokens.
</ParamField>

<ParamField body="presence_penalty" type="number" default="0">
  Number between -2.0 and 2.0. Positive values penalize tokens already in the text.
</ParamField>

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

<ParamField body="tools" type="array">
  A list of tools the model may call (function calling).
</ParamField>

<ParamField body="tool_choice" type="string | object">
  Controls how the model uses tools. Options: `auto`, `none`, `required`, or a specific tool object.
</ParamField>

<ParamField body="parallel_tool_calls" type="boolean" default="true">
  Whether to enable parallel function calling. Set to false to call functions sequentially.
</ParamField>

<ParamField body="max_completion_tokens" type="integer">
  Maximum tokens for the completion. Alternative to `max_tokens`, useful for newer reasoning-enabled model families.
</ParamField>

<ParamField body="reasoning_effort" type="string">
  Reasoning effort for reasoning-enabled models. Options: `low`, `medium`, `high`.
</ParamField>

<ParamField body="seed" type="integer">
  Random seed for deterministic sampling.
</ParamField>

<ParamField body="n" type="integer" default="1">
  Number of completions to generate (1-128).
</ParamField>

<ParamField body="logprobs" type="boolean">
  Whether to return log probabilities.
</ParamField>

<ParamField body="top_logprobs" type="integer">
  Number of top log probabilities to return (0-20). Requires `logprobs: true`.
</ParamField>

<ParamField body="top_k" type="integer">
  Top-K sampling parameter (for Anthropic/Gemini models).
</ParamField>

<ParamField body="response_format" type="object">
  Response format specification. Use `{"type": "json_object"}` for JSON mode. Treat `{"type": "json_schema", "json_schema": {...}}` as a best-effort path that depends on the selected model and routed behavior.
</ParamField>

<ParamField body="logit_bias" type="object">
  Modify the likelihood of specified tokens appearing. Map token IDs (as strings) to bias values from -100 to 100.
</ParamField>

<ParamField body="user" type="string">
  A unique identifier representing your end-user for abuse monitoring.
</ParamField>

## Response

<ResponseField name="id" type="string">
  Unique identifier for the completion.
</ResponseField>

<ResponseField name="object" type="string">
  Always `chat.completion`.
</ResponseField>

<ResponseField name="created" type="integer">
  Unix timestamp of when the completion was created.
</ResponseField>

<ResponseField name="model" type="string">
  The model used for completion.
</ResponseField>

<ResponseField name="choices" type="array">
  List of completion choices.

  Each choice contains:

  * `index` (integer): Index of the choice
  * `message` (object): The generated message
  * `finish_reason` (string): Why the model stopped (`stop`, `length`, `tool_calls`)
</ResponseField>

<ResponseField name="usage" type="object">
  Token usage statistics.

  * `prompt_tokens` (integer): Tokens in the prompt
  * `completion_tokens` (integer): Tokens in the completion
  * `total_tokens` (integer): Total tokens used
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.tokenlab.sh/v1/chat/completions" \
    -H "Authorization: Bearer sk-your-api-key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4o",
      "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
      ],
      "temperature": 0.7,
      "max_tokens": 1000
    }'
  ```

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

  client = OpenAI(
      api_key="sk-your-api-key",
      base_url="https://api.tokenlab.sh/v1"
  )

  response = client.chat.completions.create(
      model="gpt-4o",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
      ],
      temperature=0.7,
      max_tokens=1000
  )

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

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

  const client = new OpenAI({
    apiKey: 'sk-your-api-key',
    baseURL: 'https://api.tokenlab.sh/v1'
  });

  const response = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: 'Hello!' }
    ],
    temperature: 0.7,
    max_tokens: 1000
  });

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

  ```php PHP theme={null}
  <?php
  $ch = curl_init('https://api.tokenlab.sh/v1/chat/completions');

  curl_setopt_array($ch, [
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POST => true,
      CURLOPT_HTTPHEADER => [
          'Content-Type: application/json',
          'Authorization: Bearer sk-your-api-key'
      ],
      CURLOPT_POSTFIELDS => json_encode([
          'model' => 'gpt-4o',
          'messages' => [
              ['role' => 'system', 'content' => 'You are a helpful assistant.'],
              ['role' => 'user', 'content' => 'Hello!']
          ],
          'temperature' => 0.7,
          'max_tokens' => 1000
      ])
  ]);

  $response = curl_exec($ch);
  curl_close($ch);

  $data = json_decode($response, true);
  echo $data['choices'][0]['message']['content'];
  ```
</RequestExample>

## Multimodal Example

```json theme={null}
{
  "model": "gemini-2.5-pro",
  "messages": [
    {
      "role": "user",
      "content": [
        { "type": "text", "text": "Describe this video briefly." },
        { "type": "video_url", "video_url": { "url": "https://example.com/demo.mp4" } }
      ]
    }
  ],
  "max_tokens": 64
}
```

<ResponseExample>
  ```json Response theme={null}
  {
    "id": "chatcmpl-abc123",
    "object": "chat.completion",
    "created": 1706000000,
    "model": "gpt-4o",
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "Hello! How can I help you today?"
        },
        "finish_reason": "stop"
      }
    ],
    "usage": {
      "prompt_tokens": 20,
      "completion_tokens": 9,
      "total_tokens": 29
    }
  }
  ```
</ResponseExample>
