AI Agent Integration
Hand a coding agent everything it needs to wire AnyRouter into an existing project — base URL, key, and model ids.
Paste this brief into a coding agent (Claude Code, Cursor, Codex, Aider, Hermes, …) when you want it to add AnyRouter to an existing codebase. AnyRouter is OpenAI-compatible, so the change is almost always the same: new base URL, new key, new model id.
Before you start
- An AnyRouter API key (prefix
sk-ar-). Create one in the dashboard. - Store the key in the project's normal secret manager. Do not commit it.
The contract
| Setting | Value |
|---|---|
| Base URL | https://anyrouter.dev/api/v1 |
| API key env var | ANYROUTER_API_KEY (prefix sk-ar-) |
| Default chat model | z-ai/glm-4.7-flash |
| Cheapest / fastest | openai/gpt-5.4-nano, anthropic/claude-haiku-4.5 |
| High-quality | anthropic/claude-sonnet-4.6, openai/gpt-5.4 |
| Main endpoint | POST /chat/completions (OpenAI-compatible) |
| Anthropic-native | POST /messages for anthropic/* models |
| Modern agent surface | POST /responses (typed output items + tools) |
Set it up
Paste this prompt into your agent
Integrate AnyRouter as the LLM provider for this project.
API: OpenAI-compatible
Base: https://anyrouter.dev/api/v1
Key: process.env.ANYROUTER_API_KEY (prefix sk-ar-)
Model: process.env.ANYROUTER_MODEL (fallback z-ai/glm-4.7-flash)
Rules:
1. Reuse the project's existing LLM client if one exists.
If not, add one small module that exports a configured OpenAI client.
2. Never hardcode the key.
3. Keep request/response shapes compatible with the current feature.
4. Preserve existing UI and behavior — only swap the LLM source.
5. If the project uses Anthropic Messages directly:
- keep `anthropic/*` calls on /messages, OR
- migrate them to /chat/completions for cross-provider routing.
6. Add a smoke test: one short prompt that prints the response.
Add the client module
The agent adds (or reuses) a small client, using the language snippet below.
Run the smoke test
Confirm the integration with the curl call under Verify.
Client module
Use whichever language matches your project. Streaming uses the same client — set stream: true and iterate.
npm install openai
import OpenAI from "openai"
export const anyrouter = new OpenAI({
baseURL: "https://anyrouter.dev/api/v1",
apiKey: process.env.ANYROUTER_API_KEY,
})
export const MODEL = process.env.ANYROUTER_MODEL ?? "z-ai/glm-4.7-flash"
export async function generate(prompt: string) {
const r = await anyrouter.chat.completions.create({
model: MODEL,
messages: [{ role: "user", content: prompt }],
})
return r.choices[0]?.message.content ?? ""
}
pip install openai
import os
from openai import OpenAI
client = OpenAI(
base_url="https://anyrouter.dev/api/v1",
api_key=os.environ["ANYROUTER_API_KEY"],
)
r = client.chat.completions.create(
model=os.getenv("ANYROUTER_MODEL", "z-ai/glm-4.7-flash"),
messages=[{"role": "user", "content": "Reply with: anyrouter-ok"}],
)
print(r.choices[0].message.content)
Verify
Run the smoke test. A reply of anyrouter-ok confirms the integration is live.
curl https://anyrouter.dev/api/v1/chat/completions \
-H "Authorization: Bearer $ANYROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "z-ai/glm-4.7-flash",
"messages": [{"role":"user","content":"Reply with: anyrouter-ok"}],
"max_tokens": 20
}'
The integration is done when:
- Calls hit
https://anyrouter.dev/api/v1 - The key comes from
ANYROUTER_API_KEY(no inline literal) - Model ids use the
provider/modelform - Streaming still streams if it streamed before
- Errors flow through the project's existing error path
Optional extras
Attribute usage to your app
Set attribution headers to identify your app in the dashboard.
new OpenAI({
baseURL: "https://anyrouter.dev/api/v1",
apiKey: process.env.ANYROUTER_API_KEY,
defaultHeaders: {
"X-AnyRouter-Title": "My App",
"X-AnyRouter-Source": "web-app",
"X-AnyRouter-Version": process.env.APP_VERSION ?? "dev",
},
})
Set routing preferences
Add a provider block to the request body for price caps, latency sort, or a fixed upstream order.
{
"provider": {
"sort": "latency",
"max_price": { "prompt": "2.00", "completion": "8.00" },
"allow_fallbacks": true
}
}
Related
- Quickstart — first request in 2 minutes
- Migrate from OpenAI — swap an existing OpenAI app
- Chat Completions API — the main endpoint
- Responses API — typed output items and tools
- App Attribution — how the attribution headers work