From 0e31725192736fcf6e791e19c7ff79757a6f085c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=80=D1=83=D0=B1=D0=B8=D0=BD=20=D0=A4=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D0=BF=D0=BF?= <289370046+zarubinphil@users.noreply.github.com> Date: Wed, 17 Jun 2026 17:01:58 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20token-spend=20=D1=80=D0=B0=D0=B7=D0=B1?= =?UTF-8?q?=D0=B8=D0=B2=D0=BA=D0=B0=20=D0=BF=D0=BE=20=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D1=8F=D0=BC=20(per-model=20rates=20+=20$)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Группировка usage по message.model: блок на модель (input/output/ cache-write/cache-read/всего/≈$), сортировка по убыванию трат, затем общий ИТОГО. Стоимость по корректным per-model rates (Opus/Sonnet/ Haiku), не плоский Opus. Synthetic-сообщения отфильтрованы. Даёт визуальную проверку правила «качество-на-токен»: куда реально ушёл бюджет по tier'ам. Live синхронизирован (бэкап .bak-permodel-*). Co-Authored-By: Claude Opus 4.8 (1M context) --- .../scripts/executable_token-spend.sh | 79 ++++++++++++++----- 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/chezmoi/dot_claude/scripts/executable_token-spend.sh b/chezmoi/dot_claude/scripts/executable_token-spend.sh index ee4c162..83520b3 100755 --- a/chezmoi/dot_claude/scripts/executable_token-spend.sh +++ b/chezmoi/dot_claude/scripts/executable_token-spend.sh @@ -16,31 +16,72 @@ fi python3 - "$F" <<'PY' import json, sys f = sys.argv[1] -it = ot = cc = cr = n = 0 -models = set() + +# Rate-карта за Mtok [in, out, cache-write, cache-read]; ключ — подстрока model id. +RATES = { + "opus": [15.0, 75.0, 18.75, 1.50], + "sonnet": [ 3.0, 15.0, 3.75, 0.30], + "haiku": [ 1.0, 5.0, 1.25, 0.10], +} +def rate_for(model): + ml = (model or "").lower() + for k, r in RATES.items(): + if k in ml: return r + return RATES["opus"] # неизвестная → Opus-rate (консервативно) + +def short(model): + ml = (model or "?").lower() + for k in RATES: + if k in ml: return k + return model or "?" + +# Агрегация по модели: {model: [in, out, cw, cr, calls]} +per = {} +n = 0 for line in open(f): try: d = json.loads(line) except Exception: continue m = d.get("message", {}) u = m.get("usage") or {} if not u: continue + mid = m.get("model") or "?" + if mid == "": continue # синтетические сообщения — без реальных токенов n += 1 - it += u.get("input_tokens", 0); ot += u.get("output_tokens", 0) - cc += u.get("cache_creation_input_tokens", 0); cr += u.get("cache_read_input_tokens", 0) - if m.get("model"): models.add(m["model"]) -# Opus-rate (груб): in $15 / out $75 / cache-write $18.75 / cache-read $1.50 за Mtok -cost = it/1e6*15 + ot/1e6*75 + cc/1e6*18.75 + cr/1e6*1.5 -tot = it + ot + cc + cr + a = per.setdefault(mid, [0, 0, 0, 0, 0]) + a[0] += u.get("input_tokens", 0); a[1] += u.get("output_tokens", 0) + a[2] += u.get("cache_creation_input_tokens", 0); a[3] += u.get("cache_read_input_tokens", 0) + a[4] += 1 + +def cost_of(it, ot, cc, cr, model): + r = rate_for(model) + return it/1e6*r[0] + ot/1e6*r[1] + cc/1e6*r[2] + cr/1e6*r[3] + +T_it = T_ot = T_cc = T_cr = T_cost = 0 print(f"сессия: {f.split('/')[-1]}") -print(f"вызовов с usage: {n} | модели: {', '.join(models) or '—'}") -print(f"input(uncached): {it:>13,}") -print(f"output: {ot:>13,}") -print(f"cache-write: {cc:>13,}") -print(f"cache-read: {cr:>13,}") -print(f"ВСЕГО: {tot:>13,}") -print(f"≈ стоимость: ${cost:,.2f} (груб. Opus-rate)") -# доминанта -parts = {"input": it, "output": ot, "cache-write": cc, "cache-read": cr} -dom = max(parts, key=parts.get) -print(f"доминанта: {dom} ({parts[dom]/tot*100:.0f}%)") +print(f"вызовов с usage: {n} | моделей: {len(per)}") +# Блок на модель (сортировка по убыванию $) +rows = sorted(per.items(), key=lambda kv: -cost_of(kv[1][0], kv[1][1], kv[1][2], kv[1][3], kv[0])) +for mid, (it, ot, cc, cr, calls) in rows: + c = cost_of(it, ot, cc, cr, mid) + tot = it + ot + cc + cr + T_it += it; T_ot += ot; T_cc += cc; T_cr += cr; T_cost += c + print(f"\n── {short(mid)} ({mid}) · {calls} выз. ──") + print(f" input(uncached): {it:>13,}") + print(f" output: {ot:>13,}") + print(f" cache-write: {cc:>13,}") + print(f" cache-read: {cr:>13,}") + print(f" всего: {tot:>13,} ≈ ${c:,.2f}") + +T_tot = T_it + T_ot + T_cc + T_cr +print(f"\n═══ ИТОГО ({len(per)} модел.) ═══") +print(f"input(uncached): {T_it:>13,}") +print(f"output: {T_ot:>13,}") +print(f"cache-write: {T_cc:>13,}") +print(f"cache-read: {T_cr:>13,}") +print(f"ВСЕГО: {T_tot:>13,}") +print(f"≈ стоимость: ${T_cost:,.2f} (per-model rates)") +if T_tot: + parts = {"input": T_it, "output": T_ot, "cache-write": T_cc, "cache-read": T_cr} + dom = max(parts, key=parts.get) + print(f"доминанта: {dom} ({parts[dom]/T_tot*100:.0f}%)") PY