LangChain

Use LangChain with AnyRouter by pointing ChatOpenAI at one endpoint, in Python or TypeScript.

AnyRouter is OpenAI-compatible, so keep LangChain's OpenAI integration and change the base URL, key and model id. Create an sk-ar- key in the dashboard and export it as ANYROUTER_API_KEY.

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"],
    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)

Requests also appear in Logs.

Model ids

  • anyrouter/free routes to a healthy free model, no credits. Needs Go ($2/mo, or free when you donate one working provider key to the shared Pool); no daily cap. See Free Tier.
  • anyrouter/auto picks from the full catalog.
  • Any provider/model id from the catalog pins one model.

Own provider keys (BYOK) deduct no credits: see Get a provider API key and BYOK.

Troubleshooting

SymptomFix
401Key must start with sk-ar- and be passed as api_key (Python) or apiKey (TypeScript).
404 model not foundUse a full id such as anyrouter/free or z-ai/glm-4.7-flash, not a bare model name.

AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer