router README

项目的 README,来自 github.com/weave-os/router。合并为一个 markdown 文件:README.md。

Weave Router

License: Apache-2.0 Managed deployment

Built with Weave Router

Weave Router

Weave Router is a proxy that selects a model for each API request. It works with Anthropic, OpenAI, and Gemini APIs, so you can use it with coding tools or your own application. You can use the hosted router or run it yourself with your own provider keys. For the best experience, use the managed router hosted by Weave.

The router uses an HMM classifier policy to choose from enabled models. It supports streaming, tool calls, and vision.

Get started

For the hosted router, run the installer and follow the setup prompts:

npx @weave-os/router

You'll need Node 18 or later. The hosted setup page can help you get a router key; you can also create one in the Weave dashboard under Settings → API keys. To choose a tool directly, use one of these commands:

npx @weave-os/router --claude
npx @weave-os/router --codex
npx @weave-os/router --copilot
npx @weave-os/router --opencode
npx @weave-os/router --pi

The installer configures your selected tool to use Weave. For Copilot, it adds Weave Router to the model picker; you can also start it with copilot --model weave/auto.

For example, to switch Codex back to its previous provider, run npx @weave-os/router off --codex. Use on --codex to switch back to Weave, or --uninstall --codex to remove the configuration. See the installation guide for what changes for each tool, project-level setup, and all available options.

For client-specific setup, connection checks, switching back, and uninstall, see the Router quickstart, Claude Code guide, and Codex CLI guide.

Run it yourself

You'll need Docker, Go 1.26 or later, a provider API key, and a local checkout. For example, with an OpenRouter key:

git clone https://github.com/weave-os/router.git
cd router
echo 'OPENROUTER_API_KEY=sk-or-v1-...' >> .env.local
echo 'ROUTER_ADMIN_PASSWORD=replace-with-a-strong-password' >> .env.local
make full-setup

This starts the router and Postgres, creates a router key, and offers to set up Claude Code. The router listens at http://localhost:8080 and the dashboard is at http://localhost:8080/ui/. The router key (rk_...) is printed during setup; use it to authenticate clients. Your provider key is separate and stays in .env.local. Without an admin password, inference still works but dashboard administration is disabled.

To connect another supported tool to the local router, add --local to its installer command, for example:

npx @weave-os/router --codex --local

Cursor routes its Agent and Ask chats through the router. Cursor calls the base URL from its own servers, not from your machine, so a localhost router is unreachable: use the hosted router or a publicly reachable self-hosted URL. In Cursor, open Settings → Models, turn on OpenAI API Key with your rk_... router key, set Override OpenAI Base URL to <base-url>/v1, and add a custom model named Weave Auto (the router also accepts weave-auto and rejects other weave- names). Tab completion and Apply stay on Cursor's own models, and BYOK needs a paid Cursor seat.

The self-hosted router sends prompts to your configured provider. It also supports OpenTelemetry for tracing. For configuration, deployment details, and other provider options, see the configuration reference.

Self-hosted HMM

The optional self-hosted HMM is a barebones, frozen version of the policy. It doesn't include the managed service's training, online learning, or registry. For the best experience, use the Weave-managed router. The self-hosted stack defaults to it (hmm_embedding). The local compose stack boots on a local-dev-only stub classifier (smoke/hmmstub) that puts every turn in one routing group; make up-hmm (also local dev only) swaps in the real sidecar. See the HMM guide for setup.

Architecture

The router, HMM policy sidecar, database, and dashboard run locally. Requests go from the router to your configured model provider; you can also send traces to an OpenTelemetry collector. The HMM sidecar uses a compatible embedding service; its default configuration calls Google's embedding API.

