An AI API Gateway for multi-upstream routing and operations
OpenAI / Anthropic-compatible proxy · capability routing · load balancing · circuit breaking & failover · per-request billing · multi-tenant console
The full documentation site is maintained in Simplified Chinese. This English README covers the high-level overview, screenshots, and the minimal Docker quick start; for deployment, configuration, and architecture details follow the doc-site links below or use your browser's translation.
AutoRouter is a Next.js 16 (App Router) fullstack application: the frontend is an internationalized admin console, the backend is a set of Next.js API Routes. It exposes an OpenAI / Anthropic-compatible proxy at /api/proxy/v1/* that fans incoming traffic out to multiple upstreams by capability-based routing, layering load balancing, circuit breaking, quota & concurrency control, failover, session affinity, and per-request billing onto that path.
In one line: collapse scattered model upstreams into a single gateway that is governable, observable, and billable.
|
|
|
|
The lifecycle of a single proxy request inside the gateway:
flowchart LR
C(["Client<br/>API Key"]) --> V{"Verify key<br/>detect capability"}
V --> R["Build candidate pool<br/>capability · auth · model_redirects"]
R --> LB["Load balance<br/>priority · weight"]
LB --> CB{"Circuit breaker<br/>CLOSED / OPEN / HALF_OPEN"}
CB -->|"pass"| U[("Upstream forward<br/>SSE streaming")]
CB -.->|"OPEN / fail"| FO["Failover<br/>next candidate"]
FO --> U
U --> B["Log + tokens<br/>write billing snapshot"]
B --> C
- Capability routing — detect capability (chat / responses / messages, …) from path and model, resolve the provider and candidate upstreams.
- Admission control — concurrency, quota, and queue admission before forwarding; session affinity reuses the previously selected upstream when it hits.
- Resilience — timeouts or 5xx trigger failover with each attempt logged; the breaker maintains a CLOSED / OPEN / HALF_OPEN state machine per upstream.
- Billing loop — both success and failure are logged; successful requests additionally compose cost and persist a billing snapshot.
For the full request lifecycle, upstream model, and circuit-breaker details, see the Architecture guide on the docs site.
The console uses the Ops Console visual system: a dark-first persona, amber accent, terminal/operations aesthetics, LED status lights, and circuit-breaker chips.
| Dashboard | Upstreams |
|---|---|
![]() |
![]() |
| Logs | API Keys |
![]() |
![]() |
Docker Compose is the easiest way to start (ships a PostgreSQL service):
git clone https://github.com/g1331/AutoRouter.git
cd AutoRouter
cp .env.example .env
# Edit .env: at minimum set ADMIN_TOKEN and ENCRYPTION_KEY
docker compose up -d
# Visit http://localhost:3331 by defaultGenerate
ENCRYPTION_KEY(44-char base64):node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"
Requirements: Node.js 22+ (for source builds), PostgreSQL 16 (default, recommended for production); SQLite is available as a local dev sandbox. The host port 3331 maps to container port 3000 by default.
For deployment topologies, the release workflow, personal-deployment secrets, source-based local development, and SQLite switching, see the Deployment Guide on the docs site (Simplified Chinese; browser translation works for the prose, code blocks stay in English).
Environment variables are validated by a Zod schema in src/lib/utils/config.ts. The minimal set:
| Variable | Required | Description |
|---|---|---|
DATABASE_URL |
▲ | PostgreSQL connection string (required with PG; auto-detects SQLite if unset) |
ENCRYPTION_KEY |
● | Fernet root for upstream secrets (44-char base64, 32 bytes) |
ADMIN_TOKEN |
● | Admin API authentication token |
DB_TYPE |
postgres | sqlite; inferred from DATABASE_URL when unset |
|
JWT_SECRET |
HS256 key for user-login JWTs; derived from ENCRYPTION_KEY via HKDF when unset |
|
ALLOW_KEY_REVEAL |
Whether the Admin API may reveal plaintext keys; default false |
|
RECORDER_ENABLED |
Enable traffic recording; off by default (compose/deploy may enable it) |
Full list in .env.example and the Environment variable reference.
| Topic | Link |
|---|---|
| Deployment topologies & quickstart | guide/deployment |
| Environment variable reference | guide/deployment/env-reference |
| GitHub Actions deployment | guide/deployment/github-actions |
| Admin console usage | guide/usage |
| Architecture & request lifecycle | guide/architecture |
| Testing strategy & contributing | guide/architecture/testing · contributing |
Contributor-facing development commands, project structure, and working conventions live in AGENTS.md at the repo root.
AGPL-3.0 © 2025 AutoRouter Contributors
If this project helps you, please consider giving it a Star ⭐










