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

Abierto
#6,807 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
34/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
sql, typescript
Área
ai, backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
TSQL
Estrellas
32
Forks
7
Merge medio
1 d 16 h
PR fusionados (30 d)
504

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de MemberJunction/MJ

Todos los issues de MemberJunction/MJ

Issues similares

Más issues de AI Infra & Agents

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.