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

# Create a chat completion

> Use CometAPI POST /v1/chat/completions to send multi-message conversations to chat models with streaming, temperature, and max_tokens controls.

CometAPI routes Chat Completions to multiple providers — including OpenAI, Claude, and Gemini — through a single OpenAI-compatible interface. Switch between models by changing the `model` parameter; most OpenAI-compatible SDKs work by setting `base_url` to `https://api.cometapi.com/v1`.

<Tip>
  For a copyable first request, start with the [Chat Completions API quickstart](/quickstarts/text/chat-completions-api).
</Tip>

<Warning>
  Request parameters and response fields can vary significantly between model providers. Check the official documentation for the provider behind the model you use whenever you need the complete parameter list or provider-specific behavior. For example, `reasoning_effort` only applies to reasoning models (o-series, GPT-5.1+), and some models do not support `logprobs` or `n` > 1.
</Warning>

<Note>
  For OpenAI Pro models, o-series reasoning models, and Codex models, use the [Responses](/api/text/responses) endpoint instead. These model families have more complete support on the Responses API.
</Note>

***

## Message roles

| Role        | Description                                                                                                       |
| ----------- | ----------------------------------------------------------------------------------------------------------------- |
| `system`    | Sets the assistant's behavior and personality. Placed at the start of the conversation.                           |
| `developer` | Replaces `system` for newer models (o1+). Provides instructions the model should follow regardless of user input. |
| `user`      | Messages from the end user.                                                                                       |
| `assistant` | Previous model responses, used to maintain conversation history.                                                  |
| `tool`      | Results from tool/function calls. Must include `tool_call_id` matching the original tool call.                    |

<Tip>
  For newer models (GPT-4.1, GPT-5 series, o-series), prefer `developer` over `system` for instruction messages. Both work, but `developer` provides stronger instruction-following behavior.
</Tip>

***

## Send multimodal input

Many models support images and audio alongside text. To send multimodal messages, use the array format for `content`:

```json theme={null}
{
  "role": "user",
  "content": [
    {"type": "text", "text": "Describe this image"},
    {
      "type": "image_url",
      "image_url": {
        "url": "https://example.com/image.png",
        "detail": "high"
      }
    }
  ]
}
```

The `detail` parameter controls image analysis depth:

* `low` — faster, uses fewer tokens (fixed cost)
* `high` — detailed analysis, more tokens consumed
* `auto` — the model decides (default)

***

## Stream responses

To receive incremental output, set `stream` to `true`. The response is delivered as **Server-Sent Events (SSE)**, where each event contains a `chat.completion.chunk` object:

```
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}

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

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
```

<Tip>
  To include token usage statistics in streaming responses, set `stream_options.include_usage` to `true`. The usage data appears in the final chunk before `[DONE]`.
</Tip>

***

## Request structured output

To force the model to return valid JSON matching a specific schema, use `response_format`:

<CodeGroup>
  ```json JSON Schema Mode theme={null}
  {
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "result",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "answer": {"type": "string"},
            "confidence": {"type": "number"}
          },
          "required": ["answer", "confidence"],
          "additionalProperties": false
        }
      }
    }
  }
  ```

  ```json JSON Object Mode theme={null}
  {
    "response_format": {"type": "json_object"}
  }
  ```
</CodeGroup>

<Note>
  JSON Schema mode (`json_schema`) guarantees the output matches your schema exactly. JSON Object mode (`json_object`) only guarantees valid JSON — the structure is not enforced.
</Note>

***

## Call tools and functions

To enable the model to call external functions, provide tool definitions:

```json theme={null}
{
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get current weather for a city",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {"type": "string", "description": "City name"}
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}
```

When the model decides to call a tool, the response will have `finish_reason: "tool_calls"` and the `message.tool_calls` array will contain the function name and arguments. You then execute the function and send the result back as a `tool` message with the matching `tool_call_id`.

***

## Cross-provider notes

