AI Prompt Runs: record thinking tokens separately (TokensThinking), for every driver and in streaming
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 34/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- sql, typescript
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- TSQL
- スター
- 32
- フォーク
- 7
- 平均マージ
- 1日 16時間
- マージ済み PR(30日)
- 504
環境構築
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
MemberJunction/MJ のほかの issue
-
DBAutoDoc: export --state-file writes SQL Server SQL for a PostgreSQL database, ignoring --providerオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
MemberJunction/MJ#7094 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 85/100
MemberJunction/MJ#6971 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
MemberJunction/MJ#6947 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
MemberJunction/MJ#6800 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
MemberJunction/MJ#6746 ·
メンテナーはふだん 1 日以内に返信
MemberJunction/MJ の issue をすべて見る
似ている issue
-
[Feature]: Skill Workshop skill diet should use the configured utility model, not always the primaryオープンclawsweeper: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
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
openclaw/openclaw#210348 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
ai-generated tests
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
danielmiessler/LifeOS#3121 ·
メンテナーはふだん 5 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
diegosouzapw/OmniRoute#21908 ·
-
bug help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100