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

# LangChain framework integration

> Connect Langchain to the AI Gateway for enhanced LLM orchestration. Use Toqan Router as a drop-in provider for chains, agents, and RAG applications.

> [card] [AI Gateway](#ai-gateway)
>
> Route your LLM calls through the AI Gateway with a single base URL change. Zero vendor lock-in: always run on the best model at the lowest cost for your use case.

> [card] [Observability](#observability)
>
> Use OpenInference and OpenTelemetry to capture LangChain and LangGraph traces.

## AI Gateway

### Overview

LangChain is a framework for building LLM-powered applications through composable chains, agents, and integrations with external data sources. By connecting LangChain to Toqan Router's AI Gateway, you access 500+ models through a single base URL change.

### Key Benefits

Toqan Router's AI Gateway enhances your LangChain applications with:

> [card] Complete Observability
>
> Track every chain step, tool use, and LLM call with detailed traces

> [card] Built-in Reliability
>
> Automatic fallbacks, retries, and load balancing for production resilience

> [card] Cost Optimization
>
> Real-time cost tracking and spend management across all your AI operations

> [card] Multi-Provider Access
>
> Access 500+ LLMs and 30+ providers through a single, unified integration

### Prerequisites

Before integrating LangChain with Toqan Router, ensure you have:

* A Toqan Router account and API Key
* Python 3.8 or higher

> [!NOTE]
>
> To set up your API key, see API keys & Endpoints.

### Installation

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install langchain langchain-openai
```

### Configuration

Configure LangChain to use Toqan Router's AI Gateway via `ChatOpenAI` with a custom `base_url`:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from langchain_openai import ChatOpenAI
import os

llm = ChatOpenAI(
    model="openai/gpt-5.6-sol",
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)
```

> **base\_url**: `https://router.toqan.ai/eu/v3/router`

### Basic Example

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from langchain_openai import ChatOpenAI
import os

llm = ChatOpenAI(
    model="openai/gpt-5.6-sol",
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

result = llm.invoke("Explain quantum computing in simple terms.")
print(result.content)
```

### Chains

Build composable chains using LangChain's pipe operator:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
import os

llm = ChatOpenAI(
    model="openai/gpt-5.6-sol",
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("user", "{input}"),
])

chain = prompt | llm
result = chain.invoke({"input": "Tell me a joke about programming."})
print(result.content)
```

### Streaming

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from langchain_openai import ChatOpenAI
import os

llm = ChatOpenAI(
    model="openai/gpt-5.6-sol",
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

for chunk in llm.stream("Write a short poem about the ocean."):
    print(chunk.content, end="", flush=True)
print()
```

### Model Selection

With Toqan Router, you can use any supported model from 30+ providers:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from langchain_openai import ChatOpenAI
import os

# Use Claude
claude = ChatOpenAI(
    model="anthropic/claude-sonnet-5",
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

# Use Gemini
gemini = ChatOpenAI(
    model="google-ai/gemini-2.5-flash",
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

# Use Groq
groq = ChatOpenAI(
    model="groq/qwen/qwen3.8-27b",
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)
```

## Observability

Model calls through the AI Gateway appear in [Traces](/docs/ai-studio/observability/traces.md). To capture the surrounding application workflow, use OpenTelemetry with a framework instrumentor and the regional OTLP endpoint.

For Python, install `openinference-instrumentation-langchain` and follow the [OpenInference LangChain setup](https://arize-ai.github.io/openinference/python/instrumentation/openinference-instrumentation-langchain/). It supports LangChain and LangGraph. For Node.js, use [OpenInference LangChain.js](https://arize-ai.github.io/openinference/js/packages/openinference-instrumentation-langchain/).

Configure the exporter using the [Observability quickstart](/docs/ai-studio/observability/quickstart.md):

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
export OTEL_EXPORTER_OTLP_ENDPOINT="https://router.toqan.ai/eu/v2/otel"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer $TOQAN_API_KEY"
```

Use the provider API directly when exporting framework spans for the same model calls, or choose which layer to trace so one call is not recorded twice.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.