"""Verification 2 — recomptage independant depuis les JSONL bruts. N'importe aucun code d'analyse existant. Si un chiffre du papier ne tombe pas ici, il ne va pas dans le papier.""" import glob, json, os from collections import defaultdict ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) files = sorted(glob.glob(os.path.join(ROOT, "*", "raw", "*.jsonl"))) tot_obs = tot_err = 0 tot_cost = 0.0 tot_tok = 0 lat_all = [] par_fichier = [] for f in files: n_obs = n_err = 0; cost = 0.0; tok = 0; lats = [] for line in open(f): r = json.loads(line) k = r.get("kind") if k == "obs": n_obs += 1 if r.get("cost"): cost += r["cost"] if r.get("input_tokens"): tok += r["input_tokens"] if r.get("latency_ms"): lats.append(r["latency_ms"]) elif k in ("error", "http_error"): n_err += 1 elif k == "rtt": pass par_fichier.append((os.path.basename(f), n_obs, n_err, cost, tok)) tot_obs += n_obs; tot_err += n_err; tot_cost += cost; tot_tok += tok lat_all += lats print("=" * 92) print("RECOMPTAGE INDEPENDANT — toutes les donnees brutes") print("=" * 92) print(f"{'fichier':46s} {'obs':>7s} {'err':>5s} {'cout $':>10s} {'tokens':>10s}") print("-" * 92) for nom, n, e, c, t in par_fichier: print(f"{nom:46s} {n:7d} {e:5d} {c:10.5f} {t:10,d}") print("-" * 92) print(f"{'TOTAL':46s} {tot_obs:7d} {tot_err:5d} {tot_cost:10.5f} {tot_tok:10,d}") lat_all.sort() def p(q): return lat_all[min(len(lat_all)-1, int(len(lat_all)*q))] print(f"\nAppels reussis : {tot_obs:,}") print(f"Echecs : {tot_err} ({tot_err/(tot_obs+tot_err)*100:.2f} %)") print(f"Cout total mesure : ${tot_cost:.4f}") print(f"Tokens d'entree : {tot_tok:,}") print(f"Cout / 1000 appels : ${tot_cost/tot_obs*1000:.4f}") print(f"Latence p50/p95/p99 : {p(.50):.0f} / {p(.95):.0f} / {p(.99):.0f} ms (n={len(lat_all):,})") # le fichier de concurrence n'enregistre pas les tokens : on l'exclut du ratio cost_px = sum(c for n, _, _, c, t in par_fichier if t > 0) tok_px = sum(t for _, _, _, _, t in par_fichier if t > 0) print(f"Prix unitaire verifie : ${cost_px/tok_px*1_000_000:.6f} / million de tokens " f"(annonce : $0.042000)") print(f" (calcul sur {tok_px:,} tokens ; le fichier de concurrence est exclu,") print(f" il n'enregistrait pas les tokens)")