LlamaIndex
Point LlamaIndex's OpenAI LLM class at AnyRouter to power RAG, search, and agent loops with any model.
Drive your LlamaIndex indexes and agents with any model in AnyRouter's catalog. LlamaIndex's OpenAI LLM class works unchanged — you only point api_base at AnyRouter and pass your key.
Before you start
- A LlamaIndex 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 LLM package, then configure the base URL, your key, and a model from the catalog. Use the LLM in your index querying or agent loops.
pip install llama-index-llms-openai
from llama_index.llms.openai import OpenAI
llm = OpenAI(
api_base="https://anyrouter.dev/api/v1",
api_key="your-anyrouter-api-key",
model="z-ai/glm-4.7-flash",
)
response = llm.complete("Explain semantic search in one sentence.")
print(response)
npm install llamaindex
import { OpenAI } from "llamaindex"
const llm = new OpenAI({
additionalSessionOptions: {
baseURL: "https://anyrouter.dev/api/v1",
},
apiKey: process.env.ANYROUTER_API_KEY,
model: "google/gemini-3.5-flash",
})
const response = await llm.complete({
prompt: "Explain vectors in machine learning.",
})
console.log(response.text)
Verify
Run the snippet above. A printed completion confirms LlamaIndex is routing through AnyRouter. Export your key first if you read it from the environment:
export ANYROUTER_API_KEY="sk-ar-your-key-here"
Troubleshooting
401 Unauthorized
Confirm the key starts with sk-ar- and is passed as api_key (Python) or apiKey (TypeScript). Manage keys in the dashboard.
404 model not found
Pass the full provider/model id (for example z-ai/glm-4.7-flash). Valid ids live in the catalog.
Related
- LangChain — chains and agents
- Vercel AI SDK — TypeScript-native framework
- Chat Completions API — the underlying endpoint
- Models catalog — every model id you can pass