> ## Documentation Index
> Fetch the complete documentation index at: https://docs.definable.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# xAI

> Use xAI Grok models with optional live search capabilities.

## Setup

```bash theme={null}
export XAI_API_KEY="xai-..."
```

## Basic Usage

```python theme={null}
from definable.model import xAI
from definable.model.message import Message

model = xAI(id="grok-3")
response = model.invoke(
    messages=[Message(role="user", content="Hello!")],
    assistant_message=Message(role="assistant", content=""),
)
print(response.content)
```

## Parameters

<ParamField path="id" type="str" default="grok-3">
  Model identifier. Common values: `grok-3`, `grok-2`.
</ParamField>

<ParamField path="api_key" type="str">
  xAI API key. Defaults to the `XAI_API_KEY` environment variable.
</ParamField>

<ParamField path="base_url" type="str" default="https://api.x.ai/v1">
  xAI API base URL.
</ParamField>

<ParamField path="temperature" type="float">
  Sampling temperature.
</ParamField>

<ParamField path="max_tokens" type="int">
  Maximum output tokens.
</ParamField>

<ParamField path="search_parameters" type="dict">
  Configuration for Grok's live search capabilities.
</ParamField>

## Live Search

xAI Grok models can search the web for real-time information:

```python theme={null}
from definable.model.message import Message

model = xAI(
    id="grok-3",
    search_parameters={"mode": "auto"},
)

response = model.invoke(
    messages=[Message(role="user", content="What happened in tech news today?")],
    assistant_message=Message(role="assistant", content=""),
)
print(response.content)

# Access citations if available
if response.citations:
    for url in response.citations.urls:
        print(f"  Source: {url.url} - {url.title}")
```

## Streaming

```python theme={null}
from definable.model.message import Message

for chunk in model.invoke_stream(
    messages=[Message(role="user", content="Explain the latest AI developments.")],
    assistant_message=Message(role="assistant", content=""),
):
    if chunk.content:
        print(chunk.content, end="", flush=True)
```

## Async Usage

```python theme={null}
from definable.model.message import Message

response = await model.ainvoke(
    messages=[Message(role="user", content="Hello!")],
    assistant_message=Message(role="assistant", content=""),
)
```

<Note>
  xAI uses an OpenAI-compatible API with extensions for live search. Citations are returned in the response when search is enabled.
</Note>
