LlamaIndex

Use LlamaIndex with AnyRouter through OpenAILike (Python) or the OpenAI LLM (TypeScript).

Run LlamaIndex indexes and agents on any AnyRouter model. You need an API key (sk-ar-...) from the dashboard.

Use OpenAILike; the plain OpenAI class rejects non-OpenAI model names.

pip install llama-index-llms-openai-like
import os
from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="anyrouter/free",
    api_base="https://anyrouter.dev/api/v1",
    api_key=os.environ["ANYROUTER_API_KEY"],
    is_chat_model=True,
    context_window=128000,  # the model's context length
)

print(llm.complete("Explain semantic search in one sentence."))

Requests appear in Logs.

Models

  • anyrouter/free: a healthy free model, no credits used. Needs the Go plan ($2/mo, or donate one working provider key). See Free Tier.
  • anyrouter/auto: AnyRouter picks from the full catalog.
  • Any provider/model id from the catalog.

Using your own provider keys? See BYOK.

Troubleshooting

SymptomFix
401 UnauthorizedThe key must start with sk-ar- and be passed as api_key (Python) or apiKey (TypeScript).
ValueError: Unknown modelUse OpenAILike, not llama_index.llms.openai.OpenAI.
404 model not foundPass a full id such as anyrouter/free or z-ai/glm-4.7-flash.

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