Skip to content

fix(proxy): strip Claude total token markers - #6803

Open
wskk267 wants to merge 3 commits into
farion1231:mainfrom
wskk267:fix/strip-anthropic-total-tokens
Open

fix(proxy): strip Claude total token markers#6803
wskk267 wants to merge 3 commits into
farion1231:mainfrom
wskk267:fix/strip-anthropic-total-tokens

Conversation

@wskk267

@wskk267 wskk267 commented Aug 25, 2026

Copy link
Copy Markdown

Summary / 概述

Fix the Claude-to-OpenAI conversion path so Claude Code's dynamic
<total_tokens>...</total_tokens> markers do not accumulate in the
merged OpenAI system prompt.

修复 Claude→OpenAI 转换路径,避免 Claude Code 的动态
<total_tokens>...</total_tokens> 标签不断累积到 OpenAI system 字符串中,
导致 OpenAI 兼容端点的 prompt cache 命中率下降。

The fix only affects system messages. User and assistant content remains unchanged.

该修复只处理 system 消息,不会修改 user 和 assistant 内容。

Related Issue / 关联 Issue

Fixes #6789

Screenshots / 截图

Before / 修改前 After / 修改后
Not applicable: backend-only change / 不适用:仅后端代理逻辑修改 Not applicable: no UI changes / 不适用:没有界面变化

Validation / 验证

  • cargo fmt --check --manifest-path src-tauri/Cargo.toml passed.
  • cargo clippy --manifest-path src-tauri/Cargo.toml -- -D warnings passed.
  • Anthropic/OpenAI transform tests: 70 passed.
  • Rust library tests: 2672 passed, 5 ignored.
  • git diff --check passed.
  • Added regression tests for merged system messages, system content arrays,
    user content preservation, and unterminated markers.

Checklist / 检查清单

  • pnpm typecheck was not run; this is a Rust-only change /
    未运行;本次仅修改 Rust 后端代码
  • pnpm format:check was not run; this is a Rust-only change /
    未运行;本次仅修改 Rust 后端代码
  • cargo clippy passes /
    cargo clippy 已通过
  • No user-facing text changed; i18n update is not applicable /
    未修改用户可见文本,不需要更新国际化文件

@wskk267
wskk267 requested a review from farion1231 as a code owner August 25, 2026 12:05
@github-actions github-actions Bot added backend Backend (Rust/Tauri) proxy Related to proxy/API forwarding labels Aug 25, 2026
@farion1231

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e030a95cdf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


stripped.push_str(&remaining[..open_offset]);
let content_start = open_offset + ANTHROPIC_TOTAL_TOKENS_OPEN_TAG.len();
let Some(close_offset) = remaining[content_start..].find(ANTHROPIC_TOTAL_TOKENS_CLOSE_TAG)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict removal to standalone Claude markers

When an application-authored system prompt contains any balanced <total_tokens>...</total_tokens> element—for example, an XML schema or few-shot example—this search treats the first subsequent closing tag as Claude metadata and deletes the entire element. The marker is documented above as a standalone Claude message, so matching arbitrary embedded elements silently corrupts legitimate system instructions; restrict removal to the known standalone marker shape and expected payload.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend (Rust/Tauri) proxy Related to proxy/API forwarding

Projects

None yet

2 participants