AI Prompt Runs: record thinking tokens separately (TokensThinking), for every driver and in streaming
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
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
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
ModelUsage.thinkingTokens?: number: the number ofcompletionTokensthat were thinking. It's a subset ofcompletionTokens, not added on top, so totals and cost don't change. Document it next tocacheReadTokens, which already describes how each driver normalizes.- Every driver fills it in, streaming and non-streaming: Gemini/Vertex from
thoughtsTokenCount; OpenAI, Azure and the gateways fromreasoning_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. AIPromptRun.TokensThinking(INT NULL) andTokensThinkingRollup, saved byAIPromptRunnerand rolled up likeTokensCacheRead/TokensCacheReadRollup. Add it to the agent run totals as well.- 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.
- Optional: store the usage block in
ModelSpecificResponseDetailson 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.thinkingTokensis 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.TokensThinkingand its rollup are saved, with a migration and CodeGen. -
TokensCompletionandCostare 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
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de MemberJunction/MJ
-
DBAutoDoc: export --state-file writes SQL Server SQL for a PostgreSQL database, ignoring --providerAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 73/100
MemberJunction/MJ#7094 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 85/100
MemberJunction/MJ#6971 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
MemberJunction/MJ#6947 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
MemberJunction/MJ#6800 ·
Los mantenedores suelen responder en 1 día
-
Convention: saved-query discoverability follows Status — Approved is canon, Pending is a draftAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
MemberJunction/MJ#6746 ·
Los mantenedores suelen responder en 1 día
Todos los issues de MemberJunction/MJ
Issues similares
-
[Feature]: Skill Workshop skill diet should use the configured utility model, not always the primaryAbiertoclawsweeper:fix-shape-clear clawsweeper:needs-live-repro clawsweeper:needs-maintainer-review clawsweeper:needs-product-decision clawsweeper:no-new-fix-pr impact:auth-provider issue-rating: 🐚 platinum hermit P2
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
openclaw/openclaw#210348 · 1 comentario · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
ai-generated tests
Dificultad 1/5 Menos de una hora Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
danielmiessler/LifeOS#3121 ·
Los mantenedores suelen responder en 5 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
diegosouzapw/OmniRoute#21908 ·
-
bug help wanted
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100