songquanpeng/one-api
Self-hosted OpenAI-compatible gateway for LLM routing and API key management
Automated SPDX check for reference only — not legal advice. Disclaimer
GitHub Star History
Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.
Evidence-Backed Technical Brief
AI Deep ReviewArchitecture & Core Mechanics
One API is a self-hosted relay, not an inference engine. The service starts at main.go, targets Go 1.20 in go.mod, and depends on Gin; the JavaScript frontend is built from web/default before compiling the single executable. The tree separates router/, middleware/, controller/, model/, and relay/, while the dependency manifest includes a Redis client and the AWS Bedrock Runtime SDK.
Clients authenticate to the gateway with a One API token and request a model through an OpenAI-compatible endpoint. The gateway selects an eligible upstream channel and uses that channel's credentials. For compatible upstreams it relays the request; for other providers it adapts the request and response bodies. The client-facing protocol stays consistent, but the available operations and behavior still depend on the selected provider adapter.
Key Capabilities & Differentiators
One API lets existing OpenAI-compatible clients use configured channels for OpenAI and Azure OpenAI, Anthropic Claude including AWS-backed access, Google Gemini, DeepSeek, and Ollama, among other providers. The gateway supports streaming, and default load balancing spreads requests across eligible channels. An explicit channel override is available with administrator-created tokens. Operators can also schedule channel availability checks and balance updates.
The web console separates upstream credential management from downstream token issuance. Token limits are distinct from account limits. Quota accounting weights prompt tokens plus completion tokens adjusted by a completion multiplier, then applies model and group ratios. This is a gateway usage policy, not a substitute for reconciling upstream invoices. Model names and provider-specific behavior still need testing on the intended route.
Trade-offs & Limitations
Compared with direct provider SDKs, One API reduces duplicate client integrations and centralizes who can use which upstream credentials. In return, it adds a network hop, a shared failure point, and a compatibility layer that may not expose native features as soon as a provider SDK does. Load balancing distributes traffic; it does not remove upstream rate limits. Unlike Ollama, the gateway does not run model weights or make a cloud-backed setup work offline.
Start with SQLite for a small single instance and prefer MySQL for higher concurrency or multiple nodes. Redis is useful when database access is slow, but caching and batched writes can delay the visibility of configuration or quota changes. Benchmark with the expected request mix and stream concurrency rather than sizing for model parameters. Because traffic and upstream credentials pass through the gateway, restrict admin access and define a log retention policy before production use.
Hardware & Deployment Guide
Start with a release binary or a Docker image from justsong/one-api or ghcr.io/songquanpeng/one-api. The default port is 3000; mount /data for SQLite persistence and set --log-dir if logs need a separate path. The supplied Compose example uses MySQL via SQL_DSN, Redis via REDIS_CONN_STRING, and a health check against /api/status. Before exposing the service, replace the initial administrator password and sample secrets, and terminate HTTPS at a reverse proxy configured for streaming requests.
Use shared MySQL and the same strong SESSION_SECRET on every node in the documented multi-node setup. Mark secondary nodes with NODE_TYPE=slave and tune SYNC_FREQUENCY if caching is enabled. PostgreSQL is listed as still being adapted, so verify it before treating it as a production substitute. Pin and test the release you deploy: v0.6.10 (2025-02-02) added Replicate chat models, OpenAI o1, and gemini-2.0-flash-exp; v0.6.9 added SiliconFlow and updated Ollama embeddings to accept multiple input texts.
LOG_SQL_DSN can separate log storage.gemini-2.0-flash-exp; v0.6.9 updated Ollama embeddings for multiple input texts.Teams with several OpenAI-compatible clients that need to share approved upstream providers without distributing provider credentials to each application.
You need local inference, immediate access to provider-native APIs, or only one provider and no central token, quota, or routing requirements.
Quick Start & Installation
# 使用 SQLite 的部署命令: docker run --name one-api -d --restart always -p 3000:3000 -e TZ=Asia/Shanghai -v /home/ubuntu/data/one-api:/data justsong/one-api # 使用 MySQL 的部署命令,在上面的基础上添加 `-e SQL_DSN="root:123456@tcp(localhost:3306)/oneapi"`,请自行修改数据库连接参数,不清楚如何修改请参见下面环境变量一节。 # 例如: docker run --name one-api -d --restart always -p 3000:3000 -e SQL_DSN="root:123456@tcp(localhost:3306)/oneapi" -e TZ=Asia/Shanghai -v /home/ubuntu/data/one-api:/data justsong/one-api
Comparison vs. Top LLMs & Inference Alternatives
Full comparison →| Repository | Stars | 7d Trend | Stack | License | Core Positioning |
|---|---|---|---|---|---|
| one-api ★ | 37.5k | +56 | JavaScript | MIT | Self-hosted OpenAI-compatible gateway for LLM routing and API key management |
| new-api | 53k | +343 | Go | AGPL-3.0 | Self-hosted Go API gateway for multi-provider model routing, access, and costs |
| gpt-load | 7.2k | +6 | Go | MIT | Self-hosted Go gateway for routing LLM traffic across providers and credentials |
| bifrost | 10.2k | +125 | Go | Apache-2.0 | Self-hosted Go gateway for routing and governing multi-provider LLM traffic |
AI Context Briefing (for Claude / Cursor / Windsurf)
Standardized architecture summary & quickstart commands ready to paste into coding agents.
curl -sL https://repoforai.com/repo/songquanpeng/one-api.md
Active README embeds unlock Hot-Tier 6h Telemetry Refresh automatically (zero pay-to-rank).
Frequently Asked Questions about one-api
How do I connect an existing OpenAI-compatible client?
/v1 if the SDK expects it. If routing returns no available channel, check the user's group, the channel's group, and the configured model name.Does the gateway need a GPU, and what drives resource usage?
What should a commercial self-hosted deployment check?
When should I bypass One API and connect directly?
Upstream README & Documentation
This profile focuses on independent engineering analysis and live telemetry. Read the full upstream README, changelog, and examples directly on GitHub.
Top 10 Breakout Open-Source AI Repos, Every Thursday
Signal-only digest of fast-growing AI repos, SPDX license alerts, and architecture picks. Zero spam.
Zero spam · 1-click unsubscribe · Privacy Policy