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

# OpenAI Provider

> OpenAI LLM integration with automatic API key rotation.

## Usage

```python theme={null}
from langchat.providers import OpenAI

llm = OpenAI("gpt-4o-mini")              # reads OPENAI_API_KEY
llm = OpenAI("gpt-4o", temperature=0.3)  # explicit model + temp
```

Pass to `LangChat`:

```python theme={null}
from langchat import LangChat
from langchat.providers import OpenAI, Pinecone, Supabase

lc = LangChat(
    llm=OpenAI("gpt-4o-mini"),
    vector_db=Pinecone("my-index"),
    db=Supabase(),
)
```

***

## Parameters

<ParamField path="model" type="str" default="gpt-4o-mini">
  OpenAI model name. First positional argument.
</ParamField>

<ParamField path="api_key" type="str | None" default="None">
  Single API key. Falls back to `OPENAI_API_KEY` environment variable.
</ParamField>

<ParamField path="api_keys" type="list[str] | None" default="None">
  Multiple API keys for automatic rotation. Takes precedence over `api_key`.
</ParamField>

<ParamField path="temperature" type="float" default="1.0">
  Sampling temperature. `0.0` = deterministic. `1.0` = creative.
</ParamField>

<ParamField path="max_retries_per_key" type="int" default="2">
  Number of retries per key before rotating to the next one.
</ParamField>

***

## API key rotation

When you have multiple API keys (e.g., to handle rate limits across multiple OpenAI projects), pass them as a list:

```python theme={null}
llm = OpenAI(
    "gpt-4o-mini",
    api_keys=[
        "sk-proj-key1...",
        "sk-proj-key2...",
        "sk-proj-key3...",
    ],
    max_retries_per_key=2,
)
```

If a request fails on `key1`, LangChat retries up to `max_retries_per_key` times, then rotates to `key2`, and so on. Total maximum attempts = `len(api_keys) × max_retries_per_key`.

***

## Environment variable

Set `OPENAI_API_KEY` in your `.env`:

```bash theme={null}
OPENAI_API_KEY=sk-...
```

***

## Available models

| Model           | Context | Speed   | Quality                    |
| --------------- | ------- | ------- | -------------------------- |
| `gpt-4o-mini`   | 128k    | Fast    | Good — recommended default |
| `gpt-4o`        | 128k    | Medium  | High                       |
| `gpt-4-turbo`   | 128k    | Medium  | High                       |
| `gpt-3.5-turbo` | 16k     | Fastest | Basic                      |

Any valid OpenAI chat completion model name is accepted.
