Apache Casbin Gateway
3. 供应商

3.2 让Agent的流量走Gateway

把流量接进来,才有LLM记录,权限里的模型和供应商规则才有东西可管,Gateway也才能替Agent保管厂商的Key。

两种接法

让Gateway改写Agent的配置

Agent客户端→打开一个Agent→选一个供应商→写入配置。

对于Gateway认得其配置格式的Agent,这一步会按该Agent的格式,把端点和令牌写进它自己的文件。

  • 预览先把要写的内容原样给你看。
  • 恢复撤销这次写入。
  • 之后换一个供应商,文件当场重写,所以从此切换只需一次点击,两个页面上都能切。

对Codex,它还会写一份列出该供应商模型的模型目录,这样Codex的模型选择器显示的是绑定的模型名,而不是自定义。Gateway会先让Codex自己读一遍这份目录,Codex不接受的目录一律不写,因为一份它解析不了的目录会让Codex启动不了。

你自己导出两个环境变量

其他Agent,页面会给出已经填好真实值的环境变量片段。在带着它的shell里启动Agent:

export ANTHROPIC_BASE_URL="http://localhost:17000/v1/agents/claude-code"
export ANTHROPIC_AUTH_TOKEN="cg-..."

这个token是Gateway自己的中继令牌,不是厂商的Key。Agent那个变量为空就不肯启动,而Gateway对上游用的是供应商的Key。

一个基础URL,四套API

Agent的基础URL是/v1/agents/<agent-id>,具体响应哪套,取决于客户端请求的路径:

Agent说的协议它请求典型客户端
OpenAI Chat Completions/chat/completionsCline、Continue、Zed、Aider、goose等大多数
Anthropic Messages/v1/messagesClaude Code
OpenAI Responses/responsesCodex——它放弃chat completions之后只说这一套
Google Gemini/v1beta/models/<model>:generateContentGemini CLI——只说Google自家的API

**供应商提供的API不必是同一套。**Gateway在这四套之间双向转换,流式回答也一样。于是下面这些都成立:

  • Codex跑在DeepSeek、Kimi或Qwen上。
  • Claude Code跑在其中任意一个上。
  • Gemini CLI跑在一个从没听说过Google API的供应商上。

如果供应商提供的正是请求进来时的那套API,就逐字节原样转发,因为没什么要转换的。

Gateway唯一自己回答的,是Anthropic和Gemini客户端在每轮之前问的token计数;绑定的供应商没有对应端点时,它自己估算。

一个请求经历了什么

  1. 鉴权——校验中继令牌,来自本机的请求除外(5.2)。
  2. 校验——按该Agent的权限检查模型、供应商,以及请求里带的工具(4.1)。被关掉的工具会在请求离开这台机器之前被摘掉。
  3. 定路径——由哪个供应商响应、用哪个模型名,以及它响应不了时降级到什么(3.6)。
  4. 转换——供应商说的是另一套API时。
  5. 转发——用供应商的Key,沿着这条路径逐个尝试,直到有一个响应为止。
  6. 记录——按llmRecordMode规定的深度(4.2)。

请求里要的东西被关掉时,返回的是Agent所说那套API里的permission_error,所以它读起来是一次拒绝,而不是网关坏了。

之后换Key

Agent都指向Gateway之后,Key只存在于一个地方。改一次供应商,所有绑定它的Agent在下一个请求就跟着变,不用碰任何Agent的配置。

下一节:3.3 没有API Key:沿用调用方自己的登录。

在GitHub上编辑
Apache Incubator

Apache Casbin (Incubating) is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Copyright © 2026 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.

Apache, Apache Casbin, the names of Apache projects, and the feather logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. All other marks mentioned may be trademarks or registered trademarks of their respective owners.

本页内容