# AI Agent Integration

> Hand a coding agent the base URL, key and model ids it needs to wire AnyRouter into a project.


# Wire AnyRouter into a project with a coding agent

Paste the brief below into a coding agent (Claude Code, Cursor, Codex, Aider, Hermes) to add AnyRouter to an existing codebase. The change is a new base URL, key and model id.

| Setting | Value |
| --- | --- |
| Base URL | `https://anyrouter.dev/api/v1` |
| Key | `ANYROUTER_API_KEY` (prefix `sk-ar-`, from the [dashboard](https://anyrouter.dev/dashboard/keys)) |
| Default model | `z-ai/glm-4.7-flash` |
| Cheap / fast | `openai/gpt-5.4-nano`, `anthropic/claude-haiku-4-5` |
| High quality | `anthropic/claude-sonnet-4-6`, `openai/gpt-5.4` |
| Endpoints | `POST /chat/completions`, `POST /messages` (Anthropic-native), `POST /responses` |

## Steps

1. Paste this prompt into your agent:

   ```text
   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 existing LLM client; if none, 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. Only swap the LLM source; keep existing UI and behavior.
   5. Keep `anthropic/*` calls on /messages, or move them to /chat/completions.
   6. Add a smoke test: one short prompt that prints the response.
   ```

2. The client it adds looks like this (Python: `OpenAI(base_url=..., api_key=os.environ["ANYROUTER_API_KEY"])`):

   ```typescript
   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"
   ```

3. Verify. A reply of `anyrouter-ok` means it works:

   ```bash
   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}'
   ```

Done when calls hit the base URL above, the key comes from the env var, model ids use `provider/model`, and streaming still streams.

## Optional

Attribute usage to your app with headers `X-AnyRouter-Title`, `X-AnyRouter-Source` and `X-AnyRouter-Version` (OpenAI client: `defaultHeaders`).

Set routing preferences with a `provider` block in the request body:

```json
{ "provider": { "sort": "latency", "max_price": { "prompt": "2.00", "completion": "8.00" }, "allow_fallbacks": true } }
```


## Related

- [Quickstart](/docs/getting-started/quickstart.md)
- [Migrate from OpenAI](/docs/guides/migrate-from-openai.md)
- [Chat Completions API](/docs/api-reference/chat-completions.md)
- [Responses API](/docs/api-reference/responses.md)
- [App Attribution](/docs/features/app-attribution.md)