<AccordionGroup>
  <Accordion title="Parameter support across providers">
    | Parameter          | OpenAI GPT         | Claude (via compat) | Gemini (via compat)                  |
    | ------------------ | ------------------ | ------------------- | ------------------------------------ |
    | `temperature`      | 0–2                | 0–1                 | 0–2                                  |
    | `top_p`            | 0–1                | 0–1                 | 0–1                                  |
    | `n`                | 1–128              | 1 only              | 1–8                                  |
    | `stop`             | Up to 4            | Up to 4             | Up to 5                              |
    | `tools`            | ✅                  | ✅                   | ✅                                    |
    | `response_format`  | ✅                  | ✅ (json\_schema)    | ✅                                    |
    | `logprobs`         | ✅                  | ❌                   | ❌                                    |
    | `reasoning_effort` | o-series, GPT-5.1+ | ❌                   | ❌ (use `thinking` for Gemini native) |
  </Accordion>

  <Accordion title="max_tokens vs max_completion_tokens">
    * **`max_tokens`** — The legacy parameter. Works with most models but is deprecated for newer OpenAI models.
    * **`max_completion_tokens`** — The recommended parameter for GPT-4.1, GPT-5 series, and o-series models. Required for reasoning models as it includes both output tokens and reasoning tokens.

    CometAPI automatically handles the mapping when routing to different providers.
  </Accordion>

  <Accordion title="system vs developer role">
    * **`system`** — The traditional instruction role. Works with all models.
    * **`developer`** — Introduced with o1 models. Provides stronger instruction-following for newer models. Falls back to `system` behavior on older models.

    Use `developer` for new projects targeting GPT-4.1+ or o-series models.
  </Accordion>
</AccordionGroup>

***

## FAQ

### How to handle rate limits?

When encountering `429 Too Many Requests`, implement exponential backoff:

```python theme={null}
import os
import time
import random
from openai import OpenAI, RateLimitError

client = OpenAI(
    base_url="https://api.cometapi.com/v1",
    api_key=os.environ["COMETAPI_KEY"],
)

def chat_with_retry(messages, max_retries=3):
    for i in range(max_retries):
        try:
            return client.chat.completions.create(
                model="gpt-5.4",
                messages=messages,
            )
        except RateLimitError:
            if i < max_retries - 1:
                wait_time = (2 ** i) + random.random()
                time.sleep(wait_time)
            else:
                raise
```

### How to maintain conversation context?

Include the full conversation history in the `messages` array:

```python theme={null}
messages = [
    {"role": "developer", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is Python?"},
    {"role": "assistant", "content": "Python is a high-level programming language..."},
    {"role": "user", "content": "What are its main advantages?"},
]
```

### What does `finish_reason` mean?

| Value            | Meaning                                                |
| ---------------- | ------------------------------------------------------ |
| `stop`           | Natural completion or hit a stop sequence.             |
| `length`         | Reached `max_tokens` or `max_completion_tokens` limit. |
| `tool_calls`     | The model invoked one or more tool/function calls.     |
| `content_filter` | Output was filtered due to content policy.             |

### How to control costs?

1. Use `max_completion_tokens` to cap output length.
2. Choose cost-effective models (e.g., `gpt-5.4-mini` or `gpt-5.4-nano` for simpler tasks).
3. Keep prompts concise — avoid redundant context.
4. Monitor token usage in the `usage` response field.


## OpenAPI

