ai-gateway

module
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: MIT

README

My Blog: https://vaala.cat/posts/vibe-ai-gateway-oss/

AI Gateway

A distributed-by-design AI API gateway with a separated control-plane (master) / data-plane (agent) architecture. Provides OpenAI/Claude-compatible /v1/* relay endpoints, built-in management APIs, Web UI, and single-binary distributed deployment.

中文文档

Features

  • Control Plane Management — Users (groups), tokens, channels, models, and agents
  • Data Plane Relay — OpenAI/Claude-compatible API endpoints (/v1/chat/completions, /v1/responses, /v1/messages, etc.) with automatic cross-protocol conversion
  • Real-Time Config Sync — Master/agent incremental sync over WebSocket; lightweight distributed deployment with zero external dependencies
  • Multi-Region Routing — Route requests from region A to agents in region B, enabling cross-region load balancing and bypassing regional restrictions
  • Quota & Billing — Usage-based settlement and quota enforcement
  • Model Routing — Aggregate multiple upstream models under one name with priority/weight load balancing and error retries
  • BYOK (Bring Your Own Key) — End-users can self-serve upload their own provider API keys (AES-GCM encrypted at rest); private channels are merged into the candidate pool with priority over shared admin channels, with optional service-fee billing mode
  • Single Binary — Frontend static assets embedded; no separate web server needed

Screenshots

DashboardChannels
Channels — upstream provider configurationModels
Models — per-model pricingModel Routings
Model Routings — priority/weight aggregationUsage Logs
Usage Logs — per-request audit trailBilling
Billing — daily rollups by token and channelPlayground
Playground — in-browser chat tester

See all 20 screenshots →

Architecture

┌─────────────────────────────────────────────────────┐
│                   master (control plane)             │
│  ┌──────────┐  ┌──────────┐  ┌───────────────────┐ │
│  │ Admin API│  │  Web UI  │  │ Agent Sync Hub    │ │
│  │ & Auth   │  │ (embed)  │  │ (WebSocket)       │ │
│  └──────────┘  └──────────┘  └───────────────────┘ │
│  ┌──────────────────────────────────────────────┐   │
│  │         Billing & Quota Settlement           │   │
│  └──────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────┘
          │ WebSocket sync
          ▼
┌─────────────────────────────────────────────────────┐
│                   agent (data plane)                 │
│  ┌──────────────┐  ┌────────────┐  ┌────────────┐  │
│  │ /v1/* Relay  │  │ Token/Chan │  │  Usage     │  │
│  │ Endpoints    │  │ Cache      │  │  Reporter  │  │
│  └──────────────┘  └────────────┘  └────────────┘  │
└─────────────────────────────────────────────────────┘
Deployment Topologies
Topology Pros Cons Use Case
Single node (master + embedded agent) Simplest setup; one container Shared resources; single point of failure PoC, testing, small production
Multi-node (master + external agents) Horizontal scaling; fault isolation; geo-distribution Higher ops complexity; enrollment lifecycle Medium/large production, multi-region

Quick Start

# 1. Prepare config
mkdir -p deploy data
cp config.example.yaml deploy/config.yaml
# Edit deploy/config.yaml — set jwt_secret and admin_password

# 2. Run with Docker Compose
export AI_GATEWAY_IMAGE=vaalacat/ai-gateway:latest
docker compose up -d

# 3. Access
# Web UI: http://localhost:8140
# Health: http://localhost:8140/ping

Configuration

The configuration file accepts these top-level keys:

  • log_level — Logging verbosity (debug, info, warn, error)
  • master — Control plane settings (listen address, DB, JWT, admin credentials)
  • agent — Data plane settings (listen address, master URL, enrollment)
  • runtime — Optional advanced tuning (timeouts, heartbeat, retry)

See config.example.yaml for a complete template.

Deployment

Single Node (Docker Compose)

See the Quick Start section above. Full details in docker-compose.yml.

Multi-Node (External Agents)
  1. Generate an enrollment token from master
  2. Configure agent with master_url and enrollment_token
  3. Start with docker compose -f docker-compose.yml -f docker-compose.agent.yml up -d

See docker-compose.agent.yml for the overlay template.

Kubernetes

See docs/k8s-deployment.md for Kubernetes deployment guidance.

Development

# Prerequisites: Go 1.25+, Node.js 20+, pnpm

# Build (frontend + backend)
CGO_ENABLED=0 bash ./build.sh

# Run tests
CGO_ENABLED=0 go test ./... -count=1 -timeout=120s

# Frontend dev server (port 8141, proxies to :8140)
cd web && pnpm install && pnpm dev

Releasing

Releases are cut by pushing a v* git tag. GitHub Actions builds a multi-arch image (linux/amd64 + linux/arm64) and pushes it to Dockerhub.

# Stable release — also updates :latest
git tag v1.2.3
git push origin v1.2.3

# Pre-release — pushes :v1.2.3-rc1 only, does NOT update :latest
git tag v1.2.3-rc1
git push origin v1.2.3-rc1

The git tag is injected into the binary as internal/version.Version.

Contributing

See CONTRIBUTING.md for development setup, code style, and PR process.

Acknowledgments

This project supports native code (purely self-developed, supporting chat, response, and messages protocols), while other protocols are supported by the new-api channel.

It builds upon the work of the following:

  • new-api by @QuantumNous — the legacy channel adaptor, 50+ upstream provider constants, model-fetch protocols, and token-counting utilities are reused via github.com/QuantumNous/new-api. Without this prior work, out-of-the-box support for 50+ providers would not be feasible. Sincere thanks to the new-api maintainers and contributors.

  • datatype by @franktisellano — variable OpenType font (SIL OFL 1.1) used for inline sparklines in the UI. See web/public/fonts/OFL.txt.

Contract Test (optional)

test/contract/ 包含跨语言一致性测试,默认不跑,需要时手动:

# 1) 启动 master
./ai-gateway --config config.yaml &

# 2) 取 admin token
TOKEN=$(curl -s -X POST http://localhost:8140/api/login \
  -H 'Content-Type: application/json' \
  -d '{"username":"admin","password":"change-this-password"}' \
  | jq -r '.data.token')

# 3) 跑测试
AIGW_ADMIN_TOKEN=$TOKEN go test -tags=contract ./test/contract/

测试内容: 扫 web/src/lib/api/*.ts 中所有 /... 路径字面量,逐个发请求,验证后端没有返回 404 (即不存在路由漂移)。

License

MIT

Directories

Path Synopsis
internal
agent/app
Package app 装配 agent 端服务到 app.AgentApplication 接口。
Package app 装配 agent 端服务到 app.AgentApplication 接口。
agent/cache/entitycache
Package entitycache 提供通用实体缓存抽象。
Package entitycache 提供通用实体缓存抽象。
agent/cache/loaders
Package loaders 提供 entitycache.Loader 的具体实现(每实体一个文件)。
Package loaders 提供 entitycache.Loader 的具体实现(每实体一个文件)。
agent/relay/affinity
Package affinity 实现 agent 本地的渠道缓存粘性:把 (用户, 真实模型) 上次发生 缓存活动的上游 channel 记下来,供 plan 阶段优先选用。
Package affinity 实现 agent 本地的渠道缓存粘性:把 (用户, 真实模型) 上次发生 缓存活动的上游 channel 记下来,供 plan 阶段优先选用。
agent/relay/backend
Package backend defines the Backend contract and the Dispatcher that routes each Attempt to one of native / passthrough / legacy implementations based on Attempt.Mode.
Package backend defines the Backend contract and the Dispatcher that routes each Attempt to one of native / passthrough / legacy implementations based on Attempt.Mode.
agent/relay/backend/common
Package common 提供 backend(native / passthrough)共用的工具。
Package common 提供 backend(native / passthrough)共用的工具。
agent/relay/codec
Package codec defines the Intermediate Representation (IR) types and Codec interfaces used by the native protocol conversion layer.
Package codec defines the Intermediate Representation (IR) types and Codec interfaces used by the native protocol conversion layer.
agent/relay/codec/claude
Package claude implements the Anthropic Claude Messages API protocol codec.
Package claude implements the Anthropic Claude Messages API protocol codec.
agent/relay/codec/openai
Package openai implements the OpenAI Chat Completions protocol codec.
Package openai implements the OpenAI Chat Completions protocol codec.
agent/relay/dataflow
Package dataflow 把 channel 内部"解码后请求 → 待发送上游 HTTP 请求"的处理 表达成一串独立的 Step,由 ChannelDataFlow 统一运行与描述。
Package dataflow 把 channel 内部"解码后请求 → 待发送上游 HTTP 请求"的处理 表达成一串独立的 Step,由 ChannelDataFlow 统一运行与描述。
agent/relay/inflight
Package inflight 跟踪 agent 当前在途的 relay 请求,供看门狗告警与 master 远程诊断。
Package inflight 跟踪 agent 当前在途的 relay 请求,供看门狗告警与 master 远程诊断。
agent/relay/modelview
Package modelview 计算"当前 caller 在 /v1/models 中应见的模型列表"。
Package modelview 计算"当前 caller 在 /v1/models 中应见的模型列表"。
agent/relay/pipeline/ctxbuild
Package ctxbuild 是 relay pipeline 的 Phase 1:把 *gin.Context 装配成 state.RelayContext.Input 的请求级 immutable 输入。
Package ctxbuild 是 relay pipeline 的 Phase 1:把 *gin.Context 装配成 state.RelayContext.Input 的请求级 immutable 输入。
agent/relay/pipeline/publish
Package publish 是 relay pipeline 的第 4 阶段(最末端):把累加好的 RelayContext 状态 一次性收成 protocol.UsageLogEntry 并经 EventBus 发布 usage.completed。
Package publish 是 relay pipeline 的第 4 阶段(最末端):把累加好的 RelayContext 状态 一次性收成 protocol.UsageLogEntry 并经 EventBus 发布 usage.completed。
agent/relay/resilience
Package resilience 给单次 channel dispatch 套重试/熔断/超时。
Package resilience 给单次 channel dispatch 套重试/熔断/超时。
agent/relay/script
Package script 实现管理员动态 goja 脚本的编译与执行。
Package script 实现管理员动态 goja 脚本的编译与执行。
agent/relay/state
Package state 是 relay 子包之间共享的叶子包,承载跨包共享的数据结构、 pipeline phase 枚举、relay mode 枚举、Dispatcher 接口以及哨兵 error。
Package state 是 relay 子包之间共享的叶子包,承载跨包共享的数据结构、 pipeline phase 枚举、relay mode 枚举、Dispatcher 接口以及哨兵 error。
agent/relay/transform
Package transform provides IR-level request transformers (system prompt injection, role mapping, thinking pass-through/strip) that the relay pipeline applies between inbound decode and outbound encode.
Package transform provides IR-level request transformers (system prompt injection, role mapping, thinking pass-through/strip) that the relay pipeline applies between inbound decode and outbound encode.
dao
models
internal/models/user_group.go
internal/models/user_group.go
pkg/byokcrypto
Package byokcrypto 提供 BYOK key 的 AES-256-GCM 加解密。
Package byokcrypto 提供 BYOK key 的 AES-256-GCM 加解密。
pkg/metrics
Package metrics 暴露 BYOK 相关 prometheus 指标。
Package metrics 暴露 BYOK 相关 prometheus 指标。
pkg/protocol
internal/pkg/protocol/sync_routing.go
internal/pkg/protocol/sync_routing.go
settings
Package settings 见 spec.go 顶部注释。
Package settings 见 spec.go 顶部注释。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL