trace: the usage a Pi tool reports on its own result is left out of the session's usage

未关闭
#352 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Shizoqua 已经在做这个了。

开始于 2026年11月29日。

  • #354 来自 @Shizoqua —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
backend

调研方向

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 小时
30 天内合并 PR
123

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/TokenEconomizer 的其他 Issue

查看 microsoft/TokenEconomizer 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。