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

# LangChain

> Use LangChain with Auriko's routing and cost optimization

Use Auriko as your LLM provider in LangChain with a drop-in `ChatOpenAI` replacement.

This integration is Python-only. For TypeScript, use the [Vercel AI SDK](/frameworks/vercel-ai-sdk) integration or configure the OpenAI SDK directly with Auriko's base URL.

## Prerequisites

* An [Auriko API key](https://auriko.ai/signup?redirectTo=%2Fdashboard%3Ftab%3Dapi-keys)

## Installation

```bash theme={null}
pip install "auriko[langchain]"
```

## Use SDK adapter

Use the `AurikoChatOpenAI` adapter:

```python theme={null}
from auriko.frameworks.langchain import AurikoChatOpenAI

llm = AurikoChatOpenAI(model="gpt-5.4")
```

`AurikoChatOpenAI` extends LangChain's `ChatOpenAI` with:

* `use_responses_api=False` set by default (ensures routing metadata and typed error mapping)
* Routing injection via `extra_body`
* OpenAI error mapping to typed Auriko error classes

```python theme={null}
from auriko.frameworks.langchain import AurikoChatOpenAI

llm = AurikoChatOpenAI(model="gpt-5.4")

# Simple invoke
response = llm.invoke("What is 2+2?")
print(response.content)

# Streaming
for chunk in llm.stream("Count to 5"):
    print(chunk.content, end="", flush=True)

# With messages
from langchain_core.messages import HumanMessage, SystemMessage

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Explain quantum computing briefly."),
]
response = llm.invoke(messages)
print(response.content)
```

## Configure options

`AurikoChatOpenAI` accepts these parameters:

| Parameter  | Type                     | Default                      | Description                                                        |
| ---------- | ------------------------ | ---------------------------- | ------------------------------------------------------------------ |
| `model`    | `str`                    | (required, via parent)       | Model ID                                                           |
| `api_key`  | `str \| None`            | `AURIKO_API_KEY` env         | API key                                                            |
| `routing`  | `RoutingOptions \| None` | `None`                       | Routing configuration                                              |
| `base_url` | `str`                    | `"https://api.auriko.ai/v1"` | API base URL                                                       |
| `**kwargs` |                          |                              | Passed through to `ChatOpenAI` (e.g., `temperature`, `max_tokens`) |

## Configure routing

You can pass a `RoutingOptions` instance to control cost, latency, and quality trade-offs:

```python theme={null}
from auriko.frameworks.langchain import AurikoChatOpenAI
from auriko.route_types import RoutingOptions

llm = AurikoChatOpenAI(
    model="gpt-5.4",
    routing=RoutingOptions(optimize="cost", max_ttft_ms=1000),
)

response = llm.invoke("Hello!")
print(response.content)
```

Access routing metadata through `generation_info` when using `generate()`:

```python theme={null}
result = llm.generate([[HumanMessage(content="Hello!")]])
info = result.generations[0][0].generation_info
if info and "routing_metadata" in info:
    print(f"Provider: {info['routing_metadata']['provider']}")
```

## Configure manually

<Accordion title="Alternative: configure LangChain manually">
  If you prefer to use `ChatOpenAI` directly:

  ```python theme={null}
  import os
  from langchain_openai import ChatOpenAI

  llm = ChatOpenAI(
      model="gpt-5.4",
      api_key=os.environ["AURIKO_API_KEY"],
      base_url="https://api.auriko.ai/v1",
      use_responses_api=False,
  )
  ```

  `use_responses_api=False` is the default. Both Chat Completions and Response API streaming include `routing_metadata`.
</Accordion>

## Alternative: use `AurikoAsyncOpenAI` (experimental)

If you can't use `auriko[langchain]` (for example, your project pins a different `langchain-openai` version), pass `AurikoAsyncOpenAI` into LangChain's `async_client` parameter:

```python theme={null}
from langchain_openai import ChatOpenAI
from auriko import AurikoAsyncOpenAI

client = AurikoAsyncOpenAI()
llm = ChatOpenAI(
    model="gpt-4o",
    async_client=client.chat.completions,
    api_key="placeholder",
)
```

Pass `client.chat.completions` (not the whole client) and provide any string as `api_key` (LangChain requires it for construction). Read `client.last_routing_metadata` after each call. See [`AurikoAsyncOpenAI`](/sdk/python-reference#aurikoasyncopenai-experimental) for the full class reference.

## Notes

* OpenAI API errors map to typed Auriko error classes (`RateLimitError`, `PermissionDeniedError`, `BadRequestError`, etc.).
* `AurikoChatOpenAI` sets `use_responses_api=False` by default.
