feat(token-tracking): surface token_usage in ReportData

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
wangwei
2026-07-02 15:14:11 +08:00
co-authored by Copilot
parent 8245c7f9c3
commit 4bb1952348
3 changed files with 64 additions and 1 deletions
+4
View File
@@ -99,6 +99,10 @@ class ReportData(BaseModel):
default_factory=dict,
description="该次运行使用的文档权重配置(来自 scenario.snapshot.yaml)。",
)
token_usage: dict[str, dict[str, int]] = Field(
default_factory=dict,
description="按模型累计的 token 用量:{model: {input_tokens, output_tokens, calls}}。",
)
class RunDetail(BaseModel):
+5 -1
View File
@@ -187,6 +187,9 @@ def build_report(run_dir: Path, metrics: list[str]) -> ReportData:
summary_markdown = run_reader.read_summary_markdown(run_dir)
advice_markdown = run_reader.read_advice_markdown(run_dir)
metric_weights, doc_weights = _read_weights_from_snapshot(run_dir)
# Read once up front so both the empty-frame and full branches can surface it.
metadata = run_reader._read_json(run_dir / "metadata.json")
token_usage = metadata.get("token_usage") or {}
if frame.empty or not metrics:
return ReportData(
@@ -196,6 +199,7 @@ def build_report(run_dir: Path, metrics: list[str]) -> ReportData:
advice_markdown=advice_markdown,
metric_weights=metric_weights,
doc_weights=doc_weights,
token_usage=token_usage,
)
score_rows_list = frame.to_dict(orient="records")
@@ -218,7 +222,6 @@ def build_report(run_dir: Path, metrics: list[str]) -> ReportData:
# Cross-run history: scores of the same question in *other* runs (Approach A —
# on-demand global scan, excluding the run currently being viewed).
metadata = run_reader._read_json(run_dir / "metadata.json")
current_run_id = str(metadata.get("run_id") or run_dir.name)
history_index = question_history.build_question_history_index(
exclude_run_id=current_run_id
@@ -235,4 +238,5 @@ def build_report(run_dir: Path, metrics: list[str]) -> ReportData:
weighted_score_mean=_round_or_none(overall_ws),
metric_weights=metric_weights,
doc_weights=doc_weights,
token_usage=token_usage,
)