API Overview
Base URL, API key families, common headers, error codes and rate-limit headers for the AnyRouter API.
Call any AnyRouter model by overriding the base URL and API key in OpenAI-compatible tooling, or use Messages for Anthropic-compatible tooling. The full OpenAPI spec, with a try-it runner, is at /docs/api.
Base URL
https://anyrouter.dev/api/v1OpenAI-compatible clients that set the base URL to https://anyrouter.dev (and append /v1/... themselves) also work: /v1/models and /v1/chat/completions are the same API as /api/v1/....
Endpoints are versioned under /v1. Breaking changes land on /v2; /v1 stays available for a 12-month deprecation window, announced with a Deprecation response header. New fields and endpoints ship without notice.
Authentication: which key for which endpoint
AnyRouter has two API key families.
| Key family | Prefix | Use for | Get one at |
|---|---|---|---|
| LLM API key | sk-ar-v1- | Inference: /chat/completions, /messages, /responses, /embeddings, /rerank, /moderations, /decisions (/systemone is the same handler). Also reads /models, /generation/:id, /auth/key. | /dashboard/keys |
| Management API key | ak_ | Account administration: /keys (LLM-key CRUD), /presets, /auth/byok, /credits, /management-keys. | /dashboard/managed-access |
The families do not cross over. An ak_ key on /chat/completions returns 401; an sk-ar-v1- key on /api/v1/keys returns 401 with a hint pointing here.
See Management API Keys for scopes and lifecycle.
Common headers
| Header | Required | Description |
|---|---|---|
Authorization: Bearer <key> | Yes | LLM key (sk-ar-…) on inference routes; management key (ak_…) on account routes. |
Content-Type: application/json | POST | Required on POST endpoints. |
x-api-key | Messages | Anthropic-compatible auth header for /messages (accepts sk-ar-…). |
anthropic-version | Messages | Required by Anthropic-compatible Messages clients. |
X-AnyRouter-Provider | No | Force routing to a specific upstream provider. |
X-AnyRouter-Balance-After-Debit: true | No | Ask for this request's charge and the workspace balance after it. The response waits up to 500 ms for the charge, so leave it off unless you read the values. |
X-AnyRouter-Trace-Id | Response | Canonical trace id on every inference response. Paste it into the dashboard logs "Request ID" filter. |
X-Request-ID | Response | Legacy alias of X-AnyRouter-Trace-Id, same value. |
X-AnyRouter-Model | Response | Public catalog model id that served the request (including the resolved model for a virtual preset). |
X-AnyRouter-Upstream-Model | Response | Wire model name sent to the selected upstream. |
X-AnyRouter-Provider-Chain | Response | Ordered backend ids tried, joined with ->; contains route ids only, never credentials. |
X-AnyRouter-Credential-Source | Response | platform, own_byok, or pool for the winning attempt. This is a class label, not key material. |
X-AnyRouter-Requested-Model / X-AnyRouter-Fallback-Reason | Response, on model fallback | Sent on /chat/completions when a different model served the request (for example through models[]). The reason is one of rate_limited, timeout, model_unavailable, credential_error, upstream_error, or not_attempted. The body carries the same facts as requested_model plus fallback: { used, reason, attempts[] }, on the non-streaming response and, for streams, on the last chunk, next to anyrouter_metadata (and usage, when you set stream_options.include_usage). The headers are authoritative. requested_model is the resolved canonical id, not the raw @preset/... string (for a preset, the first branch's model). anyrouter/* routers never get these. |
X-AnyRouter-Credits-Balance-At-Admission | Response, when read | Workspace balance observed during paid-inference admission, before the asynchronous debit. It is omitted when the balance gate is skipped (free/BYOK-only). |
X-AnyRouter-Credits-Balance-After-Debit | Response, non-streaming, on request | Workspace balance right after this request's own charge was applied, on /chat/completions, /messages, and /responses. Sent only when the request included X-AnyRouter-Balance-After-Debit: true. Streaming responses carry it as anyrouter_metadata.balanceAfterDebit in the final metadata event. Omitted for free and BYOK requests, for a retried request already charged, and when the charge has not settled by the time the response is written (use GET /credits then). Concurrent requests can lower the balance further. |
X-AnyRouter-Credits-Charged | Response, non-streaming, on request | Credits this request was charged, from the billing ledger (includes retries, fallbacks and cache pricing). Sent on /chat/completions, /messages, and /responses only when the request included X-AnyRouter-Balance-After-Debit: true. 0 for free and BYOK requests. Streaming responses carry it as anyrouter_metadata.creditsCharged in the final metadata event. It is omitted for a retried request that was already charged and whenever the charge has not settled by the time the response is written. |
X-AnyRouter-Credits-Balance | GET /credits response | Current authoritative workspace balance returned by the credits read endpoint. |
X-RateLimit-* / RateLimit-* | Response | The per-minute limit and policy for the tier the request hit (free models are capped per account by plan: 150 per minute on Go, 60 on Go from a donated key, 60 on Free after a $5 top-up, 400 on Pro, Pro+ and Max, reported as free-go, free-go-donation, free-topup or free-pro; see Rate Limits). No remaining count or reset is sent. |
Server-Timing | Response | Latency split in ms on /chat/completions, /messages and /responses: gw is AnyRouter's work before the first upstream call (auth, routing, admission), upstream is time inside upstream attempts (repeated per attempt with a desc naming the backend), post is AnyRouter's work after the upstream call, total is the sum. For a stream the header leaves with the first byte, so post covers only what happened before it; the full split arrives as anyrouter_metadata.timings in the final metadata frame. |
Endpoint summary
| Method | Path | Description |
|---|---|---|
| POST | /chat/completions | Chat completions (OpenAI-compatible) |
| POST | /ocr/extract | OCR — extract text as Markdown |
| POST | /messages | Anthropic Messages passthrough |
| POST | /responses | Responses for structured output and tool workflows |
| POST | /embeddings | Embeddings for search and clustering |
| POST | /rerank | Rerank documents by relevance to a query |
| POST | /documents/parse | Document parse — file to Markdown, your own Nace key |
| POST | /moderations | Moderations — classify text for harmful content |
| POST | /decisions | Decisions TypeSafe decisions (not chat). Preferred path. |
| POST | /systemone | Same handler as /decisions |
| GET | /models | List models |
| GET | /models/:id | Model metadata |
| GET | /models/count | Total model count |
| GET | /generation/:id | Per-request stats (tokens, cost, latency) |
| GET | /providers | List upstream providers |
| GET | /credits | Remaining credit balance |
| GET | /auth/key | Current API key info |
| GET | /health | Gateway + provider status |
Error shape
All errors follow a consistent JSON envelope:
{
"error": {
"code": "payment_required",
"message": "Upstream provider \"cloudflare\" returned 402 — insufficient quota",
"metadata": {
"type": "upstream_error",
"upstream_backend": "cloudflare",
"upstream_status": 402,
"upstream_message": "You exceeded your current quota, please check your plan and billing details."
}
}
}error.code is a stable machine-readable slug. The canonical set:
| HTTP | error.code | Notes |
|---|---|---|
| 400 | bad_request / max_tokens_too_small | Malformed body or invalid parameters. max_tokens_too_small: a reasoning model spent the whole output budget on reasoning and produced no content — raise max_tokens (max_output_tokens on /responses) to 256 or more, or set stream: true. |
| 401 | unauthorized | Missing or invalid API key. |
| 402 | insufficient_balance | Out of credits on a paid model. Top up at Credits. Upstream BYOK billing failures may also surface as 402 with metadata.type: "upstream_error" — read metadata.upstream_message. |
| 403 | forbidden | Key lacks permission for the resource. |
| 404 | not_found | Unknown model or missing resource. |
| 408 | request_timeout | Upstream did not respond in time. |
| 413 | content_too_large | Request body exceeds the configured limit. |
| 422 | unprocessable_entity | Validation failed semantically. |
| 429 | rate_limit_exceeded / ip_rate_limit_exceeded | Rate limit hit. Honor Retry-After. No X-RateLimit-Remaining / X-RateLimit-Reset is sent. |
| 500 | internal_server_error | Unexpected AnyRouter failure. |
| 502 | bad_gateway / upstream_exhausted | Upstream 5xx or every fallback failed; see metadata.upstream_attempts. |
| 503 | service_unavailable | Upstream temporarily unavailable. |
When a BYOK call is rejected, error.metadata.upstream_message holds the provider's verbatim message and is the authoritative reason. See Errors for retry recipes.
Rate limits
Rate limits are enforced per-key. Every response carries at least:
X-RateLimit-Limit: <requests per minute>
X-RateLimit-Window: 60
X-RateLimit-Tier: <tier name>
RateLimit-Limit: <same as X-RateLimit-Limit>
RateLimit-Policy: <limit>;w=60The per-minute tier does not emit X-RateLimit-Remaining or X-RateLimit-Reset — the edge limiter only reports the cap. See Rate Limits.
Tracing
POST /chat/completions and POST /responses also accept an optional request-body trace object:
{
"trace": {
"trace_id": "4f8c2b9670b44b49a2e71e7fd5c0b1d3",
"trace_name": "checkout-flow",
"span_name": "summarize-order"
}
}AnyRouter uses it to correlate request steps. Without a trace object you still get an X-Request-ID header for support.
AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer