litellm/docs/my-website/docs/providers/xai.md
Cesar Garcia 1c65800f4a
Feat: add support for Grok 4.1 Fast models (#16936)
* feat: Add support for Grok 4.1 Fast models

Add new xAI Grok 4.1 Fast models optimized for high-performance agentic tool calling:

- xai/grok-4-1-fast (alias for grok-4-1-fast-reasoning)
- xai/grok-4-1-fast-reasoning (with reasoning capabilities)
- xai/grok-4-1-fast-reasoning-latest
- xai/grok-4-1-fast-non-reasoning (without reasoning for faster responses)
- xai/grok-4-1-fast-non-reasoning-latest

Features:
- Context window: 2,000,000 tokens
- Pricing: $0.20/1M input, $0.50/1M output tokens
- Cached tokens: $0.05/1M tokens
- Supports: Function calling, Structured outputs, Vision, Audio input, Web search, Reasoning

Fixes #16927

* docs: Add comprehensive Grok models documentation

- Add 'Supported Models' section highlighting new Grok 4.1 Fast models
- Include comparison guide for reasoning vs non-reasoning models
- Add complete model family table (Grok 4.1, 4, 3, Code, 2)
- Add features legend explaining capabilities
- Remove pricing details (link to xAI docs instead for current rates)
- Improve documentation clarity and consistency

Related to #16927

* docs: Minor corrections to xai.md
2025-11-21 15:57:55 -08:00

7.9 KiB

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

xAI

https://docs.x.ai/docs

:::tip

We support ALL xAI models, just set model=xai/<any-model-on-xai> as a prefix when sending litellm requests

:::

Supported Models

Latest Release - Grok 4.1 Fast: Optimized for high-performance agentic tool calling with 2M context and prompt caching.

Model Context Features
xai/grok-4-1-fast-reasoning 2M tokens Reasoning, Function calling, Vision, Audio, Web search, Caching
xai/grok-4-1-fast-non-reasoning 2M tokens Function calling, Vision, Audio, Web search, Caching

When to use:

  • ✅ Reasoning model: Complex analysis, planning, multi-step reasoning problems
  • ✅ Non-reasoning model: Simple queries, faster responses, lower token usage

Example:

from litellm import completion

# With reasoning
response = completion(
    model="xai/grok-4-1-fast-reasoning",
    messages=[{"role": "user", "content": "Analyze this problem step by step..."}]
)

# Without reasoning
response = completion(
    model="xai/grok-4-1-fast-non-reasoning",
    messages=[{"role": "user", "content": "What's 2+2?"}]
)

All Available Models

Model Family Model Context Features
Grok 4.1 xai/grok-4-1-fast-reasoning 2M Reasoning, Tools, Vision, Audio, Web search, Caching
xai/grok-4-1-fast-non-reasoning 2M Tools, Vision, Audio, Web search, Caching
Grok 4 xai/grok-4 256K Tools, Web search
xai/grok-4-0709 256K Tools, Web search
xai/grok-4-fast-reasoning 2M Reasoning, Tools, Web search
xai/grok-4-fast-non-reasoning 2M Tools, Web search
Grok 3 xai/grok-3 131K Tools, Web search
xai/grok-3-mini 131K Tools, Web search
xai/grok-3-fast-beta 131K Tools, Web search
Grok Code xai/grok-code-fast 256K Reasoning, Tools, Code generation, Caching
Grok 2 xai/grok-2 131K Tools, Vision
xai/grok-2-vision-latest 32K Tools, Vision

Features:

  • Reasoning = Chain-of-thought reasoning with reasoning tokens
  • Tools = Function calling / Tool use
  • Web search = Live internet search
  • Vision = Image understanding
  • Audio = Audio input support
  • Caching = Prompt caching for cost savings
  • Code generation = Optimized for code tasks

Pricing: See xAI's pricing page for current rates.

API Key

# env variable
os.environ['XAI_API_KEY']

Sample Usage

from litellm import completion
import os

os.environ['XAI_API_KEY'] = ""
response = completion(
    model="xai/grok-3-mini-beta",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Boston today in Fahrenheit?",
        }
    ],
    max_tokens=10,
    response_format={ "type": "json_object" },
    seed=123,
    stop=["\n\n"],
    temperature=0.2,
    top_p=0.9,
    tool_choice="auto",
    tools=[],
    user="user",
)
print(response)

