Loading repository data…
Loading repository data…
study8677 / repository
自托管 OpenAI-compatible AI Gateway:用 auto / auto-coding / auto-longtext 自动选择合适模型,支持流式、工具调用、多模态透传和 fallback。
把普通 OpenAI-compatible 中转站,升级成会自动选模型的本地 AI Gateway。
auto / auto-coding / auto-longtext
快速开始 · 虚拟模型 · 架构 · 路由策略 · Docker · 文档
LLM Router 运行在你的客户端和上游中转站之间。客户端继续使用熟悉的 /v1/chat/completions,只需要把模型名写成 auto、auto-coding 或 auto-longtext,路由器会根据请求内容、模型价格、能力约束和失败重试策略,选择更合适的真实模型。
Client / SDK -> LLM Router -> your existing OpenAI-compatible relay
model=auto local policy selected real model
适合已经有 base_url 和 API Key,但不想每次手动切模型的人:简单任务尽量便宜,复杂 coding、长文本推理、架构规划和安全 review 不为了省钱牺牲质量。
base_url=http://127.0.0.1:8787/v1。auto、auto-coding、auto-longtext 覆盖日常、工程和长文本场景。git clone https://github.com/study8677/llm-router.git
cd llm-router
npm install
cp .env.example .env
编辑 .env,填入你已有的上游中转站:
UPSTREAM_BASE_URL=https://your-relay.example.com
UPSTREAM_API_KEY=sk-your-upstream-key
启动服务:
npm run build
npm start
打开本地配置页:
http://127.0.0.1:8787/admin
这里可以查看上游模型、当前生效的路由模型,并把 auto 使用的路由模型从“自动选择最便宜已知价格模型”改成“手动指定某个模型”。配置会保存到 .llm-router.local.json。
客户端改成:
base_url=http://127.0.0.1:8787/v1
api_key=任意值
model=auto
如果设置了 ROUTER_API_KEY,客户端的 api_key 需要填写这个本地代理 Key。
auto通用入口,适合问答、翻译、改写、推理、简单分析和大多数日常请求。简单任务优先低成本模型,困难推理会升到更强模型。
auto-coding工程入口,适合代码生成、debug、架构设计、repo 级规划、PR review 和安全分析。简单代码任务可以走 coding specialist,复杂工程任务会倾向最强模型。
auto-longtext长文本入口,适合总结、抽取、合同/文档分析、长上下文推理。简单抽取优先低成本长上下文模型,复杂分析会选择更强推理模型。
你也可以继续传真实模型 ID。真实模型会直接转发,不经过路由模型。
flowchart LR
Client["Client / SDK"] --> Router["LLM Router"]
Router --> RouteModel["Router model<br/>auto cheapest or manual"]
RouteModel --> Decision["Routing JSON"]
Decision --> Router
Router --> AnswerModel["Selected answer model"]
AnswerModel --> Client
路由是两阶段完成的:
遇到 timeout、network error、429、5xx 时,自动路由会按配置重新路由并重试。流式响应只有在上游还没吐出 chunk 前才能 fallback;一旦已经发给客户端,就不能安全换模型。
更多细节见 Architecture 和 Routing Behavior。
健康检查:
curl http://localhost:8787/health
模型列表:
curl http://localhost:8787/v1/models
自动路由:
curl http://localhost:8787/v1/chat/completions \
-H 'content-type: application/json' \
-d '{
"model": "auto",
"messages": [{"role": "user", "content": "Explain binary search in one paragraph."}]
}'
流式自动路由:
curl -N http://localhost:8787/v1/chat/completions \
-H 'content-type: application/json' \
-d '{
"model": "auto-coding",
"stream": true,
"messages": [{"role": "user", "content": "Write a TypeScript debounce function."}]
}'
响应 header 会暴露关键路由信息:
x-llm-router-request-id
x-llm-router-original-model
x-llm-router-target-model
| 类型 | 当前支持 |
|---|---|
| OpenAI-compatible API | POST /v1/chat/completions、GET /v1/models、GET /health |
| Chat Completions | 普通响应、SSE streaming、auto fallback |
| 请求透传 | tools / function calling、多模态 Chat Completions |
| 计划中 | /v1/embeddings、/v1/responses、Anthropic Messages API |
默认策略偏向“简单任务省成本,复杂任务保质量”:
gpt-5.4-mini 或其他低成本可用模型。gpt-5.3-codex。gpt-5.5,并使用 xhigh。high 或 xhigh。模型池、价格、能力标签和重试行为都可以通过配置调整。配置入口见 Configuration。
访问 http://127.0.0.1:8787/admin 可以配置 auto 路由第一跳使用的“路由模型”:
ROUTER_API_KEY,页面会要求输入这个本地 Key 才能读取或保存配置。这只影响路由判断模型,不会把它当成最终回答复用。最终回答仍然由路由 JSON 选出的目标模型单独调用。
LLM Router 会尽量保持最终请求和原始客户端请求一致:
tools、tool_choice、parallel_tool_calls、旧版 functions、function_call 会透传给最终模型。required_capabilities。cp .env.example .env
docker compose up --build
Docker 和生产运行建议见 Operations。
npm install
npm run build
npm test
真实上游路由评估:
npm run test:live-routing
test:live-routing 会读取本地 .env 并调用真实上游。
默认建议把 LLM Router 作为本机或内网服务使用:
.env。ROUTER_API_KEY。