All guides

Set Up LangChain & LangGraph with MonoRouter

Route your LangChain agents and LangGraph workflows through your Claude Pro or Max subscription. Two env vars, zero code changes.


What you need

  • A Claude Pro or Max subscription at claude.ai
  • Python 3.9+ with pip
  • A MonoRouter account — sign up at /signup
  • A MonoRouter API key (mrk_*) — get one at /keys
  • Your Claude subscription connected to MonoRouter — go to /tokens, click [ + add token ], and run the auto-connect script

Sign up for MonoRouter

Open /signup, enter your email and set a password. Save the six recovery codes — they're your offline fallback if you lose your password; you can also always reset it by email at /forgot-password.

Connect your Claude subscription

From the dashboard, go to /tokens, click [ + add token ], and pick ANTHROPIC. Use the auto-connect tab — it's the fastest.

The modal shows a one-liner like this:

bash
curl -sSL https://api.monorouter.dev/connect.sh | bash -s -- mrc_xxxxxxxxxxxx

Paste it into a terminal. The script runs claude setup-token, which opens a browser window for authorization and creates a long-lived token. When the dashboard flips to connected, you're done.

Install the packages

bash
pip install langchain-anthropic langgraph

langchain-anthropic is the official LangChain integration for Claude. langgraph is optional — install it if you're building stateful agents with tool use.

Set environment variables

Add these to your shell config or .env file:

bash
export ANTHROPIC_BASE_URL="https://api.monorouter.dev"
export ANTHROPIC_API_KEY="mrk_your_key_here"
The langchain-anthropic package reads these environment variables automatically. No code changes needed.

Basic LangChain usage

Simple completion through MonoRouter:

python
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(model="claude-sonnet-4-6")

response = llm.invoke("What is MonoRouter?")
print(response.content)

The ChatAnthropic class picks up ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY from the environment. You can also pass them explicitly:

python
llm = ChatAnthropic(
    model="claude-sonnet-4-6",
    anthropic_api_url="https://api.monorouter.dev",
    anthropic_api_key="mrk_your_key_here",
)

LangGraph agent with tools

Build a tool-calling agent that routes through MonoRouter:

python
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"It's sunny in {city}!"

llm = ChatAnthropic(model="claude-sonnet-4-6")
agent = create_react_agent(llm, tools=[get_weather])

result = agent.invoke({
    "messages": [{"role": "user", "content": "What's the weather in Tokyo?"}]
})

for msg in result["messages"]:
    print(msg.content)

Every LLM call the agent makes routes through MonoRouter automatically. You can see each call on your /dashboard.

Streaming

MonoRouter supports streaming out of the box:

python
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(model="claude-sonnet-4-6", streaming=True)

for chunk in llm.stream("Explain quantum computing in one paragraph"):
    print(chunk.content, end="", flush=True)

Available models

Use any of these model IDs with ChatAnthropic:

ModelIDContextMax output
Fable 5claude-fable-51M128k
Opus 4.8claude-opus-4-81M128k
Opus 4.7claude-opus-4-71M128k
Opus 4.6claude-opus-4-61M128k
Sonnet 5claude-sonnet-51M128k
Sonnet 4.6claude-sonnet-4-61M128k
Haiku 4.5claude-haiku-4-5-20251001200k64k

Pricing

  • 1,500 successful calls are free for the lifetime of your account. Failed calls don't count.
  • After that, paid plans start at $9/month (Hobby, 10,000 calls). Pro is $29/month for unlimited personal use; Team is $99/month for 5 pooled seats. See /billing for the payment flow.

Ready to get started?

Sign up in 30 seconds — no credit card required. 1,500 free API calls included.

guide: set up LangChain with MonoRouter · monorouter