Inference API

Legacy & Deprecated

POST /v1/completions

(Legacy - Not supported by reasoning models) Create a text completion response for a given prompt. Replaced by /v1/chat/completions.

Request Body

  • best_of (integer | null) — (Unsupported) Generates multiple completions internally and returns the top-scoring one. Not functional yet.

  • echo (boolean | null) — Option to include the original prompt in the response along with the generated completion.

  • frequency_penalty (number | null) — (Unsupported) 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_bias (object | null) — (Unsupported) Accepts a JSON object that maps tokens to an associated bias value from -100 to 100. You can use this tokenizer tool to convert text to token IDs. 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.

  • logprobs (boolean | null) — Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the logprob of the sampled token, so there may be up to `logprobs+1` elements in the response. Not supported by models `grok-4.20` and newer; the field will be silently ignored if set.

  • max_tokens (integer | null) — Limits the number of tokens that can be produced in the output. Ensure the sum of prompt tokens and `max_tokens` does not exceed the model's context limit.

  • model (string) — Specifies the model to be used for the request.

  • n (integer | null) — Determines how many completion sequences to produce for each prompt. Be cautious with its use due to high token consumption; adjust `max_tokens` and stop sequences accordingly.

  • presence_penalty (number | null) — (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 (string | array<string>)

  • seed (integer | null) — 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.

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

  • stream (boolean | null) — Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a `data: [DONE]` message.

  • stream_options (object)

    • include_usage (boolean, required) — Set an additional chunk to be streamed before the `data: [DONE]` message. The other chunks will return `null` in `usage` field.

  • suffix (string | null) — (Unsupported) Optional string to append after the generated text.

  • temperature (number | null) — 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. We generally recommend altering this or `top_p` but not both.

  • top_p (number | null) — 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. We generally recommend altering this or temperature but not both.

  • user (string | null) — A unique identifier representing your end-user, which can help xAI to monitor and detect abuse.

Response Body

  • choices (array<object>, required) — A list of response choices from the model. The length corresponds to the `n` in request body (default to 1).

    • finish_reason (string, required) — Finish reason. `"stop"` means the inference has reached a model-defined or user-supplied stop sequence in `stop`. `"length"` means the inference result has reached models' maximum allowed token length or user defined value in `max_tokens`. `"end_turn"` or `null` in streaming mode when the chunk is not the last.

    • index (integer, required) — Index of the choice.

    • text (string, required) — Text response.

  • created (integer, required) — The chat completion creation time in Unix timestamp.

  • id (string, required) — ID of the request.

  • model (string, required) — Model to be used.

  • object (string, required) — Object type of the response. This is always `"text_completion"`.

  • system_fingerprint (string | null) — System fingerprint, used to indicate xAI system configuration changes.

  • usage (object)

    • completion_tokens (integer, required) — Total completion token used.

    • completion_tokens_details (object, required) — Details of completion usage.

      • accepted_prediction_tokens (integer, required) — The number of tokens in the prediction that appeared in the completion.

      • audio_tokens (integer, required) — Audio input tokens generated by the model.

      • reasoning_tokens (integer, required) — Tokens generated by the model for reasoning.

      • rejected_prediction_tokens (integer, required) — The number of tokens in the prediction that did not appear in the completion.

    • cost_in_usd_ticks (integer, required) — Accurate cost of this request in USD ticks, where "tick" is defined as follows: TICKS_IN_USD_CENT: i64 = 100_000_000 which means there is 10'000'000'000 ticks in one *dollar*.

    • num_sources_used (integer, required) — Number of individual live search source used.

    • prompt_tokens (integer, required) — Total prompt token used.

    • prompt_tokens_details (object, required) — Details of prompt usage.

      • audio_tokens (integer, required) — Audio prompt token used.

      • cached_tokens (integer, required) — Token cached by xAI from previous requests and reused for this request.

      • image_tokens (integer, required) — Image prompt token used.

      • text_tokens (integer, required) — Total text prompt token used (cached + non-cached text tokens).

    • total_tokens (integer, required) — Total token used, the sum of prompt token and completion token amount.

**Request example:**

JSON

{
  "prompt": "1, 2, 3, 4, ",
  "model": "grok-3",
  "max_tokens": 3
}

**Response example:**

JSON

{
  "id": "873492b3-6144-4279-ac2e-2c45242c5ce6",
  "object": "text_completion",
  "created": 1743771779,
  "model": "grok-3",
  "choices": [
    {
      "index": 0,
      "text": "5, ",
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 3,
    "total_tokens": 15,
    "prompt_tokens_details": {
      "text_tokens": 12,
      "audio_tokens": 0,
      "image_tokens": 0,
      "cached_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 0,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    }
  },
  "system_fingerprint": "fp_156d35dcaa"
}

POST /v1/messages

Create a messages response. This endpoint is compatible with the Anthropic API.

Request Body

  • max_tokens (integer) — The maximum number of tokens to generate before stopping. The model may stop before the max_tokens when it reaches the stop sequence.

  • messages (array<object>) — Input messages.

    • content (string | array<object | object | object | object | object | object>, required)

    • role (string, required) — The role that the message belongs to, `"system"` for system prompt, `"user"` for user prompt, and `"assistant"` for response from the model.

  • metadata (object)

    • user_id (string | null) — A unique identifier representing your end-user, which can help xAI to monitor and detect abuse.

  • model (string) — Model name for the model to use.

  • stop_sequences (array | null) — (Not supported by reasoning models) Up to 4 sequences where the API will stop generating further tokens.

  • stream (boolean | null) — 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.

  • system (string | array<object>)

  • temperature (number | null) — 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. It may not work well with reasoning models.

  • tool_choice (object | object | object)

  • tools (array | null) — A list of tools the model may call in JSON-schema. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.

  • top_k (integer | null) — (Unsupported) When generating next tokens, randomly selecting the next token from the k most likely options.

  • top_p (number | null) — 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.

Response Body

  • content (array<object | object | object | object>, required) — Response message content.

  • id (string, required) — Unique object identifier.

  • model (string, required) — Model name that handled the request.

  • role (string, required) — Role of the generated message. Always `"assistant"`

  • stop_reason (string | null) — Reason to stop. `"stop_sequence"` means the inference has reached a model-defined or user-supplied stop sequence in `stop`. `"max_tokens"` means the inference result has reached models' maximum allowed token length or user defined value in `max_tokens`. `"end_turn"` or `null` in streaming mode when the chunk is not the last. `"tool_use"` means the model has called a tool and is waiting for the tool response.

  • stop_sequence (string | null) — Custom stop sequence used to stop the generation.

  • type (string, required) — Object type. This is always `"message"` for message types.

  • usage (object, required)

    • cache_creation_input_tokens (integer, required) — (Unsupported) Number of tokens written to the cache when creating a new entry.

    • cache_read_input_tokens (integer, required) — Number of tokens retrieved from the cache for this request.

    • input_tokens (integer, required) — Number of input tokens used

    • output_tokens (integer, required) — Number of output tokens used

**Request example:**

JSON

{
  "model": "latest",
  "max_tokens": 32,
  "messages": [
    {
      "role": "user",
      "content": "Hello, world"
    }
  ]
}

**Response example:**

JSON

{
  "id": "4f224bfb-9d53-4c82-b40a-b7cd80831ec2",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello there! \"Hello, world\" is a classic, isn't it? Whether you're just saying hi or channeling your inner coder, I'm happy to greet you back"
    }
  ],
  "model": "latest",
  "stop_reason": "max_tokens",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 9,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0,
    "output_tokens": 32
  }
}

