AI Prompt Runs: record thinking tokens separately (TokensThinking), for every driver and in streaming

Aperta
#6,807 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
34/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
sql, typescript
Ambito
ai, backend

Direzione di ricerca

Start with the ModelUsage type and the Gemini driver's usageFromMetadata (added in #5171), then read how the OpenAI and Anthropic drivers fill ModelSpecificResponseDetails on the non-streaming path to see where the streaming path drops it. Follow TokensCacheRead end to end as the template: entity field, migration and CodeGen, then AIPromptRunner saving and the TokensCacheReadRollup equivalent, plus the agent run totals. Done means every listed driver sets thinkingTokens on both paths with a unit test each, and regression tests show TokensCompletion and Cost unchanged.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

Problem

Thinking (reasoning) tokens are billed as output, and MJ's cost is already right: each driver puts them into completionTokens. What's missing is a separate count. AIPromptRun has TokensCompletion but no field that says how much of it was thinking, so you can't tell whether a slow or expensive call wrote a long answer or thought for a long time.

Example from a production Gemini 3.8 Flash agent: three consecutive calls of the same planning prompt reported 11,854, 16,650 and 19,943 completion tokens. The visible output was only about 1,300 tokens each time (≈5K characters). So roughly 10–18K tokens per call, and most of the 92–146 seconds, were thinking. That is an inference from output length: no stored field holds the real split.

Where it's lost today

Driver Provider field What MJ does with it
Gemini / Vertex usageMetadata.thoughtsTokenCount (outside candidatesTokenCount) Added into completionTokens (usageFromMetadata, since #5171). The separate number is kept only in ModelSpecificResponseDetails.raw on the non-streaming path.
OpenAI (+ OpenAI-compatible gateways) completion_tokens_details.reasoning_tokens (inside completion_tokens) Copied to ModelSpecificResponseDetails.usage_details.reasoning_tokens, non-streaming only.
Anthropic Thinking is inside output_tokens (thinking_usage.output_tokens where reported) Copied to ModelSpecificResponseDetails.thinking_tokens, non-streaming only.

On the streaming paths, ModelSpecificResponseDetails is null, so the split is lost completely. Agents usually run streaming, so for most agent prompt runs there is no record at all.

Proposal

  1. ModelUsage.thinkingTokens?: number: the number of completionTokens that were thinking. It's a subset of completionTokens, not added on top, so totals and cost don't change. Document it next to cacheReadTokens, which already describes how each driver normalizes.
  2. Every driver fills it in, streaming and non-streaming: Gemini/Vertex from thoughtsTokenCount; OpenAI, Azure and the gateways from reasoning_tokens; Anthropic and Bedrock Claude from the thinking usage they report; any other driver that reports reasoning (xAI, DeepSeek, Groq reasoning models, ...). Leave it null when the provider doesn't report it, rather than 0.
  3. AIPromptRun.TokensThinking (INT NULL) and TokensThinkingRollup, saved by AIPromptRunner and rolled up like TokensCacheRead / TokensCacheReadRollup. Add it to the agent run totals as well.
  4. Cost: today every provider prices thinking at the output rate, so no change is needed. If a model's price list ever sets a separate thinking price, the field is there to use it.
  5. Optional: store the usage block in ModelSpecificResponseDetails on streaming runs too, so streaming and non-streaming runs can be checked the same way.

Why it matters

  • Tuning effort levels (#5530, #5171) can't be measured without it. Today the only way is to switch a prompt to non-streaming and read the raw response.
  • Latency triage: on thinking models, output speed is roughly constant, so duration depends on completion tokens, and you need the split to know whether to cut the effort level or the size of the output.
  • Usage analytics (#4396): "thinking spend by agent or prompt" becomes one query.

Acceptance

  • ModelUsage.thinkingTokens is set by the Gemini, Vertex, OpenAI (and inheriting gateways), Anthropic and Bedrock drivers, in both streaming and non-streaming, with a unit test for each path.
  • AIPromptRun.TokensThinking and its rollup are saved, with a migration and CodeGen.
  • TokensCompletion and Cost are unchanged for the same responses (regression tests).

Related: #5530, #5171, #5339 (the same gap for Gemini Live, closed), #4396.

🤖 Generated with Claude Code

Lingua principale
TSQL
Stelle
32
Fork
7
Merge medio
1g 16h
PR unite (30g)
504

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di MemberJunction/MJ

Tutte le issue di MemberJunction/MJ

Issue simili

Altre issue su AI Infra & Agents

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.