Files
magpie/internal
yetoneandmdwsk88 fc7fde82c5 gateway: a Claude reply's thinking tokens are counted as its reasoning, and told to Claude Code when another protocol's model reasoned (#3430)
A Messages API reply can say in its message_delta how many of its output
tokens were thinking (usage.output_tokens_details.thinking_tokens), as
Copilot's Claude replies do. aUsage had no field for it, so every such
reply, relayed or translated, was recorded with reasoning 0 (#3430: all
277 claude-desktop rows through Copilot on 2026-10-23). The Claude
subscription's cliUsage already read the field
(claude_subscription.go:4272), and the Chat, Responses and Gemini usages
read their own counts.

aUsage now reads output_tokens_details into Usage.Reasoning, and
Usage.anthropic() writes it when there is some, so a Claude Code client
of a Chat or Responses model that reasoned is told its thinking tokens in
message_delta, as Usage.chat() and the Responses encoder tell theirs. A
usage without reasoning is written as before. Reasoning is a part of
Output and is priced nowhere on its own (usage, request_page), so no cost
changes. The change is mdwsk88's patch (6bdf0fe53) as written; reviewed
line by line, nothing taken out.

Run on mote (macOS 27, go1.26.8) at dc49feca9 plus this change:
- go test -count=1 -run 'Thinking|Reasoning|Anthropic.*Usage|Usage'
  ./internal/gateway/: ok (75s), the two new tests among them:
  TestAnthropicThinkingTokensCounted (Copilot's own reply bytes, through
  /v1/messages, /v1/chat/completions and /v1/responses) and
  TestTranslatedReasoningToldAsThinkingTokens.
- Broken by hand: anthropic.go put back at dc49feca9, the tests kept:
  both fail, "thinking tokens not counted: 200 output 1277 reasoning 0"
  on each of the three paths and "message_delta doesn't tell the
  thinking tokens".
- gofmt -l on the two files: nothing.
Not tried: a live Copilot Claude request through a built magpie (the
fixture is the reporter's real Copilot reply); whether Anthropic's own
API sends the field.

Reported-by: mdwsk88 (dawei on Discord)
Co-authored-by: mdwsk88 <11055210+mdwsk88@users.noreply.github.com>
2026-11-07 04:03:01 +08:00
..
…
…
…
…
…