AnyRouter

LangChain

Use LangChain with AnyRouter. Point ChatOpenAI at one endpoint to run chains and agents on free or paid models, in Python or TypeScript.

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
import os
from langchain_openai import ChatOpenAI

chat = ChatOpenAI(
    base_url="https://anyrouter.dev/api/v1",
    api_key=os.environ["ANYROUTER_API_KEY"],  # sk-ar-...
    model="anyrouter/free",
)

response = chat.invoke("What is a fallback route? Answer in one sentence.")
print(response.response_metadata.get("model_name"))  # the model that served it
print(response.content)

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

Verify

Export your key, then run the snippet. It prints the model that served the request, then the reply.

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

Every request also shows up in Logs with the model, provider, tokens, and latency.

Free models

Set the model to anyrouter/free and AnyRouter routes the request to a free model that is healthy right now — no credits used. Free models have no daily cap on any plan. Details on Free Tier.

Use anyrouter/auto to let AnyRouter pick from the full catalog, or any provider/model id from the catalog to pin one model.

Bring your own keys

Already pay for OpenAI, Anthropic, Google, or another provider? Add those keys to AnyRouter and your requests use them, with no credits deducted. See Get a provider API key and BYOK.

Troubleshooting

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

Use a full catalog id — anyrouter/free, anyrouter/auto, or a provider/model id such as z-ai/glm-4.7-flash — not a bare model name. Browse valid ids in the catalog.