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

# xAI Grok

> Configure xAI Grok models for document extraction

xAI Grok models provide competitive document understanding with OpenAI-compatible API format.

## Installation

```bash theme={null}
npm install @doclo/providers-llm
```

## Basic Setup

### Via OpenRouter (Recommended)

```typescript theme={null}
import { createVLMProvider } from '@doclo/providers-llm';

const provider = createVLMProvider({
  provider: 'xai',
  model: 'x-ai/grok-4.1',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});
```

### Native API

```typescript theme={null}
const provider = createVLMProvider({
  provider: 'xai',
  model: 'grok-4.1',
  apiKey: process.env.XAI_API_KEY!
});
```

## Available Models

| Model           | Context | Reasoning | Best For               |
| --------------- | ------- | --------- | ---------------------- |
| `grok-4.1`      | 256k    | Yes       | Complex extraction     |
| `grok-4.1-fast` | 2M      | No        | High-volume processing |
| `grok-4`        | 256k    | Yes       | Previous generation    |
| `grok-4-fast`   | 2M      | No        | Fast processing        |

### OpenRouter Model IDs

When using OpenRouter, prefix models with `x-ai/`:

```typescript theme={null}
// OpenRouter
model: 'x-ai/grok-4.1'

// Native
model: 'grok-4.1'
```

## Configuration Options

```typescript theme={null}
createVLMProvider({
  provider: 'xai',
  model: string,           // Model ID
  apiKey: string,          // API key
  via?: 'openrouter',      // Access method
  baseUrl?: string,        // Custom endpoint
  limits?: {
    maxFileSize?: number,      // Max file size (bytes)
    requestTimeout?: number,   // Timeout (ms)
    maxJsonDepth?: number      // Max JSON nesting
  }
})
```

## Capabilities

| Feature           | Support                     |
| ----------------- | --------------------------- |
| Images            | Yes (JPEG, PNG only)        |
| PDFs              | Yes                         |
| Structured Output | Yes (json\_schema)          |
| Reasoning         | Yes (grok-4.1, grok-4)      |
| Streaming         | No (with structured output) |

## Input Formats

### Images

xAI only supports JPEG and PNG images:

```typescript theme={null}
// Supported: JPEG, PNG only
{
  images: [{
    url: 'https://example.com/image.jpg',
    mimeType: 'image/jpeg'
  }]
}

// Or base64
{
  images: [{
    base64: 'data:image/jpeg;base64,...',
    mimeType: 'image/jpeg'
  }]
}
```

Image limits: 30MB per file, max 10 images via API.

### PDFs

```typescript theme={null}
{
  pdfs: [{
    base64: 'data:application/pdf;base64,...'
  }]
}

// Or via URL
{
  pdfs: [{
    url: 'https://example.com/document.pdf'
  }]
}
```

xAI also supports DOCX, TXT, MD, and CSV files.

## Extended Reasoning

Grok models support reasoning for complex extractions:

```typescript theme={null}
import { createFlow, extract } from '@doclo/flows';

const flow = createFlow()
  .step('extract', extract({
    provider: createVLMProvider({
      provider: 'xai',
      model: 'x-ai/grok-4.1',
      apiKey: process.env.OPENROUTER_API_KEY!,
      via: 'openrouter'
    }),
    schema: complexSchema,
    reasoning: {
      enabled: true,
      effort: 'high'  // 'low' | 'medium' | 'high'
    }
  }))
  .build();
```

Reasoning configuration uses the same `effort` parameter as OpenAI.

## Large Context Processing

Grok fast models have 2M token context:

```typescript theme={null}
const provider = createVLMProvider({
  provider: 'xai',
  model: 'x-ai/grok-4.1-fast',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});

// Can process very large documents
```

## Production Setup

```typescript theme={null}
import { buildLLMProvider } from '@doclo/providers-llm';

const provider = buildLLMProvider({
  providers: [
    {
      provider: 'xai',
      model: 'x-ai/grok-4.1',
      apiKey: process.env.OPENROUTER_API_KEY!,
      via: 'openrouter'
    },
    {
      provider: 'xai',
      model: 'x-ai/grok-4.1-fast',
      apiKey: process.env.OPENROUTER_API_KEY!,
      via: 'openrouter'
    }
  ],
  maxRetries: 2,
  retryDelay: 1000,
  useExponentialBackoff: true
});
```

## Pricing

Via OpenRouter (approximate):

| Model         | Input (per 1k) | Output (per 1k) |
| ------------- | -------------- | --------------- |
| grok-4.1      | \$0.003        | \$0.015         |
| grok-4.1-fast | \$0.005        | \$0.025         |
| grok-4        | \$0.003        | \$0.015         |
| grok-4-fast   | \$0.005        | \$0.025         |

## Example: Technical Document

```typescript theme={null}
import { createFlow, extract } from '@doclo/flows';
import { createVLMProvider } from '@doclo/providers-llm';

const provider = createVLMProvider({
  provider: 'xai',
  model: 'x-ai/grok-4.1',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});

const techDocSchema = {
  type: 'object',
  properties: {
    title: { type: 'string' },
    version: { type: 'string' },
    components: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          name: { type: 'string' },
          specifications: {
            type: 'object',
            additionalProperties: { type: 'string' }
          }
        }
      }
    },
    requirements: {
      type: 'array',
      items: { type: 'string' }
    }
  }
};

const flow = createFlow()
  .step('extract', extract({
    provider,
    schema: techDocSchema
  }))
  .build();

const result = await flow.run({
  base64: 'data:application/pdf;base64,...'
});

console.log(result.output.components);
```

## API Compatibility

xAI uses OpenAI-compatible API format, making it easy to switch between providers:

```typescript theme={null}
// Same code works for both OpenAI and xAI
const openaiProvider = createVLMProvider({
  provider: 'openai',
  model: 'openai/gpt-4.1',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});

const xaiProvider = createVLMProvider({
  provider: 'xai',
  model: 'x-ai/grok-4.1',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});

// Both can be used interchangeably in flows
```

## Next Steps

<CardGroup cols={2}>
  <Card title="OpenAI" icon="robot" href="/sdk/providers/llm/openai">
    GPT-4 models
  </Card>

  <Card title="Google" icon="google" href="/sdk/providers/llm/google">
    Gemini models
  </Card>
</CardGroup>
