> ## 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.

# OpenAI

> Configure OpenAI models for document extraction

OpenAI provides GPT-4 series models with vision capabilities for document processing.

## 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: 'openai',
  model: 'openai/gpt-4.1',
  apiKey: process.env.OPENROUTER_API_KEY!,
  via: 'openrouter'
});
```

### Native API

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

## Available Models

| Model          | Context | Reasoning | Best For                  |
| -------------- | ------- | --------- | ------------------------- |
| `gpt-5.1`      | 256k    | Yes       | Complex extraction        |
| `gpt-4.1`      | 128k    | No        | General extraction        |
| `gpt-4.1-mini` | 128k    | No        | Cost-effective extraction |
| `o3`           | 200k    | Yes       | Complex reasoning         |
| `o3-mini`      | 200k    | Yes       | Fast reasoning            |
| `o4-mini`      | 200k    | Yes       | Latest reasoning model    |

### OpenRouter Model IDs

When using OpenRouter, prefix models with `openai/`:

```typescript theme={null}
// OpenRouter
model: 'openai/gpt-4.1'

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

## Configuration Options

```typescript theme={null}
createVLMProvider({
  provider: 'openai',
  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 (PNG, JPEG, WebP, GIF) |
| PDFs              | Yes (up to 100 pages)      |
| Structured Output | Yes (json\_schema)         |
| Reasoning         | o3, o4-mini models         |
| Streaming         | Yes                        |

## Input Formats

### Images

```typescript theme={null}
// Via URL
{
  images: [{
    url: 'https://example.com/image.jpg',
    mimeType: 'image/jpeg'
  }]
}

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

### PDFs

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

## Extended Reasoning

Enable reasoning for complex extractions:

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

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

Reasoning configuration:

| Option    | Type    | Description                              |
| --------- | ------- | ---------------------------------------- |
| `enabled` | boolean | Enable reasoning                         |
| `effort`  | string  | Reasoning depth: 'low', 'medium', 'high' |
| `exclude` | boolean | Exclude reasoning tokens from response   |

## Production Setup

Use `buildLLMProvider` for retry logic and fallback:

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

const provider = buildLLMProvider({
  providers: [
    {
      provider: 'openai',
      model: 'openai/gpt-4.1',
      apiKey: process.env.OPENROUTER_API_KEY!,
      via: 'openrouter'
    }
  ],
  maxRetries: 2,
  retryDelay: 1000,
  useExponentialBackoff: true,
  circuitBreakerThreshold: 3
});
```

## Pricing

Via OpenRouter (approximate):

| Model        | Input (per 1k) | Output (per 1k) |
| ------------ | -------------- | --------------- |
| gpt-4.1      | \$0.002        | \$0.008         |
| gpt-4.1-mini | \$0.0004       | \$0.0016        |
| o3           | \$0.010        | \$0.040         |
| o3-mini      | \$0.0011       | \$0.0044        |

## Example: Invoice Extraction

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

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

const invoiceSchema = {
  type: 'object',
  properties: {
    invoiceNumber: { type: 'string' },
    date: { type: 'string' },
    total: { type: 'number' },
    vendor: { type: 'string' }
  }
};

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

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

console.log(result.output);
// { invoiceNumber: 'INV-001', date: '2024-01-15', total: 1250, vendor: 'Acme Corp' }
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Anthropic" icon="message" href="/sdk/providers/llm/anthropic">
    Claude models
  </Card>

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