Install and create the provider
The provider is a thin wrapper over the AnyRouter API that implements the AI SDK's provider interface. Install it alongside the AI SDK itself, then call createAnyRouter() — it reads ANYROUTER_API_KEY from the environment when you omit the key.
npm install @anyr/ai-sdk-provider ai zodimport { createAnyRouter } from "@anyr/ai-sdk-provider"
import { generateText } from "ai"
const anyrouter = createAnyRouter()
const { text } = await generateText({
model: anyrouter("z-ai/glm-4.7-flash"),
prompt: "What is AnyRouter?",
})
console.log(text)Stream, and swap the model id
Every AI SDK function takes the same model factory, so streamText is the same call with a different helper. And because model ids are plain provider/model strings, changing which model runs is a one-string edit — no new client, no new key.
import { createAnyRouter } from "@anyr/ai-sdk-provider"
import { streamText } from "ai"
const anyrouter = createAnyRouter()
const { textStream } = await streamText({
// swap freely: "anthropic/claude-opus-4-5" or the free tier "anyrouter/free"
model: anyrouter("z-ai/glm-4.7-flash"),
prompt: "Write a haiku about the ocean.",
})
for await (const chunk of textStream) {
process.stdout.write(chunk)
}| Model id | What it routes to |
|---|---|
| z-ai/glm-4.7-flash | Z.AI GLM-4.7 Flash |
| anthropic/claude-opus-4-5 | Anthropic Claude Opus 4.5 |
| anyrouter/free | The free tier — no credits spent |
Structured output with generateObject
generateObject works the same way — pass a zod schema and the provider returns a typed object. This holds across models, so you can move a structured-output task to a cheaper or a stronger model without touching the schema.
import { createAnyRouter } from "@anyr/ai-sdk-provider"
import { generateObject } from "ai"
import { z } from "zod"
const anyrouter = createAnyRouter()
const { object } = await generateObject({
model: anyrouter("z-ai/glm-4.7-flash"),
schema: z.object({
title: z.string(),
summary: z.string(),
tags: z.array(z.string()),
}),
prompt: "Summarise the latest AI news in a structured format.",
})
console.log(object.title, object.tags)Not on the AI SDK? Use @anyr/sdk
If your app doesn't use the Vercel AI SDK, the plain TypeScript SDK talks to the same gateway with the same key. It exposes an OpenAI-shaped client — chat, embeddings, the Responses API, and the Anthropic Messages API — with typed requests and responses.
import { AnyRouter } from "@anyr/sdk"
// Reads ANYROUTER_API_KEY when omitted.
const client = new AnyRouter()
const completion = await client.chat.completions.create({
model: "z-ai/glm-4.7-flash",
messages: [{ role: "user", content: "Hello!" }],
})
console.log(completion.choices[0].message.content)Both packages read ANYROUTER_API_KEY — create one to get started.
Create a keyRelated posts
Run the Claude Agent SDK on any model
The Claude Agent SDK reads ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY, the same two variables Claude Code uses. Point them at AnyRouter and every catalog model is available to query() behind the Anthropic /messages dialect — in TypeScript or Python.
Use the OpenAI SDK with any model
The official openai package for Python and TypeScript doesn't have to mean OpenAI models. Override base_url and api_key, keep every other call the same, and address 170+ models across 28+ providers as provider/model strings — including OpenAI's own.
Free LLM API: 150+ models with a real free tier
You don't need a credit card to start building with large language models. A free LLM API should give you real models, an OpenAI-compatible endpoint, and a way to keep costs at zero as you grow — here's how that works in practice.
Route your first request in 2 minutes
Start free with your own keys, or top up and pay per token. Get $4/mo in credits and free models on Go — $2/mo, or free when you donate a provider key.
Start free