Sample Usage - Streaming

from litellm import completion
import os

os.environ['XAI_API_KEY'] = ""
response = completion(
    model="xai/grok-3-mini-beta",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Boston today in Fahrenheit?",
        }
    ],
    stream=True,
    max_tokens=10,
    response_format={ "type": "json_object" },
    seed=123,
    stop=["\n\n"],
    temperature=0.2,
    top_p=0.9,
    tool_choice="auto",
    tools=[],
    user="user",
)

for chunk in response:
    print(chunk)

Sample Usage - Vision

import os 
from litellm import completion

os.environ["XAI_API_KEY"] = "your-api-key"

response = completion(
    model="xai/grok-2-vision-latest",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://science.nasa.gov/wp-content/uploads/2023/09/web-first-images-release.png",
                        "detail": "high",
                    },
                },
                {
                    "type": "text",
                    "text": "What's in this image?",
                },
            ],
        },
    ],
)

Usage with LiteLLM Proxy Server

Here's how to call a XAI model with the LiteLLM Proxy Server

  1. Modify the config.yaml
model_list:
  - model_name: my-model
    litellm_params:
      model: xai/<your-model-name>  # add xai/ prefix to route as XAI provider
      api_key: api-key                 # api key to send your model
  1. Start the proxy
$ litellm --config /path/to/config.yaml
  1. Send Request to LiteLLM Proxy Server
import openai
client = openai.OpenAI(
    api_key="sk-1234",             # pass litellm proxy key, if you're using virtual keys
    base_url="http://0.0.0.0:4000" # litellm-proxy-base url
)

response = client.chat.completions.create(
    model="my-model",
    messages = [
        {
            "role": "user",
            "content": "what llm are you"
        }
    ],
)

print(response)
curl --location 'http://0.0.0.0:4000/chat/completions' \
    --header 'Authorization: Bearer sk-1234' \
    --header 'Content-Type: application/json' \
    --data '{
    "model": "my-model",
    "messages": [
        {
        "role": "user",
        "content": "what llm are you"
        }
    ],
}'

Reasoning Usage

LiteLLM supports reasoning usage for xAI models.

import litellm
response = litellm.completion(
    model="xai/grok-3-mini-beta",
    messages=[{"role": "user", "content": "What is 101*3?"}],
    reasoning_effort="low",
)

print("Reasoning Content:")
print(response.choices[0].message.reasoning_content)

print("\nFinal Response:")
print(completion.choices[0].message.content)

print("\nNumber of completion tokens (input):")
print(completion.usage.completion_tokens)

print("\nNumber of reasoning tokens (input):")
print(completion.usage.completion_tokens_details.reasoning_tokens)
import openai
client = openai.OpenAI(
    api_key="sk-1234",             # pass litellm proxy key, if you're using virtual keys
    base_url="http://0.0.0.0:4000" # litellm-proxy-base url
)

response = client.chat.completions.create(
    model="xai/grok-3-mini-beta",
    messages=[{"role": "user", "content": "What is 101*3?"}],
    reasoning_effort="low",
)

print("Reasoning Content:")
print(response.choices[0].message.reasoning_content)

print("\nFinal Response:")
print(completion.choices[0].message.content)

print("\nNumber of completion tokens (input):")
print(completion.usage.completion_tokens)

print("\nNumber of reasoning tokens (input):")
print(completion.usage.completion_tokens_details.reasoning_tokens)

Example Response:

Reasoning Content:
Let me calculate 101 multiplied by 3:
101 * 3 = 303.
I can double-check that: 100 * 3 is 300, and 1 * 3 is 3, so 300 + 3 = 303. Yes, that's correct.

Final Response:
The result of 101 multiplied by 3 is 303.

Number of completion tokens (input):
14

Number of reasoning tokens (input):
310