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

# xAI (Grok)

[xAI's Grok](https://x.ai/) provides advanced reasoning capabilities and real-time knowledge. The plugin supports conversation memory, streaming responses, and function calling (Grok 4.1+).

<Info>
  Vision Agents requires a [Stream](https://getstream.io/try-for-free/) account
  for real-time transport. Most providers offer free tiers to get started.
</Info>

<Tip>
  xAI also provides [Realtime speech-to-speech](/integrations/realtime/xai) and [text-to-speech](/integrations/tts/xai).
</Tip>

## Installation

```sh theme={null}
uv add "vision-agents[xai]"
```

## Quick Start

```python theme={null}
from vision_agents.core import Agent, User
from vision_agents.plugins import xai, getstream, deepgram, elevenlabs

agent = Agent(
    edge=getstream.Edge(),
    agent_user=User(name="Assistant", id="agent"),
    instructions="You are a helpful assistant.",
    llm=xai.LLM(model="grok-4-latest"),
    stt=deepgram.STT(),
    tts=elevenlabs.TTS(),
)
```

<Warning>
  Set `XAI_API_KEY` in your environment or pass `api_key` directly.
</Warning>

## Parameters

| Name               | Type  | Default           | Description                                 |
| ------------------ | ----- | ----------------- | ------------------------------------------- |
| `model`            | `str` | `"grok-4-latest"` | Model (`"grok-4-latest"`, `"grok-4.1"`)     |
| `api_key`          | `str` | `None`            | API key (defaults to `XAI_API_KEY` env var) |
| `tools_max_rounds` | `int` | `3`               | Maximum tool-call rounds per response       |

## Function Calling

Grok 4.1+ supports function calling:

```python theme={null}
@agent.llm.register_function(description="Get weather for a location")
async def get_weather(location: str) -> str:
    return f"The weather in {location} is sunny and 72°F"
```

See the [Function Calling guide](/guides/mcp-tool-calling) for details.

## Events

The xAI plugin emits a low-level event for streaming chunks. Most developers should use the core [LLMResponseCompletedEvent](/reference/events-reference#llmresponsecompletedevent) instead.

```python theme={null}
from vision_agents.plugins.xai.events import XAIChunkEvent

@agent.events.subscribe
async def on_xai_chunk(event: XAIChunkEvent):
    print(f"Chunk: {event.chunk}")
```

## Next Steps

<CardGroup cols={2}>
  <Card title="xAI Realtime" icon="bolt" href="/integrations/realtime/xai">
    Speech-to-speech over WebSocket
  </Card>

  <Card title="xAI TTS" icon="volume-high" href="/integrations/tts/xai">
    Text-to-speech with expressive voices
  </Card>
</CardGroup>
