路由信息

默认情况下,客户端只能看到自己请求的模型别名。 打开路由信息开关后,响应头与查询接口会额外给出这次请求落到了哪个渠道与账户, 方便排查「为什么慢了」「为什么报错」。

这些数据反映的是上游拓扑,因此默认全部关闭。

一、会话键

先能定位到一段会话,才谈得上查询它的路由。会话键按优先级取第一个可用的:

优先级 来源
1 请求头 x-portara-client-session
2 请求头 x-deepseek-harness-session-id
3 请求体 prompt_cache_key
4 请求内容指纹(可通过 PORTARA_AFFINITY_CONTEXT 关闭)
bash
curl http://127.0.0.1:52740/v1/chat/completions \
  -H "authorization: Bearer $PORTARA_CLIENT_KEY" \
  -H "x-portara-client-session: my-conversation-001" \
  -H "content-type: application/json" \
  -d '{"model":"balance/deepseek-flash","messages":[{"role":"user","content":"hi"}]}'

二、响应头

头 何时出现 含义
x-portara-request-id 总是 请求级 uuid,排查锚点,不含拓扑信息
x-portara-route 开启路由信息时 别名 / 渠道 / 账户 / 上游模型,随档位变化
x-portara-route-attempts 开启时 第几次尝试才成功(1 = 首击成功)
x-portara-route-switch 发生切换时 切换原因
x-portara-concurrency-wait-ms 排过队时 等并发槽位花了多少毫秒
x-portara-session 注入了厂商会话头时 实际发给上游的会话头值

x-portara-route 的三档形态由 exposeRoutingDetail 决定:

text
off      balance/deepseek-flash
alias    balance/deepseek-flash
channel  balance/deepseek-flash/OpenDesign/ACP-003/deepseek-v4.1-flash

切换原因取值:quota / concurrency / auth / upstream / rate_limit / error / retry。

头值里只允许可见 ASCII:空格折叠成 -,引号、逗号、非 ASCII 字符会被剥掉。 因此渠道名与账户名在头里可能变短甚至为空。

三、查询接口

http
GET /v1/route-info HTTP/1.1
authorization: Bearer sk-portara-xxxxxxxx
x-portara-client-session: my-conversation-001

响应:

json
{
  "object": "route.info",
  "sessionKey": "8ff4d582",
  "updatedAt": "2026-01-01T00:00:00.000Z",
  "route": {
    "alias": "balance/deepseek-flash",
    "channel": "OpenDesign",
    "account": "ACP-003",
    "upstreamModel": "deepseek-v4.1-flash"
  },
  "history": [
    {
      "attempt": 1,
      "channel": "CommandCode",
      "account": "ACP-002",
      "outcome": "quota_cooldown",
      "statusCode": 429,
      "reason": "...",
      "at": "2026-01-01T00:00:00.000Z"
    }
  ]
}

字段说明:

  • route —— 当前路由。数据来自缓存(默认 15 分钟)里的「最近一次成功路由」, 因此首击成功也能查到(失败事件表只在失败/切换时写)
  • history —— 最近 routeInfoWindowMin 分钟内的失败与切换事件,按时间正序,最多 50 条
  • history[].outcome —— attempt_failed / failover_success / quota_cooldown
  • 没有记录时返回结构一致的空对象(history: [],route 各字段 null), 客户端不需要写两套分支

四、隐私与开关

设置项(/api/settings,分组 gateway):

键 默认 说明
exposeRoutingInfo false 关闭时响应头只含 x-portara-request-id,查询接口返回空结构
exposeRoutingDetail alias off 只回别名与是否切换;alias 同;channel 才回渠道与账户
routeInfoWindowMin 60 查询回溯窗口(分钟,1–1440)

脱敏规则:

档位 行为
off 渠道 / 账户置空;history[].reason 退化成 ok / switched
alias 渠道 / 账户置空,其余照常
channel 完整

五、典型用法

bash
# 1. 发一次请求,带上会话键
curl -s -D /tmp/h.txt http://127.0.0.1:52740/v1/chat/completions \
  -H "authorization: Bearer $PORTARA_CLIENT_KEY" \
  -H "x-portara-client-session: fuzz-001" \
  -H "content-type: application/json" \
  -d '{"model":"balance/deepseek-flash","messages":[{"role":"user","content":"hi"}]}'

# 2. 看这次落到哪
grep -i x-portara-route /tmp/h.txt

# 3. 查这段会话的切换历史
curl -s http://127.0.0.1:52740/v1/route-info \
  -H "authorization: Bearer $PORTARA_CLIENT_KEY" \
  -H "x-portara-client-session: fuzz-001"

六、给插件作者的建议

  • 用 x-portara-request-id 做关联,不要依赖渠道名做逻辑判断
  • 渠道 / 账户字段可能为空(隐私档位、名字被 ASCII 过滤),代码要能吃 null
  • 想让用户看到实时状态时,查询接口比解析响应头可靠 —— 首击成功也有记录
提交803e538 纯文本(适合 AI)编辑此页