````yaml api/openapi/text/post-chat.openapi.json POST /v1/chat/completions
openapi: 3.1.0
info:
  title: Chat Completions API
  version: 1.0.0
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /v1/chat/completions:
    post:
      summary: Chat Completions
      description: >-
        Routes chat requests across multiple model providers. Request parameters
        and response fields can vary significantly by provider, so check the
        official documentation for the provider behind the model you use when
        you need provider-specific parameters or behavior details.
      operationId: chat_completions
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - model
                - messages
              properties:
                model:
                  type: string
                  description: >-
                    Model ID to use for this request. See the [Models
                    page](/overview/models) for current options.
                  example: gpt-4.1
                  default: gpt-5.4
                messages:
                  type: array
                  description: >-
                    A list of messages forming the conversation. Each message
                    has a `role` (`system`, `user`, `assistant`, or `developer`)
                    and `content` (text string or multimodal content array).
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                        enum:
                          - system
                          - user
                          - assistant
                          - tool
                          - developer
                        description: >-
                          The role of the message author. Common values:
                          `system` (recommended for system-level instructions),
                          `user`, `assistant`, `tool`. Newer OpenAI models may
                          also accept `developer` instead of `system`.
                      content:
                        type: string
                        description: >-
                          The message content. Can be a text string or an array
                          of content objects for multimodal input (text +
                          images).
                  default:
                    - role: system
                      content: You are a helpful assistant.
                    - role: user
                      content: Hello!
                stream:
                  type: boolean
                  description: >-
                    If `true`, partial response tokens are delivered
                    incrementally via server-sent events (SSE). The stream ends
                    with a `data: [DONE]` message.
                temperature:
                  type: number
                  description: >-
                    Sampling temperature between 0 and 2. Higher values (e.g.,
                    0.8) produce more random output; lower values (e.g., 0.2)
                    make output more focused and deterministic. Recommended to
                    adjust this or `top_p`, but not both.
                  minimum: 0
                  maximum: 2
                  default: 1
                top_p:
                  type: number
                  description: >-
                    Nucleus sampling parameter. The model considers only the
                    tokens whose cumulative probability reaches `top_p`. For
                    example, 0.1 means only the top 10% probability tokens are
                    considered. Recommended to adjust this or `temperature`, but
                    not both.
                  minimum: 0
                  maximum: 1
                  default: 1
                'n':
                  type: integer
                  description: >-
                    Number of completion choices to generate for each input
                    message. Defaults to 1.
                  default: 1
                stop:
                  type: string
                  description: >-
                    Up to 4 sequences where the API will stop generating further
                    tokens. Can be a string or an array of strings.
                max_tokens:
                  type: integer
                  description: >-
                    Maximum number of tokens to generate in the completion. The
                    total of input + output tokens is capped by the model's
                    context length.
                presence_penalty:
                  type: number
                  description: >-
                    Number between -2.0 and 2.0. Positive values penalize tokens
                    based on whether they have already appeared, encouraging the
                    model to explore new topics.
                  minimum: -2
                  maximum: 2
                  default: 0
                frequency_penalty:
                  type: number
                  description: >-
                    Number between -2.0 and 2.0. Positive values penalize tokens
                    proportionally to how often they have appeared, reducing
                    verbatim repetition.
                  minimum: -2
                  maximum: 2
                  default: 0
                logit_bias:
                  type: object
                  description: >-
                    A JSON object mapping token IDs to bias values from -100 to
                    100. The bias is added to the model's logits before
                    sampling. Values between -1 and 1 subtly adjust likelihood;
                    -100 or 100 effectively ban or force selection of a token.
                user:
                  type: string
                  description: >-
                    A unique identifier for your end-user. Helps with abuse
                    detection and monitoring.
                max_completion_tokens:
                  type: integer
                  description: >-
                    An upper bound for the number of tokens to generate,
                    including visible output tokens and reasoning tokens. Use
                    this instead of `max_tokens` for GPT-4.1+, GPT-5 series, and
                    o-series models.
                response_format:
                  type: object
                  description: >-
                    Specifies the output format. Use `{"type": "json_object"}`
                    for JSON mode, or `{"type": "json_schema", "json_schema":
                    {...}}` for strict structured output.
                  properties:
                    type:
                      type: string
                      enum:
                        - text
                        - json_object
                        - json_schema
                      description: >-
                        Output format type: `text` (default), `json_object`, or
                        `json_schema`.
                    json_schema:
                      type: object
                      description: The JSON Schema definition.
                tools:
                  type: array
                  description: >-
                    A list of tools the model may call. Currently supports
                    `function` type tools.
                  items:
                    type: object
                    properties:
                      type:
                        type: string
                        enum:
                          - function
                        description: Tool type. Use `function`.
                      function:
                        type: object
                        properties:
                          name:
                            type: string
                            description: >-
                              Function name. The model repeats it inside
                              `tool_calls` when it calls the tool.
                          description:
                            type: string
                            description: >-
                              What the function does. The model uses this text
                              to decide when to call it.
                          parameters:
                            type: object
                            description: >-
                              JSON Schema object that describes the function
                              arguments.
                          strict:
                            type: boolean
                            description: >-
                              If `true`, the model must produce arguments that
                              exactly match the JSON Schema.
                        description: The function definition the model may call.
                tool_choice:
                  type:
                    - string
                    - object
                  description: >-
                    Controls how the model selects tools. `auto` (default):
                    model decides. `none`: no tools. `required`: must call a
                    tool.
                  default: auto
                logprobs:
                  type: boolean
                  description: Whether to return log probabilities of the output tokens.
                  default: false
                top_logprobs:
                  type: integer
                  description: >-
                    Number of most likely tokens to return at each position
                    (0-20). Requires `logprobs` to be `true`.
                  minimum: 0
                  maximum: 20
                reasoning_effort:
                  type: string
                  description: >-
                    Controls the reasoning effort for o-series and GPT-5.1+
                    models.
                  enum:
                    - low
                    - medium
                    - high
                stream_options:
                  type: object
                  description: Options for streaming. Only valid when `stream` is `true`.
                  properties:
                    include_usage:
                      type: boolean
                      description: >-
                        If true, includes usage stats in the final streaming
                        chunk.
                service_tier:
                  type: string
                  description: Specifies the processing tier.
                  enum:
                    - auto
                    - default
                    - flex
                    - priority
              default:
                model: gpt-5.4
                messages:
                  - role: system
                    content: You are a helpful assistant.
                  - role: user
                    content: Hello!
            examples:
              Default:
                summary: Default
                value:
                  model: gpt-5.4
                  messages:
                    - role: system
                      content: You are a helpful assistant.
                    - role: user
                      content: Hello!
              Image Input:
                summary: Image Input
                value:
                  model: gpt-4.1
                  messages:
                    - role: user
                      content:
                        - type: text
                          text: What is in this image?
                        - type: image_url
                          image_url:
                            url: https://picsum.photos/1920/1080
                  max_tokens: 300
              Streaming:
                summary: Streaming
                value:
                  model: gpt-5.4
                  messages:
                    - role: system
                      content: You are a helpful assistant.
                    - role: user
                      content: Hello!
                  stream: true
              Function Calling:
                summary: Function Calling
                value:
                  model: gpt-5.4
                  messages:
                    - role: user
                      content: What is the weather like in Boston today?
                  tools:
                    - type: function
                      function:
                        name: get_current_weather
                        description: Get the current weather in a given location
                        parameters:
                          type: object
                          properties:
                            location:
                              type: string
                              description: The city and state, e.g. San Francisco, CA
                            unit:
                              type: string
                              enum:
                                - celsius
                                - fahrenheit
                              description: Temperature unit.
                          required:
                            - location
                  tool_choice: auto
      responses:
        '200':
          description: Successful chat completion response.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                    description: Unique completion identifier.
                    example: chatcmpl-abc123
                  object:
                    type: string
                    enum:
                      - chat.completion
                    example: chat.completion
                    description: >-
                      Object type. Non-streaming responses use
                      `chat.completion`.
                  created:
                    type: integer
                    description: Unix timestamp of creation.
                    example: 1774412483
                  model:
                    type: string
                    description: The model used (may include version suffix).
                    example: gpt-5.4-2026-03-05
                  choices:
                    type: array
                    description: Array of completion choices.
                    items:
                      type: object
                      properties:
                        index:
                          type: integer
                          description: Index of this choice in the `choices` array.
                        message:
                          type: object
                          properties:
                            role:
                              type: string
                              enum:
                                - assistant
                              description: Role of the generated message, `assistant`.
                            content:
                              type:
                                - string
                                - 'null'
                              description: >-
                                The generated text. null when the model calls
                                tools.
                            refusal:
                              type:
                                - string
                                - 'null'
                              description: Refusal message if the model refused.
                            tool_calls:
                              type: array
                              description: Tool calls the model wants to make.
                              items:
                                type: object
                                properties:
                                  id:
                                    type: string
                                    description: >-
                                      Unique ID of the tool call. Send it back
                                      as `tool_call_id` with the tool result
                                      message.
                                  type:
                                    type: string
                                    enum:
                                      - function
                                    description: Tool call type, `function`.
                                  function:
                                    type: object
                                    properties:
                                      name:
                                        type: string
                                        description: Name of the function to call.
                                      arguments:
                                        type: string
                                        description: >-
                                          Function arguments as a JSON-encoded
                                          string. Parse before use; the model can
                                          produce invalid JSON.
                                    description: The function the model wants to call.
                            annotations:
                              type: array
                              items:
                                type: object
                              description: >-
                                Annotations attached to the message content,
                                such as URL citations, when the provider returns
                                them.
                          description: The assistant message generated by the model.
                        logprobs:
                          type:
                            - object
                            - 'null'
                          description: >-
                            Log probability details when the request sets
                            `logprobs`; otherwise `null`.
                        finish_reason:
                          type: string
                          enum:
                            - stop
                            - length
                            - tool_calls
                            - content_filter
                          description: >-
                            Why generation stopped: `stop`, `length`,
                            `tool_calls`, or `content_filter`.
                  usage:
                    type: object
                    properties:
                      prompt_tokens:
                        type: integer
                        example: 29
                        description: Tokens in the input messages.
                      completion_tokens:
                        type: integer
                        example: 2
                        description: >-
                          Tokens generated in the completion, including
                          reasoning tokens for reasoning models.
                      total_tokens:
                        type: integer
                        example: 31
                        description: Sum of prompt and completion tokens.
                      prompt_tokens_details:
                        type: object
                        properties:
                          cached_tokens:
                            type: integer
                            example: 0
                            description: >-
                              Prompt tokens served from the provider prompt
                              cache.
                          audio_tokens:
                            type: integer
                            example: 0
                            description: Prompt tokens that came from audio input.
                        description: Breakdown of prompt token sources.
                      completion_tokens_details:
                        type: object
                        properties:
                          reasoning_tokens:
                            type: integer
                            example: 0
                            description: >-
                              Tokens the model spent on internal reasoning.
                              Billed as output tokens.
                          audio_tokens:
                            type: integer
                            example: 0
                            description: Completion tokens used for audio output.
                          accepted_prediction_tokens:
                            type: integer
                            example: 0
                            description: >-
                              Predicted-output tokens that matched the final
                              output and were accepted.
                          rejected_prediction_tokens:
                            type: integer
                            example: 0
                            description: >-
                              Predicted-output tokens that did not match the
                              final output and were discarded.
                        description: Breakdown of completion token usage.
                    description: >-
                      Token accounting for this request. Billing uses these
                      counts.
                  service_tier:
                    type: string
                    example: default
                    description: >-
                      Service tier that processed the request, when the provider
                      reports one.
                  system_fingerprint:
                    type:
                      - string
                      - 'null'
                    example: fp_490a4ad033
                    description: >-
                      Provider backend configuration fingerprint, when the
                      provider reports one.
              example:
                id: chatcmpl-DNA27oKtBUL8TmbGpBM3B3zhWgYfZ
                object: chat.completion
                created: 1774412483
                model: gpt-4.1-nano-2025-04-14
                choices:
                  - index: 0
                    message:
                      role: assistant
                      content: Four
                      refusal: null
                      annotations: []
                    logprobs: null
                    finish_reason: stop
                usage:
                  prompt_tokens: 29
                  completion_tokens: 2
                  total_tokens: 31
                  prompt_tokens_details:
                    cached_tokens: 0
                    audio_tokens: 0
                  completion_tokens_details:
                    reasoning_tokens: 0
                    audio_tokens: 0
                    accepted_prediction_tokens: 0
                    rejected_prediction_tokens: 0
                service_tier: default
                system_fingerprint: fp_490a4ad033
        '400':
          description: >-
            Request validation failed before the request could be processed
            normally.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: ''
                  message: 'model name is required (request id: <request_id>)'
                  type: comet_api_error
        '401':
          description: API key is missing, malformed, or invalid.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  code: ''
                  message: 'invalid token (request id: <request_id>)'
                  type: comet_api_error
        '500':
          description: >-
            Internal failure or a request-shape error surfaced as a
            server-status response.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error:
                  message: 'field messages is required (request id: <request_id>)'
                  type: comet_api_error
                  param: ''
                  code: invalid_request
      x-codeSamples:
        - lang: Python
          label: Default
          source: |
            import os
            from openai import OpenAI
            client = OpenAI(
                base_url="https://api.cometapi.com/v1",
                api_key=os.environ["COMETAPI_KEY"],
            )

            completion = client.chat.completions.create(
                model="gpt-5.4",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": "Hello!"},
                ],
            )

            print(completion.choices[0].message)
        - lang: Python
          label: Image Input
          source: |
            import os
            from openai import OpenAI
            client = OpenAI(
                base_url="https://api.cometapi.com/v1",
                api_key=os.environ["COMETAPI_KEY"],
            )

            completion = client.chat.completions.create(
                model="gpt-4.1",
                messages=[
                    {
                        "role": "user",
                        "content": [
                            {"type": "text", "text": "What is in this image?"},
                            {
                                "type": "image_url",
                                "image_url": {"url": "https://picsum.photos/1920/1080"},
                            },
                        ],
                    }
                ],
                max_tokens=300,
            )

            print(completion.choices[0].message)
        - lang: Python
          label: Streaming
          source: |
            import os
            from openai import OpenAI
            client = OpenAI(
                base_url="https://api.cometapi.com/v1",
                api_key=os.environ["COMETAPI_KEY"],
            )

            stream = client.chat.completions.create(
                model="gpt-5.4",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": "Hello!"},
                ],
                stream=True,
            )

            for chunk in stream:
                if chunk.choices[0].delta.content is not None:
                    print(chunk.choices[0].delta.content, end="")
        - lang: Python
          label: Functions
          source: |
            import os
            from openai import OpenAI

            client = OpenAI(
                base_url="https://api.cometapi.com/v1",
                api_key=os.environ["COMETAPI_KEY"],
            )

            completion = client.chat.completions.create(
                model="gpt-4.1",
                messages=[
                    {"role": "user", "content": "What is the weather like in Boston today?"}
                ],
                tools=[
                    {
                        "type": "function",
                        "function": {
                            "name": "get_current_weather",
                            "description": "Get the current weather in a given location",
                            "parameters": {
                                "type": "object",
                                "properties": {
                                    "location": {
                                        "type": "string",
                                        "description": "The city and state, e.g. San Francisco, CA",
                                    },
                                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                                },
                                "required": ["location"],
                            },
                        },
                    }
                ],
                tool_choice="auto",
            )

            print(completion.choices[0].message)
        - lang: Python
          label: Logprobs
          source: |
            import os
            from openai import OpenAI

            client = OpenAI(
                base_url="https://api.cometapi.com/v1",
                api_key=os.environ["COMETAPI_KEY"],
            )

            completion = client.chat.completions.create(
                model="gpt-5.4",
                messages=[
                    {"role": "user", "content": "Hello!"}
                ],
                logprobs=True,
                top_logprobs=2,
            )

            print(completion.choices[0].logprobs)
        - lang: JavaScript
          label: Default
          source: |
            import OpenAI from "openai";

            const client = new OpenAI({
                baseURL: "https://api.cometapi.com/v1",
                apiKey: process.env.COMETAPI_KEY,
            });

            const completion = await client.chat.completions.create({
                model: "gpt-5.4",
                messages: [
                    { role: "system", content: "You are a helpful assistant." },
                    { role: "user", content: "Hello!" },
                ],
            });

            console.log(completion.choices[0].message);
        - lang: JavaScript
          label: Image Input
          source: |
            import OpenAI from "openai";

            const client = new OpenAI({
                baseURL: "https://api.cometapi.com/v1",
                apiKey: process.env.COMETAPI_KEY,
            });

            const completion = await client.chat.completions.create({
                model: "gpt-4.1",
                messages: [
                    {
                        role: "user",
                        content: [
                            { type: "text", text: "What is in this image?" },
                            {
                                type: "image_url",
                                image_url: { url: "https://picsum.photos/1920/1080" },
                            },
                        ],
                    },
                ],
                max_tokens: 300,
            });

            console.log(completion.choices[0].message);
        - lang: JavaScript
          label: Streaming
          source: |
            import OpenAI from "openai";

            const client = new OpenAI({
                baseURL: "https://api.cometapi.com/v1",
                apiKey: process.env.COMETAPI_KEY,
            });

            const stream = await client.chat.completions.create({
                model: "gpt-5.4",
                messages: [
                    { role: "system", content: "You are a helpful assistant." },
                    { role: "user", content: "Hello!" },
                ],
                stream: true,
            });

            for await (const chunk of stream) {
                process.stdout.write(chunk.choices[0]?.delta?.content || "");
            }
        - lang: JavaScript
          label: Functions
          source: |
            import OpenAI from "openai";

            const client = new OpenAI({
                baseURL: "https://api.cometapi.com/v1",
                apiKey: process.env.COMETAPI_KEY,
            });

            const completion = await client.chat.completions.create({
                model: "gpt-4.1",
                messages: [
                    { role: "user", content: "What is the weather like in Boston today?" },
                ],
                tools: [
                    {
                        type: "function",
                        function: {
                            name: "get_current_weather",
                            description: "Get the current weather in a given location",
                            parameters: {
                                type: "object",
                                properties: {
                                    location: { type: "string", description: "The city and state" },
                                    unit: { type: "string", enum: ["celsius", "fahrenheit"] },
                                },
                                required: ["location"],
                            },
                        },
                    },
                ],
                tool_choice: "auto",
            });

            console.log(completion.choices[0].message);
        - lang: JavaScript
          label: Logprobs
          source: |
            import OpenAI from "openai";

            const client = new OpenAI({
                baseURL: "https://api.cometapi.com/v1",
                apiKey: process.env.COMETAPI_KEY,
            });

            const completion = await client.chat.completions.create({
                model: "gpt-5.4",
                messages: [
                    { role: "user", content: "Hello!" },
                ],
                logprobs: true,
                top_logprobs: 2,
            });

            console.log(completion.choices[0].logprobs);
        - lang: Shell
          label: Default
          source: |
            curl https://api.cometapi.com/v1/chat/completions \
              -H "Content-Type: application/json" \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -d '{
                "model": "gpt-5.4",
                "messages": [
                  {"role": "system", "content": "You are a helpful assistant."},
                  {"role": "user", "content": "Hello!"}
                ]
              }'
        - lang: Shell
          label: Image Input
          source: |
            curl https://api.cometapi.com/v1/chat/completions \
              -H "Content-Type: application/json" \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -d '{
                "model": "gpt-4.1",
                "messages": [
                  {
                    "role": "user",
                    "content": [
                      {"type": "text", "text": "What is in this image?"},
                      {"type": "image_url", "image_url": {"url": "https://picsum.photos/1920/1080"}}
                    ]
                  }
                ],
                "max_tokens": 300
              }'
        - lang: Shell
          label: Streaming
          source: |
            curl https://api.cometapi.com/v1/chat/completions \
              -H "Content-Type: application/json" \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -d '{
                "model": "gpt-5.4",
                "messages": [
                  {"role": "system", "content": "You are a helpful assistant."},
                  {"role": "user", "content": "Hello!"}
                ],
                "stream": true
              }'
        - lang: Shell
          label: Functions
          source: |
            curl https://api.cometapi.com/v1/chat/completions \
              -H "Content-Type: application/json" \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -d '{
                "model": "gpt-4.1",
                "messages": [
                  {"role": "user", "content": "What is the weather like in Boston today?"}
                ],
                "tools": [
                  {
                    "type": "function",
                    "function": {
                      "name": "get_current_weather",
                      "description": "Get the current weather in a given location",
                      "parameters": {
                        "type": "object",
                        "properties": {
                          "location": {"type": "string", "description": "The city and state"},
                          "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                        },
                        "required": ["location"]
                      }
                    }
                  }
                ],
                "tool_choice": "auto"
              }'
        - lang: Shell
          label: Logprobs
          source: |
            curl https://api.cometapi.com/v1/chat/completions \
              -H "Content-Type: application/json" \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -d '{
                "model": "gpt-5.4",
                "messages": [
                  {"role": "user", "content": "Hello!"}
                ],
                "logprobs": true,
                "top_logprobs": 2
              }'
components:
  schemas:
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          type: object
          required:
            - message
            - type
          properties:
            message:
              type: string
              description: Human-readable error message. It often includes a request id.
            type:
              type: string
              description: CometAPI error type, such as `comet_api_error`.
            param:
              type:
                - string
                - 'null'
              description: Related parameter when the platform provides one.
            code:
              type:
                - string
                - 'null'
              description: Error code. Request-shape errors may use `invalid_request`.
          description: Error envelope returned for failed requests.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````