flowchart LR
    client["Claude Code, Codex, opencode,<br/>pi, Cursor, your own app"]
    router["Router :8080<br/>/v1/messages · /v1/chat/completions<br/>/v1beta/models · /v1/route"]
    hmm["HMM policy sidecar :8093<br/>local dev: stub by default, make up-hmm"]
    pg[("Postgres<br/>installations, rk_ keys,<br/>encrypted BYOK keys, usage")]
    ui["Dashboard /ui<br/>selfhosted mode only"]
    providers["Anthropic · OpenAI · Gemini<br/>OpenRouter and any<br/>OpenAI-compatible endpoint"]
    otel["Your OTLP collector<br/>Honeycomb, Datadog, Grafana"]

    client -->|"rk_… bearer token,<br/>streamed response back"| router
    router -->|"classify the turn"| hmm
    router -->|"auth, config, usage"| pg
    pg --> ui
    router -->|"provider key from env or BYOK"| providers
    router -.->|"spans and usage logs"| otel

    classDef external fill:#f4f4f5,stroke:#a1a1aa,color:#3f3f46
    class providers,otel external

Multi-replica deployments also need Pub/Sub for cache invalidation; Docker Compose runs an emulator locally.

API

The router accepts Anthropic Messages (/v1/messages), OpenAI Chat Completions (/v1/chat/completions), and Gemini requests (/v1beta/models/:action). To inspect a routing decision without sending a request to a model, use POST /v1/route. The endpoint reference covers health checks, response costs, and the other endpoints.

For example, using the local router:

curl http://localhost:8080/v1/chat/completions \
  -H 'Authorization: Bearer rk_...' \
  -H 'Content-Type: application/json' \
  -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hi"}]}'

See the configuration reference for router settings, the analytics guide for exporting routing decisions, and the session cost guide for usage and savings.

For the rest of the documentation, including routing terminology and policy integration, see the documentation index.

Contributing

See CONTRIBUTING.md for development setup and tests. The architecture guide describes the package layout and the benchmark guide covers the evaluation harness.

License

Apache License 2.0. See NOTICE and THIRD_PARTY.md for attribution and third-party licenses. The Apache-2.0 release line begins with router-v0.2.24; earlier releases retain the licenses they shipped with.

router (weave-os/router)

正在搜索数据库…

这是什么?一个即插即用的代理,会把你的编程智能体或应用发出的每条提示,通过 OpenRouter 路由到 Anthropic、OpenAI、Gemini 以及开源模型中当前最合适的可用模型。只需替换 API 端点,推理成本就能降低 40-70%。
这是我要导入的库,还是我要指向的服务器?它是一个用 Go 编写的独立代理服务器。你只需把 Claude Code、Codex、Cursor 或你自己的应用指向 localhost:8080(或托管端点),而不必把它嵌入为库。
我该怎么把它跑起来?最快的方式是使用一条命令的安装程序,把 Claude Code、Codex、opencode 或 pi 指向托管的 Weave Router,无需克隆代码、Docker 或 Postgres。若想改为自行部署完整服务,把一个提供商密钥写入 .env.local,再用 Docker Compose 运行即可。
安装前我需要准备什么?快速安装程序需要 Node >=18(如果使用 Claude Code、opencode 和 pi 的安装路径,还需要 jq);如果改为自行部署,则需要一个提供商 API 密钥,推荐以 OpenRouter 作为基础。
自己部署它有什么不足之处?自行部署意味着你要自己设置提供商密钥和控制台密码。即使没有设置密码,推理功能仍可正常工作,但这样控制台就没有防护。
我的提供商 API 密钥最终存放在哪里?密钥保存在你自己的机器上,默认进行静态加密(BYOK),不会被发送给 Weave,也不会由 Weave 存储。
路由器究竟如何决定使用哪个模型?一个源自 Avengers-Pro 方法的集群评分器会对每个上游 API 请求进行分类,并为其挑选最合适的已启用提供商,取代了靠感觉判断的提示词分类器。它按操作进行路由,而不是按对话轮次进行路由。
它支持 DeepSeek 或 Qwen 等开源模型吗?支持。它可以通过 OpenRouter 或任何兼容 OpenAI 的端点路由到 DeepSeek、Kimi、GLM、Qwen、Llama 和 Mistral,同时也支持 Anthropic、OpenAI 和 Gemini。
我能看到路由器发出的请求吗?它开箱即用地输出 OTLP 追踪数据,可以在 Weave 控制台中查看,也可以导入 Honeycomb、Datadog、Grafana 或其他任何兼容 OTLP 的工具。
作者
weave-os
大小
47.9 MB
星标
5.7K
更新
提交Dec 02
时长
7个月
提交 · 90 天