---
title: "Create a chat completion (OpenAI-compatible)"
method: POST
path: "/ai/openai/chat/completions"
tags: ["OpenAI Chat"]
---

# Create a chat completion (OpenAI-compatible)

`POST /ai/openai/chat/completions`

Chat with a language model. This endpoint is consistent with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and may be used with the OpenAI JS or Python SDK by setting the base URL to `https://api.telnyx.com/v2/ai/openai`.

## Request body

- ChatCompletionRequest
  - `api_key_ref` string — If you are using an external inference provider like xAI or OpenAI, this field allows you to pass along a reference to your API key. After creating an [integration secret](https://developers.telnyx.com/api-reference/integration-secrets/create-a-secret) for you API key, pass the secret's `identifier` in this field.
  - `best_of` integer — This is used with `use_beam_search` to determine how many candidate beams to explore.
  - `early_stopping` boolean — This is used with `use_beam_search`. If `true`, generation stops as soon as there are `best_of` complete candidates; if `false`, a heuristic is applied and the generation stops when is it very unlikely to find better candidates.
  - `enable_thinking` boolean — Whether to enable the thinking/reasoning phase for models that support it (e.g., QwQ, Qwen3). When set to false, the model will skip the internal reasoning step and respond directly, which can reduce latency. Defaults to true.
  - `frequency_penalty` number — Higher values will penalize the model from repeating the same output tokens.
  - `guided_choice` string[] — If specified, the output will be exactly one of the choices.
  - `guided_json` object — Must be a valid JSON schema. If specified, the output will follow the JSON schema.
  - `guided_regex` string — If specified, the output will follow the regex pattern.
  - `length_penalty` number — This is used with `use_beam_search` to prefer shorter or longer completions.
  - `logprobs` boolean — Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the `content` of `message`.
  - `max_tokens` integer — Maximum number of completion tokens the model should generate.
  - `messages` ChatCompletionSystemMessageParam[], required — A list of the previous chat messages for context.
    - `content` union, required
      - string
      - object[]
        - `image_url` string
        - `text` string
        - `type` 'text' | 'image_url', required
    - `role` 'system' | 'user' | 'assistant' | 'tool', required
  - `min_p` number — This is an alternative to `top_p` that [many prefer](https://github.com/huggingface/transformers/issues/27670). Must be in [0, 1].
  - `model` string — The language model to chat with.
  - `n` number — This will return multiple choices for you instead of a single chat completion.
  - `presence_penalty` number — Higher values will penalize the model from repeating the same output tokens.
  - `response_format` ChatCompletionResponseFormatParam
    - `type` 'text' | 'json_object', required
  - `seed` integer — 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.
  - `service_tier` string — The service tier to use for this request. Supported values vary by model; use `GET /v2/ai/openai/models` and inspect the model's `service_tiers` field. If omitted, Telnyx-hosted models use `default`.
  - `stop` union — Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
    - string
    - string[]
  - `stream` boolean — Whether or not to stream data-only server-sent events as they become available.
  - `temperature` number — Adjusts the "creativity" of the model. Lower values make the model more deterministic and repetitive, while higher values make the model more random and creative.
  - `tool_choice` 'none' | 'auto' | 'required'
  - `tools` union[] — The `function` tool type follows the same schema as the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat). The `retrieval` tool type is unique to Telnyx. You may pass a list of [embedded storage buckets](https://developers.telnyx.com/api-reference/embeddings/embed-documents) for retrieval-augmented generation.
    - union
      - ChatCompletionToolParam
        - `function` FunctionDefinition, required
          - `description` string
          - `name` string, required
          - `parameters` object
        - `type` 'function', required
      - Retrieval
        - `retrieval` BucketIds, required
          - `bucket_ids` string[], required — List of [embedded storage buckets](https://developers.telnyx.com/api-reference/embeddings/embed-documents) to use for retrieval-augmented generation.
          - `max_num_results` integer — The maximum number of results to retrieve as context for the language model.
        - `type` 'retrieval', required
  - `top_logprobs` integer — This is used with `logprobs`. An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.
  - `top_p` number — An alternative or complement to `temperature`. This adjusts how many of the top possibilities to consider.
  - `use_beam_search` boolean — Setting this to `true` will allow the model to [explore more completion options](https://huggingface.co/blog/how-to-generate#beam-search). This is not supported by OpenAI.

## Response `200`

Successful Response

- object

## Other responses

- `422` — Validation Error

---

[API](https://skmtc.net/team-telnyx/apis/telnyx-api-2.md) · [All operations](https://skmtc.net/team-telnyx/apis/telnyx-api-2/llms.txt) · [OpenAPI document](https://skmtc-service-staging.skmtc.workers.dev/v1/apis/team-telnyx/telnyx-api-2/revisions/3fdc16374d70/schema)
