Chat

Create chat completion

Creates a model response for the given chat conversation with support for retries, fallbacks, prompts, and variables.

post/v2/router/chat/completions

Request body

modelstring required

Model ID used to generate the response, like openai/gpt-4o or anthropic/claude-haiku-4-5-20251001. The AI Gateway offers a wide range of models with different capabilities, performance characteristics, and price points. Refer to the (Supported models)[/docs/proxy/supported-models] to browse available models.

metadataobject

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can have a maximum length of 64 characters and values can have a maximum length of 512 characters.

namestring

The name to display on the trace. If not specified, the default system name will be used.

frequency_penaltynumber nullable

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.

max_tokensinteger nullable

[Deprecated]. The maximum number of tokens that can be generated in the chat completion. This value can be used to control costs for text generated via API.

This value is now deprecated in favor of max_completion_tokens, and is not compatible with o1 series models.

max_completion_tokensinteger nullable

An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens

logprobsboolean nullable

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.

top_logprobsinteger nullable

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. logprobs must be set to true if this parameter is used.

ninteger nullable

How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.

presence_penaltynumber nullable

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.

reasoning_effort'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh'

Constrains effort on reasoning for reasoning models. Currently supported values are none, minimal, low, medium, high, and xhigh. Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.

  • gpt-5.1 defaults to none, which does not perform reasoning. The supported reasoning values for gpt-5.1 are none, low, medium, and high. Tool calls are supported for all reasoning values in gpt-5.1.
  • All models before gpt-5.1 default to medium reasoning effort, and do not support none.
  • The gpt-5-pro model defaults to (and only supports) high reasoning effort.
  • xhigh is currently only supported for gpt-5.1-codex-max.

Any of "none", "minimal", "low", "medium", "high", "xhigh".

verbositystring

Adjusts response verbosity. Lower levels yield shorter answers.

seednumber nullable

If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

temperaturenumber nullable

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

top_pnumber nullable

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass.

top_knumber nullable

Limits the model to consider only the top k most likely tokens at each step.

parallel_tool_callsboolean

Whether to enable parallel function calling during tool use.

modalitiesstring[] nullable

Output types that you would like the model to generate. Most models are capable of generating text, which is the default: ["text"]. The gpt-4o-audio-preview model can also be used to generate audio. To request that this model generate both text and audio responses, you can use: ["text", "audio"].

variablesobject

Variables to substitute in message templates. Uses f-string syntax ({{variableName}}) by default. For advanced templating with Jinja or Mustache syntax, use in conjunction with template_engine.

prompt_cache_keystring

Used by OpenAI to cache responses for similar requests to optimize your cache hit rates. Replaces the legacy user field for prompt caching.

streamboolean

Example request

{
  "fallbacks": [
    {
      "model": "openai/gpt-4o-mini"
    }
  ],
  "retry": {
    "count": 3,
    "on_codes": [
      429,
      500,
      502,
      503,
      504
    ]
  },
  "cache": {
    "ttl": 3600
  },
  "load_balancer": {
    "type": "weight_based",
    "models": [
      {
        "model": "openai/gpt-4o",
        "weight": 0.7
      },
      {
        "model": "anthropic/claude-3-5-sonnet",
        "weight": 0.3
      }
    ]
  },
  "timeout": {
    "call_timeout": 30000
  },
  "variables": {
    "customer_name": "John Smith",
    "product_name": "Premium Plan"
  },
  "orq": {
    "retry": {
      "count": 3,
      "on_codes": [
        429,
        500,
        502
      ]
    },
    "fallbacks": [
      {
        "model": "openai/gpt-5"
      },
      {
        "model": "anthropic/claude-4-opus"
      }
    ],
    "identity": {
      "id": "identity_01ARZ3NDEKTSV4RRFFQ69G5FAV",
      "display_name": "Jane Doe",
      "email": "jane.doe@example.com"
    },
    "thread": {
      "id": "thread_01ARZ3NDEKTSV4RRFFQ69G5FAV",
      "tags": [
        "customer-support"
      ]
    },
    "inputs": {
      "customer_name": "John Smith",
      "issue_type": "billing"
    },
    "cache": {
      "ttl": 3600,
      "type": "exact_match"
    },
    "knowledge_bases": [
      {
        "knowledge_id": "knowledge_01ARZ3NDEKTSV4RRFFQ69G5FAV",
        "top_k": 5
      }
    ],
    "timeout": {
      "call_timeout": 30000
    },
    "security": {
      "mask": [
        "input",
        "system"
      ]
    }
  }
}

Response

Returns a chat completion object, or a streamed sequence of chat completion chunk objects if the request is streamed.

idstring required

A unique identifier for the chat completion.

creatednumber required

The Unix timestamp (in seconds) of when the chat completion was created.

modelstring required

The model used for the chat completion.

system_fingerprintstring nullable

This fingerprint represents the backend configuration that the model runs with.

object'chat.completion' required