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

オープン
#6,807 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
34/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
sql, typescript
領域
ai, backend

調査の方向性

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 の本文から書いたものです。

説明

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

主要言語
TSQL
スター
32
フォーク
7
平均マージ
1日 16時間
マージ済み PR(30日)
504

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

MemberJunction/MJ のほかの issue

MemberJunction/MJ の issue をすべて見る

似ている issue

AI Infra & Agents の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。