Skip to content

LangChain

Point LangChain's OpenAI integrations at AnyRouter to reach every model through one endpoint.

Run your LangChain chains and agents against any model in AnyRouter's catalog. Because AnyRouter's endpoint is OpenAI-compatible, you keep LangChain's standard OpenAI integration and change only the base URL, key, and model id.

Before you start

  • A LangChain project in Python or TypeScript.
  • An AnyRouter API key (prefix sk-ar-). Create one in the dashboard.

Set it up

Pick your language, install the OpenAI integration, then point the client at https://anyrouter.dev/api/v1 with your key and a model from the catalog.

pip install langchain-openai
from langchain_openai import ChatOpenAI

chat = ChatOpenAI(
    openai_api_base="https://anyrouter.dev/api/v1",
    openai_api_key="your-anyrouter-api-key",
    model="google/gemini-3.5-flash",
)

response = chat.invoke("What is a fallback route?")
print(response.content)
npm install @langchain/openai @langchain/core
import { ChatOpenAI } from "@langchain/openai"

const chat = new ChatOpenAI({
  configuration: {
    baseURL: "https://anyrouter.dev/api/v1",
  },
  openAIApiKey: process.env.ANYROUTER_API_KEY,
  modelName: "anthropic/claude-sonnet-4.6",
})

const response = await chat.invoke("Explain DNS load balancing.")
console.log(response.content)

Swap the model / modelName value to route the same chain to any model in the catalog — no other code changes.

Verify

Run the snippet above. A printed model reply confirms LangChain is talking to AnyRouter. If you set the key from an env var, export it first:

export ANYROUTER_API_KEY="sk-ar-your-key-here"

Troubleshooting

401 Unauthorized

Check that the key starts with sk-ar- and is passed as openai_api_key (Python) or openAIApiKey (TypeScript). Keys are created in the dashboard.

404 model not found

Use the full provider/model id (for example anthropic/claude-sonnet-4.6), not a bare model name. Browse valid ids in the catalog.