92 lines
5.5 KiB
Python
92 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
streamline-retro / stats.py — 一轮(或几段接力)会话的成本与效率画像。
|
|
|
|
python3 stats.py <session-rN.log> [<session-rN.timing.jsonl>]
|
|
|
|
输出:模型、每段会话的 API 调用数与上下文起止 / 中位 / 峰值、token 用量(新输入 / 写缓存 / 读缓存 / 输出)
|
|
与按标价的估算花费、工具结果体量按类别的占比(截图占多少)、工具执行 vs 思考时长、命令里写死的 sleep。
|
|
|
|
为什么有它:2026-10 分析 UI 走查和 bug / Feature 的成本时,同一套口径手算了四遍。几个结论都是从这些数里来的:
|
|
读缓存(= 每一步重读整段上下文)是成本大头;上下文 1M 窗口下从不压缩;截图常占工具输出一半;
|
|
节点 CLI 版本决定默认模型。会话日志要完整的那份(fetch.sh 会从节点 scp,API 那份截在 8 MB)。
|
|
|
|
价格是 2026-10 的标价(USD / 百万 token),只用于估算;内部结算可能不同。Opus 5 的读缓存价未单列,
|
|
按常规的输入价 1/10 假定。
|
|
"""
|
|
import json, re, sys, collections
|
|
|
|
PRICE = { # in, out, cache_read, cache_write(5m)
|
|
"claude-opus-5-5": (4, 20, 0.20, 5.0),
|
|
"claude-opus-5": (5, 25, 0.50, 6.25),
|
|
"claude-opus-4-8": (5, 25, 0.50, 6.25),
|
|
"claude-sonnet-5-5": (2, 10, 0.20, 2.5),
|
|
"claude-fable-5-1": (10, 50, 0.25, 12.5),
|
|
}
|
|
|
|
def main(log, timing=None):
|
|
seg = 0; seen = set(); calls = collections.defaultdict(list); models = collections.Counter()
|
|
by_id = {}; res = collections.Counter(); usage = collections.Counter(); n_img = 0; compactions = 0
|
|
for line in open(log, errors="ignore"):
|
|
try: d = json.loads(line)
|
|
except Exception: continue
|
|
t = d.get("type")
|
|
if t == "system" and d.get("subtype") == "init": seg += 1
|
|
if t == "system" and d.get("subtype") == "compact_boundary": compactions += 1
|
|
if t == "result":
|
|
for k, v in (d.get("usage") or {}).items():
|
|
if isinstance(v, int): usage[k] += v
|
|
if t == "assistant":
|
|
m = d.get("message", {}); models[m.get("model")] += 1
|
|
for c in m.get("content", []):
|
|
if c.get("type") == "tool_use":
|
|
a = json.dumps(c.get("input", {}))[:400]; k = c["name"]
|
|
if k == "Bash":
|
|
k = ("Bash:build/wait" if re.search(r"gradle|xcodebuild|airbuild|wait-for|hotswap|deploy\.sh", a)
|
|
else "Bash:git" if re.search(r"\bgit ", a) else "Bash:frida" if "frida" in a
|
|
else "Bash:device" if re.search(r"adb |catuse|record-evidence|uiautomator", a) else "Bash:other")
|
|
elif k == "Read" and re.search(r"\.(png|jpe?g)", a): k = "Read:image"
|
|
elif k.startswith("mcp__"): k = "mcp:" + k.split("__")[-1]
|
|
by_id[c["id"]] = k
|
|
if m.get("id") in seen or d.get("parent_tool_use_id"): continue
|
|
seen.add(m.get("id")); u = m.get("usage") or {}
|
|
calls[seg].append((u.get("input_tokens") or 0) + (u.get("cache_read_input_tokens") or 0) + (u.get("cache_creation_input_tokens") or 0))
|
|
if t == "user" and isinstance(d.get("message", {}).get("content"), list):
|
|
for c in d["message"]["content"]:
|
|
if c.get("type") != "tool_result": continue
|
|
k = by_id.get(c.get("tool_use_id"), "?"); cont = c.get("content"); n = 0
|
|
if isinstance(cont, list):
|
|
for x in cont:
|
|
if x.get("type") == "image": n += 1600; n_img += 1
|
|
else: n += len(x.get("text", "")) // 3
|
|
else: n = len(str(cont)) // 3
|
|
res[k] += n
|
|
model = models.most_common(1)[0][0] if models else "?"
|
|
print(f"模型: {dict(models.most_common(3))}")
|
|
for s, cs in sorted(calls.items()):
|
|
srt = sorted(cs)
|
|
print(f"段 {s}: API 调用 {len(cs)},上下文 起 {cs[0]//1000}k → 中位 {srt[len(srt)//2]//1000}k → 末 {cs[-1]//1000}k(峰值 {max(cs)//1000}k),读入合计 {sum(cs)/1e6:.0f}M")
|
|
print(f"自动压缩: {compactions} 次")
|
|
if usage:
|
|
i, o, cr, cw = usage["input_tokens"], usage["output_tokens"], usage["cache_read_input_tokens"], usage["cache_creation_input_tokens"]
|
|
print(f"token: 新输入 {i:,} · 写缓存 {cw:,} · 读缓存 {cr:,} · 输出 {o:,}")
|
|
p = PRICE.get(model)
|
|
if p:
|
|
cost = i * p[0] / 1e6 + o * p[1] / 1e6 + cr * p[2] / 1e6 + cw * p[3] / 1e6
|
|
print(f"按 {model} 标价估算: ${cost:.1f}(读缓存 ${cr*p[2]/1e6:.1f} · 输出 ${o*p[1]/1e6:.1f} · 写缓存 ${cw*p[3]/1e6:.1f};写缓存按 5 分钟档)")
|
|
else:
|
|
print(f"(没有 {model} 的价格,不估算)")
|
|
tot = sum(res.values()) or 1
|
|
print(f"工具结果体量(估 token,共 {tot//1000}k;截图 {n_img} 张):")
|
|
for k, v in res.most_common(8): print(f" {k:<18} {v//1000:>6}k {100*v/tot:>3.0f}%")
|
|
if timing:
|
|
rows = [json.loads(l) for l in open(timing) if l.strip()]
|
|
tool = sum(r.get("ms") or 0 for r in rows) / 6e4; gap = sum(r.get("gap") or 0 for r in rows) / 6e4
|
|
sl = sum(float(m) for r in rows for m in re.findall(r"sleep (\d+(?:\.\d+)?)", r.get("a") or ""))
|
|
to = sum(1 for r in rows if (r.get("ms") or 0) >= 119_000)
|
|
print(f"时长: 工具执行 {tool:.0f} 分钟 · 思考 {gap:.0f} 分钟 · 命令里写死的 sleep {sl/60:.1f} 分钟 · 撞 120s 超时 {to} 次")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2: sys.exit(__doc__)
|
|
main(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else None)
|