trace: the usage a Pi tool reports on its own result is left out of the session's usage
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
Start with tokeneconomizer/trace/pi.py and inspect how pi_session_usage totals assistant messages and session entries; compare its handling with the toolResult usage in the reproduction. Add the reproduced session as a regression case in the relevant tests, then confirm totals include both calls: 150 input tokens, 300 output tokens, 2 requests, and $0.15 cost. A pull request is already open for this issue.
索引モデルが issue の本文から書いたものです。
説明
tokeneconomizer/trace/pi.py's pi_session_usage adds up the usage of the active branch's assistant messages and its usage, compaction and branch_summary entries, but not the usage on a toolResult message. A Pi tool that calls a model itself reports that usage on its own result: pi-ai's ToolResultMessage has an optional usage, documented as "Usage from the tool execution itself" (packages/ai/src/types.ts), and Pi's own session stats add it to the session's totals (AgentSession.getSessionStats in packages/coding-agent/src/core/agent-session.ts). So a session whose tools made model calls is recorded in the ledger with fewer tokens, requests and less cost than Pi itself reports for it.
Reproduction (offline):
import json, os, tempfile
from tokeneconomizer import trace
u = {"input": 100, "output": 200, "cacheRead": 1000, "cacheWrite": 0, "cost": {"total": 0.1}}
tool = {"input": 50, "output": 100, "cacheRead": 500, "cacheWrite": 0, "cost": {"total": 0.05}}
entries = [
{"type": "session", "version": 3, "id": "s1", "cwd": "/w/acme/widget"},
{"type": "message", "id": "u1", "parentId": None,
"message": {"role": "user", "content": "fix the parser"}},
{"type": "message", "id": "a1", "parentId": "u1", "message": {
"role": "assistant", "usage": u,
"content": [{"type": "toolCall", "id": "c1", "name": "subagent", "arguments": {}}]}},
{"type": "message", "id": "r1", "parentId": "a1", "message": {
"role": "toolResult", "toolCallId": "c1", "toolName": "subagent", "isError": False,
"content": [{"type": "text", "text": "tests/test_parser.py"}], "usage": tool}},
]
p = os.path.join(tempfile.mkdtemp(), "session.jsonl")
open(p, "w").write("\n".join(json.dumps(e) for e in entries) + "\n")
s = trace.pi_session_usage(p)
print({k: s[k] for k in ("input_tokens", "output_tokens", "requests", "cost_usd_vendor")})
Expected: {'input_tokens': 150, 'output_tokens': 300, 'requests': 2, 'cost_usd_vendor': 0.15}, the agent's call and the tool's.
Actual: {'input_tokens': 100, 'output_tokens': 200, 'requests': 1, 'cost_usd_vendor': 0.1}
Upstream main: 747b8016538c13da7ba9d455b89e5e6595bf5294
Python 3.13.18, Linux
- 主要言語
- Python
- スター
- 7.4k
- フォーク
- 500
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 123
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/TokenEconomizer のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
microsoft/TokenEconomizer#360 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
microskills: bundle exec and pnpm exec are recorded as bundle run and pnpm run対応中かも @Shizoqua が 2 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
microsoft/TokenEconomizer#344 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
microskills: a command wrapped in bash -lc or zsh -lc is not observed対応中かも @Shizoqua が 2 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/TokenEconomizer#343 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
README: state the minimum Python version next to the test instructions対応中かも @rahul05ranjan が 4 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
microsoft/TokenEconomizer#328 · コメント 3 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
loopguard: a PHPUnit or CTest run is judged without its summary, so a failing run can end a turn green対応中かも @Shizoqua が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 35/100
microsoft/TokenEconomizer#353 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
microsoft/TokenEconomizer の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
reasons-gate
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
benthomasson/reasons-service#190 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
ui
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 3 日以内に返信