#!/usr/bin/env python3 """Aggregate results/*.jsonl into a comparison table.""" from __future__ import annotations import json import sys from pathlib import Path import pandas as pd sys.path.insert(0, str(Path(__file__).resolve().parent / "bench")) import oolong # noqa: E402 RESULTS = Path(__file__).resolve().parent / "results" # Published list prices, USD per million tokens (input, output). Cache reads bill # at 0.1x input and cache writes at 1.25x input. PRICES = { "claude-haiku-4-5-20251001": (1.0, 5.0), "claude-sonnet-4-6": (3.0, 15.0), "claude-opus-4-8": (15.0, 75.0), } def api_cost(row, model: str) -> float: """Cost from raw token counts at list prices. The CLI's own total_cost_usd also bills a small hidden Haiku call it makes per invocation, so this is the apples-to-apples number for model comparisons. """ pin, pout = PRICES[model.replace("[1m]", "")] return ( row.get("usage_input_tokens", 0) * pin + row.get("usage_cache_creation_tokens", 0) * pin * 1.25 + row.get("usage_cache_read_tokens", 0) * pin * 0.1 + row.get("usage_output_tokens", 0) * pout ) / 1e6 def load(tag: str) -> pd.DataFrame: rows = [json.loads(l) for l in (RESULTS / f"{tag}.jsonl").read_text().splitlines() if l.strip()] df = pd.json_normalize(rows, sep="_") meta = json.loads((RESULTS / f"{tag}.meta.json").read_text()) df = df.assign(tag=tag, **meta) df["api_cost"] = df.apply(lambda r: api_cost(r, meta["model"]), axis=1) # Guard the headline number against the answer extractor. `score` uses our # clean_final() pre-step; `score_raw` is the official parser on the untouched # response. If the two disagree by much, the gap is a parsing artifact rather # than a capability difference. df["score_raw"] = [ oolong.score_response({"answer": repr([g]), "answer_type": t}, a)["score"] if a else 0.0 for g, t, a in zip(df.gold, df.answer_type, df.full_answer) ] return df def main(tags: list[str]) -> None: tags = tags or sorted(p.stem for p in RESULTS.glob("*.jsonl")) df = pd.concat([load(t) for t in tags], ignore_index=True) agg = df.groupby(["tag", "mode", "model", "context_len"], as_index=False).agg( n=("score", "size"), score=("score", "mean"), score_raw=("score_raw", "mean"), exact=("score", lambda s: (s == 1.0).mean()), errors=("error", lambda e: e.notna().sum()), cost_per_q=("api_cost", "mean"), cli_cost_per_q=("usage_cost_usd", "mean"), calls_per_q=("usage_calls", "mean"), sec_per_q=("seconds", "mean"), ) for c in ("score", "score_raw", "exact"): agg[c] = agg[c].round(3) for c in ("cost_per_q", "cli_cost_per_q"): agg[c] = agg[c].round(4) agg[["calls_per_q", "sec_per_q"]] = agg[["calls_per_q", "sec_per_q"]].round(1) print(agg.to_string(index=False)) print("\nper source dataset (mean score):") print(df.pivot_table(index="dataset", columns="tag", values="score", aggfunc="mean").round(2).to_string()) print("\nper task (mean score):") print(df.pivot_table(index="task", columns="tag", values="score", aggfunc="mean").round(2).to_string()) if __name__ == "__main__": main(sys.argv[1:])