---
title: "LangChain + rawroute: 50–70% Cheaper Agent API"
canonical: "https://rawroute.dev/integrations/langchain"
lang: "en"
last_verified: "2026-11-28"
description: "Point LangChain ChatOpenAI or ChatAnthropic to https://api.rawroute.dev for 50–70% lower LangGraph agent spend, prompt caching, and 4-provider failover."
---

# Use rawroute with LangChain.

LangChain and LangGraph agents execute dozens of LLM steps per task, multiplying token spend and 429/529 exposure. Pointing ChatOpenAI to https://api.rawroute.dev/v1 or ChatAnthropic to https://api.rawroute.dev cuts token costs 50–70% below official rates while preserving structured outputs, tool calling, and streaming.

## Setup Steps

1. Install langchain-openai or langchain-anthropic as usual.
2. Pass base_url="https://api.rawroute.dev/v1" (OpenAI) or anthropic_api_url="https://api.rawroute.dev" (Anthropic).
3. Keep all existing LangGraph chains, .with_structured_output(), and tool bindings unchanged.

### Python · ChatOpenAI & ChatAnthropic

```python
import os
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic

gpt = ChatOpenAI(
    model="gpt-6.2-astra",
    base_url="https://api.rawroute.dev/v1",
    api_key=os.environ["RAWROUTE_API_KEY"],
    max_retries=3,
)

claude = ChatAnthropic(
    model="claude-sonnet-5-5",
    anthropic_api_url="https://api.rawroute.dev",
    api_key=os.environ["RAWROUTE_API_KEY"],
)
```

## When NOT to use rawroute

- Keep LangSmith tracing enabled if you want step-by-step LangGraph visual traces alongside rawroute billing logs.
