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-likeimport 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."))npm install llamaindex @llamaindex/openaiimport { OpenAI } from "@llamaindex/openai"
const llm = new OpenAI({
model: "anyrouter/free",
apiKey: process.env.ANYROUTER_API_KEY,
baseURL: "https://anyrouter.dev/api/v1",
})
const response = await llm.complete({ prompt: "Explain vectors in one sentence." })
console.log(response.text)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/modelid from the catalog.
Using your own provider keys? See BYOK.
Troubleshooting
| Symptom | Fix |
|---|---|
401 Unauthorized | The key must start with sk-ar- and be passed as api_key (Python) or apiKey (TypeScript). |
ValueError: Unknown model | Use OpenAILike, not llama_index.llms.openai.OpenAI. |
404 model not found | Pass 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