fix: align official Grok prompt cache with CPA for Sub2API#708
Merged
Conversation
官方 cached_tokens 长期为 0 的根因不是 reasoning-replay,而是会话亲和被打散: 空 session 时每请求随机 x-grok-conv-id,且无消息 hash 粘滞兜底。 对齐 CLIProxyAPI 行为: - 无稳定 session 时不再伪造随机 conv-id / session-id - 显式 prompt_cache_key / Claude session 生成跨轮稳定 upstreamID - 无 session 时用 system+首条 user 消息锚点 soft session(多轮不漂移) - 扩展 Sub2API 会话信号提取:session_id / conversation_id / prompt_cache_key / x-grok-conv-id - 租户隔离与按模型 affinity 保持不变 测试覆盖:gateway soft session、Sub2API e2e 身份、adapter 空 key、inference seed 提取。
…edentials 1) Usage 统计: - Anthropic Messages 的 cache_read_input_tokens 写入统一 CachedInputTokens - OpenAI Chat Completions 的 prompt_tokens_details.cached_tokens 同样写入 - Responses input_tokens_details.cached_tokens 行为保持 - Chat completion_tokens_details.reasoning_tokens 一并兼容 - 增加 Messages / Chat / 优先级回归测试 2) 凭据刷新: - credential_decrypt_failed 不再标 Permanent(本地加密密钥问题可恢复) - 已标记 permanent 的 decrypt_failed 允许 force/批量调度重试 - invalid_grant 等真正 OAuth 永久失败仍阻断 - 增加密钥恢复后可重试 + invalid_grant 仍永久 的回归测试
Contributor
Author
追加提交(本 PR 第二 commit)1. Anthropic / OpenAI 缓存统计修复(看板
|
chenyme
self-requested a review
July 19, 2026 10:05
补齐复核发现的遗漏,确保 Codex(OpenAI)/Anthropic 缓存统计与亲和更稳: 1) soft session 纳入顶层 instructions/system(Responses/Chat 常见) 2) 流式 usage 不再要求 TotalTokens>0 才采纳;合并多帧避免半截帧抹掉 cached 3) conversation streamConverter 在仅有 cached_tokens 时也更新 usage 回归:stream merge Anthropic/Chat、instructions soft session、既有三协议解析。
Contributor
Author
全面复核补齐(第 3 commit)对照 CPA + 全链路审计后,本提交补上遗漏点: 已覆盖清单
仍需部署侧配合(代码无法单独保证)
自测go test ./internal/transport/http/inference/ ./internal/application/gateway/ \
./internal/infra/provider/cli/ ./internal/application/account/ \
./internal/infra/provider/conversation/ \
-run 'ExtractUsage|StreamInspector|BuildSession|Soft|OfficialCache|PromptCache|GrokSession|CredentialDecrypt|ConvertResponses'
# all ok |
Owner
|
暂时没能复现 PR 内所说的缓存提升,能否提供相关可复现的流程? |
Contributor
Author
不是缓存提升,打错字了,是完善了由于Anthropic / OpenAI 转换协议有无法缓存的问题,sorry打错字了 |
|
确实,之前用聊天补全接口,缓存率都是0() |
chenyme
approved these changes
Jul 20, 2026
chenyme
marked this pull request as draft
July 20, 2026 02:39
chenyme
marked this pull request as ready for review
July 20, 2026 02:40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
摘要
修复 Grok Build 官方 Prompt Cache(
cached_tokens)长期 0% 的问题,对齐 CLIProxyAPI (CPA) 的会话亲和行为,并兼容 Sub2API 会话信号透传。问题根因(对照 CPA)
uuid.NewV7()写x-grok-conv-idcached_tokens=0官方机制说明:xAI Prompt Caching
修改内容
1. 禁止随机 conv-id(P0)
backend/internal/infra/provider/cli/adapter.gogrokSessionID("")返回空,不再每请求生成 UUIDx-grok-conv-id/x-grok-session-id2. 稳定会话身份(对齐 CPA)
backend/internal/application/gateway/prompt_cache.goprompt_cache_key/ session seed → 跨轮稳定upstreamID+ 按模型隔离的affinityKey3. Sub2API 会话信号提取
backend/internal/transport/http/inference/prompt_cache.gosession_id/conversation_id/ bodyprompt_cache_key/X-Grok-Conv-Id等4. 网关接线
backend/internal/application/gateway/service.goprompt_cache_session_empty/prompt_cache_session_soft测试
已通过:
prompt_cache_key/session_id提取Sub2API 对接要求(部署后必配)
每轮固定传同一会话(任选其一):
{ "prompt_cache_key": "稳定-UUID" }或 header:
Nginx(若有):
验收
cached_tokens: 0)prompt_cache_key连续多轮,只追加消息说明
cached_tokens)chenyme/grok2apimain cherry-pick,避免旧 PR feat: add server-side reasoning replay cache for multi-turn Grok Build #676 分叉历史