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

# Create a chat completion

Sends a request for a model response for the given chat conversation. Supports both streaming and non-streaming modes, text, images, audio, video, files, function calling, reasoning, and structured outputs. Compatible with the OpenAI Chat Completions API format.

## Authentication

<ParamField header="Authorization" type="string" required>
  Bearer token. Use your API key as the bearer token in the Authorization header.

  Format: `Bearer <SUNRA_KEY>`
</ParamField>

## Request

This endpoint expects an object.

<ParamField body="messages" type="object[]" required>
  List of messages for the conversation. Each message has a `role` and `content`.

  <Expandable title="message types">
    <Tabs>
      <Tab title="SystemMessage">
        <ParamField body="role" type="string" required>
          Value: `system`.
        </ParamField>

        <ParamField body="content" type="string | object[]" required>
          The system message content. Can be a string or array of text content parts.
        </ParamField>

        <ParamField body="name" type="string">
          Optional name for the system message.
        </ParamField>
      </Tab>

      <Tab title="UserMessage">
        <ParamField body="role" type="string" required>
          Value: `user`.
        </ParamField>

        <ParamField body="content" type="string | object[]" required>
          The user message content. Can be a string or array of content parts.

          <Expandable title="content part types">
            <Tabs>
              <Tab title="text">
                <ParamField body="type" type="string" required>
                  Value: `text`.
                </ParamField>

                <ParamField body="text" type="string" required>
                  The text content.
                </ParamField>

                <ParamField body="cache_control" type="object">
                  Cache control for this content part.

                  <Expandable title="properties">
                    <ParamField body="type" type="string" required>
                      Value: `ephemeral`.
                    </ParamField>

                    <ParamField body="ttl" type="string">
                      Cache TTL. Supported values: `5m`, `1h`.
                    </ParamField>
                  </Expandable>
                </ParamField>
              </Tab>

              <Tab title="image_url">
                <ParamField body="type" type="string" required>
                  Value: `image_url`.
                </ParamField>

                <ParamField body="image_url" type="object" required>
                  Image URL object.

                  <Expandable title="properties">
                    <ParamField body="url" type="string" required>
                      URL of the image. Supports `data:` URLs for base64 encoded images.
                    </ParamField>

                    <ParamField body="detail" type="string">
                      Image detail level for vision models. Supported values: `auto`, `low`, `high`.
                    </ParamField>
                  </Expandable>
                </ParamField>
              </Tab>

              <Tab title="input_audio">
                <ParamField body="type" type="string" required>
                  Value: `input_audio`.
                </ParamField>

                <ParamField body="input_audio" type="object" required>
                  Audio input data.

                  <Expandable title="properties">
                    <ParamField body="data" type="string" required>
                      Base64 encoded audio data.
                    </ParamField>

                    <ParamField body="format" type="string" required>
                      Audio format (e.g., `wav`, `mp3`, `flac`, `m4a`, `ogg`, `aiff`, `aac`). Supported formats vary by provider.
                    </ParamField>
                  </Expandable>
                </ParamField>
              </Tab>

              <Tab title="video_url">
                <ParamField body="type" type="string" required>
                  Value: `video_url`.
                </ParamField>

                <ParamField body="video_url" type="object" required>
                  Video input object.

                  <Expandable title="properties">
                    <ParamField body="url" type="string" required>
                      URL of the video. Supports `data:` URLs for base64 encoded video.
                    </ParamField>
                  </Expandable>
                </ParamField>
              </Tab>

              <Tab title="file">
                <ParamField body="type" type="string" required>
                  Value: `file`.
                </ParamField>

                <ParamField body="file" type="object" required>
                  File content for document processing.

                  <Expandable title="properties">
                    <ParamField body="file_data" type="string">
                      File content as base64 data URL or URL.
                    </ParamField>

                    <ParamField body="file_id" type="string">
                      File ID for previously uploaded files.
                    </ParamField>

                    <ParamField body="filename" type="string">
                      Original filename.
                    </ParamField>
                  </Expandable>
                </ParamField>
              </Tab>
            </Tabs>
          </Expandable>
        </ParamField>

        <ParamField body="name" type="string">
          Optional name for the user.
        </ParamField>
      </Tab>

      <Tab title="DeveloperMessage">
        <ParamField body="role" type="string" required>
          Value: `developer`.
        </ParamField>

        <ParamField body="content" type="string | object[]" required>
          The developer message content. Can be a string or array of text content parts.
        </ParamField>

        <ParamField body="name" type="string">
          Optional name for the developer message.
        </ParamField>
      </Tab>

      <Tab title="AssistantMessage">
        <ParamField body="role" type="string" required>
          Value: `assistant`.
        </ParamField>

        <ParamField body="content" type="string | object[] | null">
          The assistant message content. Can be a string, array of content parts, or null (when tool\_calls are present).
        </ParamField>

        <ParamField body="name" type="string">
          Optional name for the assistant.
        </ParamField>

        <ParamField body="tool_calls" type="object[]">
          Tool calls made by the assistant.

          <Expandable title="properties">
            <ParamField body="id" type="string" required>
              Tool call identifier.
            </ParamField>

            <ParamField body="type" type="string" required>
              Value: `function`.
            </ParamField>

            <ParamField body="function" type="object" required>
              The function called.

              <Expandable title="properties">
                <ParamField body="name" type="string" required>
                  Function name.
                </ParamField>

                <ParamField body="arguments" type="string" required>
                  Function arguments as JSON string.
                </ParamField>
              </Expandable>
            </ParamField>
          </Expandable>
        </ParamField>

        <ParamField body="refusal" type="string | null">
          Refusal message if content was refused.
        </ParamField>

        <ParamField body="reasoning" type="string | null">
          Reasoning output text.
        </ParamField>

        <ParamField body="reasoning_details" type="object[]">
          Detailed reasoning information for extended thinking models.

          <Expandable title="reasoning detail types">
            <Tabs>
              <Tab title="summary">
                <ParamField body="type" type="string" required>
                  Value: `reasoning.summary`.
                </ParamField>

                <ParamField body="summary" type="string" required>
                  The reasoning summary text.
                </ParamField>

                <ParamField body="id" type="string | null">
                  Reasoning detail ID.
                </ParamField>

                <ParamField body="format" type="string | null">
                  Format. Supported values: `unknown`, `openai-responses-v1`, `azure-openai-responses-v1`, `xai-responses-v1`, `anthropic-claude-v1`, `google-gemini-v1`.
                </ParamField>
              </Tab>

              <Tab title="encrypted">
                <ParamField body="type" type="string" required>
                  Value: `reasoning.encrypted`.
                </ParamField>

                <ParamField body="data" type="string" required>
                  Encrypted reasoning data.
                </ParamField>

                <ParamField body="id" type="string | null">
                  Reasoning detail ID.
                </ParamField>

                <ParamField body="format" type="string | null">
                  Format identifier.
                </ParamField>
              </Tab>

              <Tab title="text">
                <ParamField body="type" type="string" required>
                  Value: `reasoning.text`.
                </ParamField>

                <ParamField body="text" type="string | null">
                  The reasoning text content.
                </ParamField>

                <ParamField body="signature" type="string | null">
                  Signature for verification.
                </ParamField>

                <ParamField body="id" type="string | null">
                  Reasoning detail ID.
                </ParamField>

                <ParamField body="format" type="string | null">
                  Format identifier.
                </ParamField>
              </Tab>
            </Tabs>
          </Expandable>
        </ParamField>

        <ParamField body="images" type="object[]">
          Generated images from image generation models. Each item has `image_url.url`.
        </ParamField>

        <ParamField body="audio" type="object">
          Audio output data.

          <Expandable title="properties">
            <ParamField body="id" type="string">
              Audio output identifier.
            </ParamField>

            <ParamField body="data" type="string">
              Base64 encoded audio data.
            </ParamField>

            <ParamField body="transcript" type="string">
              Audio transcript.
            </ParamField>

            <ParamField body="expires_at" type="number">
              Audio expiration timestamp.
            </ParamField>
          </Expandable>
        </ParamField>
      </Tab>

      <Tab title="ToolResponseMessage">
        <ParamField body="role" type="string" required>
          Value: `tool`.
        </ParamField>

        <ParamField body="content" type="string | object[]" required>
          Tool response content. Can be a string or array of content parts (text, image, audio, video, file).
        </ParamField>

        <ParamField body="tool_call_id" type="string" required>
          ID of the assistant message tool call this message responds to.
        </ParamField>
      </Tab>
    </Tabs>
  </Expandable>
