magpie icon indicating copy to clipboard operation
magpie copied to clipboard

gateway: reasoning tokens are 0 when usage omits them but reasoning_content was streamed

Open MYCF711 opened this issue 1 month ago • 5 comments

ReasoningHidden covers "can't see thinking", not "see thinking but no token count"

What happens

A streamed reply that does emit reasoning_content deltas but whose final usage carries no reasoning_tokens ends up with:

  • Reasoning = 0 — it is read straight from the vendor's usage (call.Usage.billed().Reasoning, e.g. internal/gateway/gateway.go:3141)
  • ReasoningHidden = false — hidesReasoning() only fires when the channel is hidden or when the protocol keeps its count to itself:
// internal/gateway/ttft.go:320
func (f *firstToken) hidesReasoning(proto provider.Protocol) bool {
	return f.hidden || (proto == provider.Responses || proto == provider.Gemini || proto == provider.CodeAssist) && f.reasoning > 0
}

So the two cases below collapse into the same record, even though they are not the same thing:

case reasoning visible in stream usage.reasoning_tokens today
A no absent ReasoningHidden = true, Reasoning = 0 — honest
B yes (reasoning_content chunks) absent ReasoningHidden = false, Reasoning = 0 — the number is missing but nothing says so

DecodeOf(out, reasoning, ms, ttft, firstText, flow) is then handed reasoning = 0 for case B, so the decode-time split it computes treats the whole reply as text.

Why it matters

For case B the reasoning was streamed — the characters are in the stream we already parsed (that is how firstReasoning gets a timestamp). We are dropping a number we could have measured instead of reporting 0, and nothing in the record marks it as an estimate or as unknown.

Proposal

When usage.reasoning_tokens is absent and reasoning chunks were seen, estimate the count from the characters we already collected and pass that to DecodeOf:

  1. accumulate the char length of reasoning_content deltas (the same deltas that feed firstReasoning)
  2. estimate tokens ≈ chars / ratio, with the ratio taken from the model's own text when the reply has one (fall back to a constant ~4 chars/token), and max(1, …) when any reasoning was seen
  3. hand that number to DecodeOf in place of 0
  4. mark it so the record still tells the truth — either reuse ReasoningHidden with a distinct value or add e.g. ReasoningEstimated bool; the Requests/Usage pages can then say "estimated" instead of silently showing a precise-looking number

If you'd rather not add a field, the minimum that fixes the "silently 0" part is: keep Reasoning as-is (0 when the vendor didn't say) and set ReasoningHidden = true for case B too — then at least a reader can tell "reasoning happened, count unknown" from "no reasoning".

What I checked

  • internal/gateway/ttft.go:320-332 — hidesReasoning / reasoningMs
  • internal/gateway/gateway.go:3141 — Reasoning: comes from call.Usage.billed().Reasoning
  • internal/gateway/speed.go:57,84 — the doc comments on DecodeOf
  • internal/gateway/reasoning_speed_test.go — existing coverage assumes reasoning is a known number

Not checked / not reproduced: I have not found a vendor in my setup that streams reasoning_content while omitting reasoning_tokens, so case B is reasoned from the code paths above, not from a captured record. If you want a repro, tell me which of the built-in providers you'd expect to behave that way and I'll capture one.

MYCF711 avatar Oct 31 '26 08:10 MYCF711

take

ggbdpq avatar Oct 31 '26 09:10 ggbdpq

Landed the minimum fix you offered ("keep Reasoning as-is and set ReasoningHidden = true for case B too") on branch fix/4545-reasoning-hidden-truth (commit 66a7f99e0).

hidesReasoning now also takes whether the usage carried a reasoning-token count:

