v4

latestOpenAPI 3.1.02026-08-0143190206.8 KB
v1

Create a chat response from text/image chat prompts. This is the endpoint for making requests to chat and image understanding models.

post/v1/chat/completions

Request body

deferredboolean nullable

If set to true, the request returns a request_id. You can then get the deferred response by GET /v1/chat/deferred-completion/{request_id}.

frequency_penaltynumber float nullable

(Not supported by reasoning models) 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.

logit_biasobject nullable

(Unsupported) A JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.

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. Not supported by models grok-4.20 and newer; the field will be silently ignored if set.

max_completion_tokensinteger nullable

An upper bound for the number of tokens that can be generated for a completion, only applies to visible output tokens (i.e. does not apply to tokens used for reasoning or function calls). Defaults to 128,000 when unset; set a larger value to allow longer generations.

max_tokensinteger nullable

[DEPRECATED] The maximum number of tokens that can be generated in the chat completion. Deprecated in favor of max_completion_tokens.

modelstring

Model name for the model to use. Obtainable from https://console.x.ai/team/default/models or https://docs.x.ai/docs/models.

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.

parallel_tool_callsboolean nullable

If set to false, the model can perform maximum one tool call.

presence_penaltynumber float nullable

(Not supported by grok-3 and reasoning models) 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.

prompt_cache_keystring nullable

A stable cache key for best-effort sticky routing / prompt-cache hits across requests sharing a prompt prefix. Plumbed to x-grok-conv-id, same as on /v1/responses.

reasoning_effortstring nullable

Constrains how hard a reasoning model thinks before responding. Only supported by grok-4.3. Possible values are none (disables reasoning completely), low (this is the default if not specified), medium and high (uses the most reasoning tokens).

seedinteger 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. Determinism is not guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend.

service_tier'default' | 'priority'

Processing tier for a request. Determines scheduling priority and billing.

stopstring[] nullable

(Not supported by reasoning models) Up to 4 sequences where the API will stop generating further tokens.

streamboolean nullable

If set, partial message deltas will be sent. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.

temperaturenumber float 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_logprobsinteger nullable

An integer between 0 and 8 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. Not supported by models grok-4.20 and newer; the field will be silently ignored if set.

top_pnumber float nullable

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. It is generally recommended to alter this or temperature but not both.

userstring nullable

A unique identifier representing your end-user, which can help xAI to monitor and detect abuse.

Example request

{
  "max_completion_tokens": 8192,
  "max_tokens": 8192,
  "model": "latest",
  "n": 1,
  "search_parameters": {
    "from_date": "2024-06-24",
    "mode": "auto",
    "sources": [
      {
        "allowed_websites": [
          "wikipedia.com"
        ],
        "country": "BE",
        "excluded_websites": [
          "wikipedia.com"
        ]
      }
    ],
    "to_date": "2024-12-24"
  },
  "stream": true,
  "temperature": 0.2,
  "tools": [
    {
      "sources": [
        {
          "allowed_websites": [
            "wikipedia.com"
          ],
          "country": "BE",
          "excluded_websites": [
            "wikipedia.com"
          ]
        }
      ]
    }
  ],
  "web_search_options": {
    "search_context_size": "medium"
  }
}

Response

Success

citationsstring[] nullable

List of all the external pages used by the model to answer.

createdinteger required

The chat completion creation time in Unix timestamp.

idstring required

A unique ID for the chat response.

modelstring required

Model ID used to create chat completion.

objectstring required

The object type, which is always "chat.completion".

service_tier'default' | 'priority' required

Processing tier for a request. Determines scheduling priority and billing.

system_fingerprintstring nullable

System fingerprint, used to indicate xAI system configuration changes.

Example response

{
  "model": "latest"
}