安装并创建 provider
这个 provider 是 AnyRouter API 上的薄封装,实现 AI SDK 的 provider 接口。和 AI SDK 一起安装,然后调用 createAnyRouter() — 省略 key 时它从环境读 ANYROUTER_API_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)流式,以及换模型 id
每个 AI SDK 函数吃同一套 model factory,所以 streamText 只是换 helper 的同一调用。模型 id 是普通 provider/model 字符串,换跑哪个模型只改一个字符串 — 不用新客户端,不用新 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)
}| 模型 id | 路由到 |
|---|---|
| z-ai/glm-4.7-flash | Z.AI GLM-4.7 Flash |
| anthropic/claude-opus-4-5 | Anthropic Claude Opus 4.5 |
| anyrouter/free | 免费档 — 不花额度 |
用 generateObject 做结构化输出
generateObject 同样 — 传 zod schema,provider 返回带类型的对象。跨模型都成立,所以你可以把结构化任务挪到更便宜或更强的模型,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)不用 AI SDK?用 @anyr/sdk
如果应用不用 Vercel AI SDK,纯 TypeScript SDK 用同一把 key 打同一网关。它暴露 OpenAI 形状的客户端 — chat、embeddings、Responses API、Anthropic Messages API — 请求和响应都有类型。
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)两个包都读 ANYROUTER_API_KEY — 创建一把就能开始。
创建 key相关文章
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.