func (f *firstToken) hidesReasoning(proto provider.Protocol, counted bool) bool {
	return f.hidden || f.reasoning > 0 && ((proto == provider.Responses || proto == provider.Gemini || proto == provider.CodeAssist) || !counted)
}
  • Case B (streamed reasoning_content, no reasoning_tokens in usage) → ReasoningHidden = true, Reasoning untouched (0): the record now says "reasoning happened, count unknown" instead of a precise-looking 0.
  • Case A and counted channels are unchanged; a counted, paced channel keeps its timing (reasoningMs untouched).
  • Wider than the report suggests: the native Anthropic protocol never carries a separate thinking count (anthropic.go builds Usage without Reasoning), so every thinking reply it serves is case B — this now reads hidden there too.
  • Call sites: gateway.go (relay path, count = call.Usage.billed().Reasoning > 0) and the Codex back end's two, where the relayed reply's flag moved after uu.add(sniff.usage()) — it read the pre-final count otherwise.

New test TestReasoningStreamedButNotCounted covers both cases plus the paced-counted channel keeping its timing; the existing reasoning/timing tests (TestReasoningSpeedEvidence, TestReasoningSpeedThroughGateway, TestGeminiSummaryWithoutThoughtCount, …) pass unchanged.

The estimate route (chars → tokens) from the proposal I left alone — it changes a displayed number, which feels like a maintainer decision.

Jackzigen avatar Oct 31 '26 09:10 Jackzigen

Read 66a7f99e0 — this is the right shape, and it matches what yetone said in the Discord thread ("记录会标成数量未知,不再当成 0", and he doesn't want a character-derived number that looks precise).

Three things I checked in the diff:

  1. The signature change is the actual fix. hidesReasoning(proto, counted) makes "the stream showed reasoning, the usage didn't say how much" and "the channel hides its thinking" land in the same honest bucket, instead of one of them silently reading as Reasoning = 0.
  2. Setting the flag after the usage lands (the Codex back end's relayed reply) is the part that is easy to get wrong — the flag has to be decided once the count is known, not from the first chunk.
  3. The Anthropic case you called out is worth keeping in the commit message: a native Anthropic reply never carries a separate thinking count, so every thinking reply there was hitting this. That makes it a correctness fix, not a niche one.

One note, not a request: with ReasoningHidden = true the decode split no longer uses reasoning at all, so for those replies the decode speed returns to the "as if the whole output were text" number — which is the honest reading of "count unknown", just worth knowing that it does not become more accurate, only labelled.

Nothing else from my side; happy to test a build if you want a second pair of eyes before it lands.

MYCF711 avatar Oct 31 '26 09:10 MYCF711

9551fadcb, tagged v0.1.2370: when reasoning streams but the usage gives no reasoning-token count, the record now has ReasoningHidden set and Reasoning left at the vendor's 0. Nothing is estimated. So "reasoned, count unknown" no longer reads as "no reasoning". A thinking + tool-call reply with no text now gets no speed. A reply with text is timed as before.

This happens with real vendors. Anthropic's Messages usage never counts thinking separately. DeepSeek's Anthropic endpoint (deepseek-v4-pro, thinking on) streamed 33 thinking deltas and reported only output_tokens 35. That stream is the test fixture (internal/gateway/testdata/reasoning-uncounted-deepseek-anthropic.sse). The Chat case (reasoning_content with no completion_tokens_details) is covered too. Responses, Gemini and Code Assist were already marked whenever reasoning streamed.

Leaving this open for your check.

yetone avatar Oct 31 '26 09:10 yetone

Checked 9551fadcb (v0.1.2370) — the shape is what I asked for and it goes further than the minimum:

  • ReasoningHidden set, Reasoning left at the vendor's 0, nothing estimated — so "reasoned, count unknown" no longer reads as "no reasoning".
  • The two sub-cases you spelled out are the ones that matter in practice: a thinking + tool-call reply with no text now gets no speed at all (there is no text channel to time), while a reply with text is timed as before.
  • New reasoning_uncounted_test.go plus a .sse fixture (reasoning-uncounted-deepseek-anthropic.sse) means this stays pinned to a real-shaped stream rather than a synthetic one — good.

Local verification: not yet. My install is 0.1.2342, so 0.1.2370 hasn't reached this machine. I'll re-check when it updates and reopen only if something contradicts the above.

Closing this as done from my side.

MYCF711 avatar Oct 31 '26 10:10 MYCF711