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

# Xai

> Python API reference for the xai llm plugin.

<Card title="Usage guide" icon="book" href="/plugins/llm/xai" horizontal>
  Setup, environment variables, and Python/JavaScript/Go usage examples.
</Card>

## XAILLM

xAI Grok LLM plugin for text and reasoning completions.

Wraps the xAI chat completions API (OpenAI-compatible) to integrate Grok
models for real-time voice agents.  Supports tool/function calling, structured
outputs, optional chain-of-thought reasoning, and prompt caching.

### Constructor

```python theme={null}
XAILLM(*, api_key: 'str | None' = None, model: 'XAILLMModel | str' = 'grok-4-1-fast-non-reasoning', base_url: 'str' = 'https://api.x.ai/v1', temperature: 'float' = 0.7, tool_choice: 'str' = 'auto', max_completion_tokens: 'int | None' = None, top_p: 'float | None' = None, frequency_penalty: 'float | None' = None, presence_penalty: 'float | None' = None, seed: 'int | None' = None, stop: 'str | None' = None, user: 'str | None' = None, parallel_tool_calls: 'bool | None' = None, response_format: 'dict | None' = None, reasoning_effort: 'str | None' = None, prompt_cache_key: 'str | None' = None, service_tier: 'str | None' = None, **kwargs) -> 'None'
```

<ParamField path="api_key" type="str | None">
  xAI API key.  Falls back to the `XAI_API_KEY` environment variable when `None`.
</ParamField>

<ParamField path="model" type="XAILLMModel | str" default="grok-4-1-fast-non-reasoning">
  Grok model ID.  Defaults to `'grok-4-1-fast-non-reasoning'`.  Other notable IDs: `'grok-4.3'` (latest general-purpose, supports `reasoning_effort`), `'grok-4.20-0309-reasoning'`, `'grok-4.20-0309-non-reasoning'`, `'grok-4.20-multi-agent-0309'`.
</ParamField>

<ParamField path="base_url" type="str" default="https://api.x.ai/v1">
  xAI API base URL.  Defaults to `'https://api.x.ai/v1'`.  Override for private deployments.
</ParamField>

<ParamField path="temperature" type="float" default="0.7">
  Sampling temperature controlling output randomness. Defaults to `0.7`.  Higher values increase creativity; lower values make output more deterministic.
</ParamField>

<ParamField path="tool_choice" type="str" default="auto">
  Controls how the model selects tools.  Defaults to `'auto'`.  Other values: `'none'`, `'required'`, or a specific tool name dict.
</ParamField>

<ParamField path="max_completion_tokens" type="int | None">
  Maximum number of tokens the model may generate per response.  `None` defaults to `1024`.
</ParamField>

<ParamField path="top_p" type="float | None">
  Nucleus sampling probability mass.  `None` uses the model default.
</ParamField>

<ParamField path="frequency_penalty" type="float | None">
  Penalises repeated tokens proportional to their frequency.  Range `-2.0`–`2.0`; `None` uses the model default.  Cannot be used with reasoning models.
</ParamField>

<ParamField path="presence_penalty" type="float | None">
  Penalises tokens that have already appeared, encouraging topic diversity.  Range `-2.0`–`2.0`; `None` uses the model default.  Cannot be used with reasoning models.
</ParamField>

<ParamField path="seed" type="int | None">
  Fixed integer seed for deterministic sampling.  `None` disables seeding.
</ParamField>

<ParamField path="stop" type="str | None">
  Stop sequence string; generation halts when this token is produced.  `None` disables early stopping.  Cannot be used with reasoning models.
</ParamField>

<ParamField path="user" type="str | None">
  Opaque end-user identifier forwarded to the API for abuse monitoring.  `None` omits the field.
</ParamField>

<ParamField path="parallel_tool_calls" type="bool | None">
  When `True`, allows the model to emit multiple tool calls in a single turn.  `None` uses the model default.
</ParamField>

<ParamField path="response_format" type="dict | None">
  Structured-output schema dict (e.g. `&#123;'type': 'json_object'&#125;`).  `None` returns plain text.
</ParamField>

<ParamField path="reasoning_effort" type="str | None">
  Chain-of-thought depth for models that support it (e.g. `'grok-4.3'`).  Valid values: `'none'`, `'low'` (default when omitted), `'medium'`, `'high'`. `None` lets the model decide.  Cannot be combined with `frequency_penalty`, `presence_penalty`, or `stop`.
</ParamField>

<ParamField path="prompt_cache_key" type="str | None">
  Cache key string for prompt caching.  Requests with the same key reuse cached KV state to reduce latency and cost.  `None` disables caching.
</ParamField>

<ParamField path="service_tier" type="str | None">
  API service tier for routing and rate-limit bucketing.  `None` uses the account default.
</ParamField>

### aclose

```python theme={null}
def aclose(self) -> 'None'
```

Release any resources held by the provider.

### cancel\_current\_generation

```python theme={null}
def cancel_current_generation(self) -> 'None'
```

Cancel the in-progress generation for the active session, if any.

### chat

```python theme={null}
def chat(self, messages: 'Any', tools: 'Any' = None, conversational_graph: 'Any' = None, **kwargs: 'Any') -> 'AsyncIterator[LLMResponse]'
```

Generate a streamed chat completion.

<ParamField path="messages" type="Any" required>
  The conversation history to send to the model.
</ParamField>

<ParamField path="tools" type="Any">
  Optional tool definitions the model may call.
</ParamField>

<ParamField path="conversational_graph" type="Any">
  Optional conversational graph guiding the exchange.
</ParamField>

<ResponseField name="returns" type="AsyncIterator[LLMResponse]">
  LLMResponse: Response chunks produced as the model generates output.
</ResponseField>

### emit

```python theme={null}
def emit(self, event: 'T', *args: 'Any') -> 'None'
```

Emit an event, invoking all registered handlers with the given arguments.

Handlers are called in registration order. Coroutine handlers are
scheduled on the running event loop. If the emitter is closed, the call
is a no-op.

<ParamField path="event" type="T" required>
  The event to emit.
</ParamField>

### off

```python theme={null}
def off(self, event: 'T', callback: 'Callable[..., Any]') -> 'None'
```

Remove a previously registered handler for an event.

If the handler is not registered for the event, the call is a no-op.

<ParamField path="event" type="T" required>
  The event the handler was registered for.
</ParamField>

<ParamField path="callback" type="Callable[..., Any]" required>
  The handler to remove.
</ParamField>

### on

```python theme={null}
def on(self, event: 'T', callback: 'Callable[..., Any] | None' = None) -> 'Callable[..., Any]'
```

Register a handler for an event.

Can be used directly by passing a callback, or as a decorator when
`callback` is omitted.

<ParamField path="event" type="T" required>
  The event to listen for.
</ParamField>

<ParamField path="callback" type="Callable[..., Any] | None">
  The handler to invoke when the event is emitted. If `None`, a decorator is returned that registers the decorated function.
</ParamField>

<ResponseField name="returns" type="Callable[..., Any]">
  The registered callback when `callback` is provided, otherwise a decorator that registers and returns the function it wraps.
</ResponseField>

***

## XAILLMModel

```python theme={null}
XAILLMModel = typing.Literal['grok-4.3', 'grok-4.20-0309-reasoning', 'grok-4.20-0309-non-reasoning', 'grok-4.20-multi-agent-0309', 'grok-build-0.1']
```