</ParamField>

<ParamField body="model" type="string" required>
  The model to use for the completion. Browse available models at [sunra.ai/models](https://sunra.ai/models).
</ParamField>

<ParamField body="provider" type="object">
  Optional provider routing preferences. Omit for automatic routing. See [Provider routing](/llm/provider-routing) for supported fields and provider discovery.
</ParamField>

<ParamField body="stream" type="boolean" default={false}>
  If set to `true`, partial message deltas will be sent as server-sent events (SSE). Streams are subject to an idle timeout and a lifetime ceiling — see [Output limits and stream lifetime](/llm/limits#stream-lifetime).
</ParamField>

<ParamField body="max_completion_tokens" type="number | null">
  Maximum tokens in completion. Replaces `max_tokens` as the preferred parameter. On providers that do not honor this field the gateway translates it into `max_tokens`; when both are set, the smaller one applies. See [Output limits](/llm/limits).
</ParamField>

<ParamField body="max_tokens" type="number | null">
  Maximum tokens in completion. Deprecated — use `max_completion_tokens` instead. Note: some providers enforce a minimum of 16. A value above the model's own output ceiling is rejected with `400` rather than reduced.
</ParamField>

<ParamField body="temperature" type="number | null" default={1}>
  Sampling temperature between 0 and 2. Higher values like 0.8 make output more random, lower values like 0.2 make it more focused and deterministic.
</ParamField>

<ParamField body="top_p" type="number | null" default={1}>
  Nucleus sampling parameter (0-1). An alternative to temperature sampling where the model considers the tokens with top\_p probability mass.
</ParamField>

<ParamField body="frequency_penalty" type="number | null" default={0}>
  Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
</ParamField>

<ParamField body="presence_penalty" type="number | null" default={0}>
  Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
</ParamField>

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

<ParamField body="n" type="integer" default={1}>
  How many chat completion choices to generate for each input message.
</ParamField>

<ParamField body="logprobs" type="boolean | null" default={false}>
  Whether to return log probabilities of the output tokens.
</ParamField>

<ParamField body="top_logprobs" type="number | null">
  An integer between 0 and 20 specifying the number of most likely tokens to return at each token position. `logprobs` must be set to `true` if this parameter is used.
</ParamField>

<ParamField body="logit_bias" type="object | null">
  Token logit bias adjustments. Modify the likelihood of specified tokens appearing in the completion. Maps token IDs to bias values from -100 to 100.
</ParamField>

<ParamField body="reasoning" type="object">
  Configuration options for reasoning models.

  <Expandable title="properties">
    <ParamField body="effort" type="string | null">
      Constrains effort on reasoning. Supported values: `xhigh`, `high`, `medium`, `low`, `minimal`, `none`.
    </ParamField>

    <ParamField body="summary" type="string">
      Controls reasoning summary verbosity. Supported values: `auto`, `concise`, `detailed`.
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="response_format" type="object">
  An object specifying the format that the model must output.

  <Expandable title="format types">
    <Tabs>
      <Tab title="text">
        <ParamField body="type" type="string" required>
          Value: `text`.
        </ParamField>
      </Tab>

      <Tab title="json_object">
        <ParamField body="type" type="string" required>
          Value: `json_object`.
        </ParamField>
      </Tab>

      <Tab title="json_schema">
        <ParamField body="type" type="string" required>
          Value: `json_schema`.
        </ParamField>

        <ParamField body="json_schema" type="object" required>
          JSON Schema configuration.

          <Expandable title="properties">
            <ParamField body="name" type="string" required>
              Schema name (a-z, A-Z, 0-9, underscores, dashes, max 64 chars).
            </ParamField>

            <ParamField body="description" type="string">
              Schema description for the model.
            </ParamField>

            <ParamField body="schema" type="object">
              JSON Schema object.
            </ParamField>

            <ParamField body="strict" type="boolean | null">
              Enable strict schema adherence.
            </ParamField>
          </Expandable>
        </ParamField>
      </Tab>

      <Tab title="grammar">
        <ParamField body="type" type="string" required>
          Value: `grammar`.
        </ParamField>

        <ParamField body="grammar" type="string" required>
          Custom grammar for text generation.
        </ParamField>
      </Tab>
    </Tabs>
  </Expandable>
</ParamField>

<ParamField body="seed" type="integer | null">
  If specified, the system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.
</ParamField>

<ParamField body="tools" type="object[]">
  A list of tools the model may call.

  <Expandable title="properties">
    <ParamField body="type" type="string" required>
      The type of the tool. Value: `function`.
    </ParamField>

    <ParamField body="function" type="object" required>
      The function definition.

      <Expandable title="properties">
        <ParamField body="name" type="string" required>
          Function name (a-z, A-Z, 0-9, underscores, dashes, max 64 chars).
        </ParamField>

        <ParamField body="description" type="string">
          A description of what the function does.
        </ParamField>

        <ParamField body="parameters" type="object">
          The parameters the function accepts, described as a JSON Schema object.
        </ParamField>

        <ParamField body="strict" type="boolean | null">
          Whether to enable strict schema adherence.
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField body="cache_control" type="object">
      Cache control for this tool definition.

      <Expandable title="properties">
        <ParamField body="type" type="string" required>
          Value: `ephemeral`.
        </ParamField>

        <ParamField body="ttl" type="string">
          Cache TTL. Supported values: `5m`, `1h`.
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="tool_choice" type="string | object">
  Controls which (if any) tool is called by the model. `none` means no tool calls. `auto` means the model decides. `required` means the model must call a tool. Can also specify a particular function.

  <Expandable title="object variant">
    <ParamField body="type" type="string" required>
      Value: `function`.
    </ParamField>

    <ParamField body="function" type="object" required>
      <Expandable title="properties">
        <ParamField body="name" type="string" required>
          The name of the function to call.
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="parallel_tool_calls" type="boolean | null" default={true}>
  Whether to enable parallel function calling during tool use.
</ParamField>

<ParamField body="modalities" type="string[]">
  Output modalities for the response. Supported values: `text`, `image`, `audio`.
</ParamField>

<ParamField body="metadata" type="object">
  Key-value pairs for additional object information. Maximum 16 pairs, 64 character keys, 512 character values.
</ParamField>

<ParamField body="cache_control" type="object">
  Enable automatic prompt caching. When set, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models.

  <Expandable title="properties">
    <ParamField body="type" type="string" required>
      Value: `ephemeral`.
    </ParamField>

    <ParamField body="ttl" type="string">
      Cache TTL. Supported values: `5m`, `1h`.
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="user" type="string">
  A unique identifier representing your end-user, which can help monitor and detect abuse.
</ParamField>

## Response

Successful chat completion response.

<ResponseField name="id" type="string">
  A unique identifier for the chat completion.
</ResponseField>

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

<ResponseField name="created" type="number">
  The Unix timestamp (in seconds) of when the chat completion was created.
</ResponseField>

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

<ResponseField name="choices" type="object[]">
  A list of chat completion choices. Can be more than one if `n` is greater than 1.

  <Expandable title="properties">
    <ResponseField name="index" type="number">
      The index of the choice in the list of choices.
    </ResponseField>

    <ResponseField name="finish_reason" type="string">
      The reason the model stopped generating tokens. Possible values: `stop`, `length`, `tool_calls`, `content_filter`, `error`.
    </ResponseField>

    <ResponseField name="message" type="object">
      A chat completion message generated by the model.

      <Expandable title="properties">
        <ResponseField name="role" type="string">
          Always `assistant`.
        </ResponseField>

        <ResponseField name="content" type="string | null">
          The text contents of the message. Null when tool\_calls are present.
        </ResponseField>

        <ResponseField name="tool_calls" type="object[]">
          The tool calls generated by the model.

          <Expandable title="properties">
            <ResponseField name="id" type="string">
              The ID of the tool call.
            </ResponseField>

            <ResponseField name="type" type="string">
              Value: `function`.
            </ResponseField>

            <ResponseField name="function" type="object">
              The function that the model called.

              <Expandable title="properties">
                <ResponseField name="name" type="string">
                  The name of the function to call.
                </ResponseField>

                <ResponseField name="arguments" type="string">
                  The arguments in JSON string format.
                </ResponseField>
              </Expandable>
            </ResponseField>
          </Expandable>
        </ResponseField>

        <ResponseField name="refusal" type="string | null">
          Refusal message if the content was refused.
        </ResponseField>

        <ResponseField name="reasoning" type="string | null">
          Reasoning output text.
        </ResponseField>

        <ResponseField name="reasoning_details" type="object[]">
          Detailed reasoning information for extended thinking models.

          <Expandable title="reasoning detail types">
            <Tabs>
              <Tab title="summary">
                <ResponseField name="type" type="string">
                  Value: `reasoning.summary`.
                </ResponseField>

                <ResponseField name="summary" type="string">
                  The reasoning summary text.
                </ResponseField>

                <ResponseField name="id" type="string | null">
                  Detail ID.
                </ResponseField>

                <ResponseField name="format" type="string | null">
                  Format identifier.
                </ResponseField>
              </Tab>

              <Tab title="encrypted">
                <ResponseField name="type" type="string">
                  Value: `reasoning.encrypted`.
                </ResponseField>

                <ResponseField name="data" type="string">
                  Encrypted reasoning data.
                </ResponseField>

                <ResponseField name="id" type="string | null">
                  Detail ID.
                </ResponseField>

                <ResponseField name="format" type="string | null">
                  Format identifier.
                </ResponseField>
              </Tab>

              <Tab title="text">
                <ResponseField name="type" type="string">
                  Value: `reasoning.text`.
                </ResponseField>

                <ResponseField name="text" type="string | null">
                  The reasoning text.
                </ResponseField>

                <ResponseField name="signature" type="string | null">
                  Verification signature.
                </ResponseField>

                <ResponseField name="id" type="string | null">
                  Detail ID.
                </ResponseField>

                <ResponseField name="format" type="string | null">
                  Format identifier.
                </ResponseField>
              </Tab>
            </Tabs>
          </Expandable>
        </ResponseField>

        <ResponseField name="images" type="object[]">
          Generated images. Each item contains `image_url.url`.
        </ResponseField>

        <ResponseField name="audio" type="object">
          Audio output data.

          <Expandable title="properties">
            <ResponseField name="id" type="string">
              Audio output identifier.
            </ResponseField>

            <ResponseField name="data" type="string">
              Base64 encoded audio data.
            </ResponseField>

            <ResponseField name="transcript" type="string">
              Audio transcript.
            </ResponseField>

            <ResponseField name="expires_at" type="number">
              Audio expiration timestamp.
            </ResponseField>
          </Expandable>
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="logprobs" type="object | null">
      Log probability information for the choice.

      <Expandable title="properties">
        <ResponseField name="content" type="object[] | null">
          Log probabilities for content tokens. Each item contains `token`, `logprob`, `bytes`, and `top_logprobs`.
        </ResponseField>

        <ResponseField name="refusal" type="object[] | null">
          Log probabilities for refusal tokens.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  Usage statistics for the completion request.

  <Expandable title="properties">
    <ResponseField name="prompt_tokens" type="number">
      Number of tokens in the prompt.
    </ResponseField>

    <ResponseField name="completion_tokens" type="number">
      Number of tokens in the generated completion.
    </ResponseField>

    <ResponseField name="total_tokens" type="number">
      Total number of tokens used in the request (prompt + completion).
    </ResponseField>

    <ResponseField name="prompt_tokens_details" type="object | null">
      Detailed prompt token usage.

      <Expandable title="properties">
        <ResponseField name="cached_tokens" type="number">
          Number of cached prompt tokens.
        </ResponseField>

        <ResponseField name="cache_write_tokens" type="number">
          Tokens written to cache. Only returned for models with explicit caching.
        </ResponseField>

        <ResponseField name="audio_tokens" type="number">
          Audio input tokens.
        </ResponseField>

        <ResponseField name="video_tokens" type="number">
          Video input tokens.
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="completion_tokens_details" type="object | null">
      Detailed completion token usage.

      <Expandable title="properties">
        <ResponseField name="reasoning_tokens" type="number | null">
          Tokens used for reasoning.
        </ResponseField>

        <ResponseField name="audio_tokens" type="number | null">
          Tokens used for audio output.
        </ResponseField>

        <ResponseField name="accepted_prediction_tokens" type="number | null">
          Accepted prediction tokens.
        </ResponseField>

        <ResponseField name="rejected_prediction_tokens" type="number | null">
          Rejected prediction tokens.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="system_fingerprint" type="string | null">
  This fingerprint represents the backend configuration that the model runs with. Can be used with the `seed` parameter to understand when backend changes have been made.
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api-llm.sunra.ai/v1/chat/completions \
    -H "Authorization: Bearer <SUNRA_KEY>" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "google/gemini-2.5-flash",
      "messages": [
        {
          "role": "system",
          "content": "You are a helpful assistant."
        },
        {
          "role": "user",
          "content": "What is the capital of France?"
        }
      ]
    }'
  ```

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

  response = requests.post(
      "https://api-llm.sunra.ai/v1/chat/completions",
      headers={
          "Authorization": "Bearer <SUNRA_KEY>",
          "Content-Type": "application/json"
      },
      json={
          "model": "google/gemini-2.5-flash",
          "messages": [
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "What is the capital of France?"}
          ]
      }
  )
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api-llm.sunra.ai/v1/chat/completions", {
    method: "POST",
    headers: {
      "Authorization": "Bearer <SUNRA_KEY>",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "google/gemini-2.5-flash",
      messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "What is the capital of France?" }
      ]
    })
  });
  const data = await response.json();
  console.log(data);
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "chatcmpl-abc123",
    "object": "chat.completion",
    "created": 1677652288,
    "model": "google/gemini-2.5-flash",
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "The capital of France is Paris.",
          "refusal": null
        },
        "finish_reason": "stop",
        "logprobs": null
      }
    ],
    "system_fingerprint": "fp_44709d6fcb",
    "usage": {
      "prompt_tokens": 25,
      "completion_tokens": 8,
      "total_tokens": 33,
      "prompt_tokens_details": null,
      "completion_tokens_details": null
    }
  }
  ```
</ResponseExample>