POST /v1/complete

(Legacy - Not supported by reasoning models) Create a text completion response. This endpoint is compatible with the Anthropic API.

Request Body

  • max_tokens_to_sample (integer) — The maximum number of tokens to generate before stopping.

  • metadata (object)

    • user_id (string | null) — A unique identifier representing your end-user, which can help xAI to monitor and detect abuse.

  • model (string) — Model to use for completion.

  • prompt (string) — Prompt for the model to perform completion on.

  • stop_sequences (array | null) — (Not supported by reasoning models) Up to 4 sequences where the API will stop generating further tokens.

  • stream (boolean | null) — (Unsupported) 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.

  • temperature (number | null) — 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_k (integer | null) — (Unsupported) When generating next tokens, randomly selecting the next token from the k most likely options.

  • top_p (number | null) — 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.

Response Body

  • completion (string, required) — The completion content up to and excluding stop sequences.

  • id (string, required) — ID of the completion response.

  • model (string, required) — The model that handled the request.

  • stop_reason (string | null) — The reason to stop completion. `"stop_sequence"` means the inference has reached a model-defined or user-supplied stop sequence in `stop`. `"length"` means the inference result has reached models' maximum allowed token length or user defined value in `max_tokens`. `"end_turn"` or `null` in streaming mode when the chunk is not the last.

  • type (string, required) — Completion response object type. This is always `"completion"`.

**Request example:**

JSON

{
  "model": "grok-3",
  "max_tokens_to_sample": 8,
  "temperature": 0.1,
  "prompt": "\n\nHuman: Hello, how are you?\n\nAssistant:"
}

**Response example:**

JSON

{
  "type": "completion",
  "id": "982044c5-760c-4c8d-8936-f906b5cedc26",
  "completion": " Hey there! I'm doing great, thanks",
  "stop_reason": "max_tokens",
  "model": "grok-3"
}