Skip to content

fix: estimateInputTokens 支持 Anthropic 格式#73

Merged
bigmanBass666 merged 3 commits into
mainfrom
worktree-fix-input-token-estimation
Jul 17, 2026
Merged

fix: estimateInputTokens 支持 Anthropic 格式#73
bigmanBass666 merged 3 commits into
mainfrom
worktree-fix-input-token-estimation

Conversation

@bigmanBass666

Copy link
Copy Markdown
Contributor

问题

Claude Code 发送给 SenseNova 的请求体使用 Anthropic 格式,其中 messages[].content 是数组而不是字符串。estimateInputTokens 将 content 解析为 string,导致流式请求的 input_tokens 始终为 0。

修复

  • 将 Content 字段类型从 string 改为 json.RawMessage
  • 运行时检测实际类型:字符串直接使用,数组则提取每个 text 字段
  • 同时兼容 OpenAI 格式和 Anthropic 格式

效果

修复后流式请求的 input_tokens 正确率从 7.3% 提升到 98.1%。

@bigmanBass666
bigmanBass666 force-pushed the worktree-fix-input-token-estimation branch from f643087 to 8f4b6fb Compare July 17, 2026 21:35
@bigmanBass666
bigmanBass666 marked this pull request as ready for review July 17, 2026 21:36
@bigmanBass666
bigmanBass666 enabled auto-merge (squash) July 17, 2026 21:36
@bigmanBass666
bigmanBass666 merged commit ba5792b into main Jul 17, 2026
4 checks passed
@bigmanBass666
bigmanBass666 deleted the worktree-fix-input-token-estimation branch July 17, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant