"""Batch against real-time LLM API cost: the daily and 30-day cost of a workload on the real-time API and on the vendor's batch endpoint, and which one the deadline allows. Inputs: input_usd_per_mtok, output_usd_per_mtok (list prices, 0 to 1000, no default); batch_discount_pct (0 to 100, no default) and batch_sla_hours (1 to 720, no default), from the vendor's batch documentation; jobs_per_day (0 to 100000000, default 500); input_tokens_per_job (default 15000) and output_tokens_per_job (default 1500), 0 to 10000000; deadline_hours (0.01 to 8760, default 24). Returns realtimeCostPerDay, batchCostPerDay, effectiveCostPerDay, savingsPerDay and the same per 30-day month (USD), batchEligible and usingBatch. mysi, Batch against real-time LLM cost, mysi.dev/batch-vs-realtime-cost, version 1, changed 2026-12-04. MIT. """ def _batch_vs_realtime_cost_num(name, value, low, high): if isinstance(value, bool) or not isinstance(value, (int, float)) or not low <= value <= high: raise ValueError(f"{name} must be a number from {low} to {high}") return float(value) def batch_vs_realtime_cost(input_usd_per_mtok, output_usd_per_mtok, batch_discount_pct, batch_sla_hours, jobs_per_day=500, input_tokens_per_job=15000, output_tokens_per_job=1500, deadline_hours=24): num = _batch_vs_realtime_cost_num p_in = num("input_usd_per_mtok", input_usd_per_mtok, 0, 1000) p_out = num("output_usd_per_mtok", output_usd_per_mtok, 0, 1000) discount = num("batch_discount_pct", batch_discount_pct, 0, 100) sla = num("batch_sla_hours", batch_sla_hours, 1, 720) jobs = num("jobs_per_day", jobs_per_day, 0, 100000000) t_in = num("input_tokens_per_job", input_tokens_per_job, 0, 10000000) t_out = num("output_tokens_per_job", output_tokens_per_job, 0, 10000000) deadline = num("deadline_hours", deadline_hours, 0.01, 8760) # prices are per million tokens; batch terms: Anthropic, OpenAI and Google batch docs (inputs) per_job = (t_in * p_in + t_out * p_out) / 1000000 realtime = per_job * jobs batch = realtime * (1 - discount / 100) eligible = deadline >= sla effective = batch if eligible else realtime return { "realtimeCostPerDay": realtime, "batchCostPerDay": batch, "effectiveCostPerDay": effective, "savingsPerDay": realtime - effective, "realtimeCostPerMonth": realtime * 30, # a month is defined as 30 identical days "batchCostPerMonth": batch * 30, "effectiveCostPerMonth": effective * 30, "savingsPerMonth": (realtime - effective) * 30, "batchEligible": eligible, "usingBatch": eligible, } BATCH_VS_REALTIME_COST_CASES = [ ({"input_usd_per_mtok": 2, "output_usd_per_mtok": 10, "batch_discount_pct": 50, "batch_sla_hours": 24}, {"realtimeCostPerDay": 22.5, "effectiveCostPerMonth": 337.5, "savingsPerMonth": 337.5}), ({"input_usd_per_mtok": 5, "output_usd_per_mtok": 25, "batch_discount_pct": 50, "batch_sla_hours": 24, "jobs_per_day": 120, "input_tokens_per_job": 40000, "output_tokens_per_job": 3000, "deadline_hours": 6}, {"realtimeCostPerMonth": 990.0, "effectiveCostPerMonth": 990.0, "savingsPerMonth": 0.0}), ({"input_usd_per_mtok": 0.75, "output_usd_per_mtok": 4.5, "batch_discount_pct": 50, "batch_sla_hours": 24, "jobs_per_day": 2000, "input_tokens_per_job": 8000, "output_tokens_per_job": 800, "deadline_hours": 48}, {"realtimeCostPerMonth": 576.0, "effectiveCostPerMonth": 288.0, "savingsPerMonth": 288.0}), ] def batch_vs_realtime_cost_check(tolerance=1e-3): for given, want in BATCH_VS_REALTIME_COST_CASES: got = batch_vs_realtime_cost(**given) for name, value in want.items(): close = abs(got[name] - value) <= tolerance * max(1, abs(value)) assert close, (name, got[name], value) return "ok" if __name__ == "__main__": import json, sys batch_vs_realtime_cost_check() given = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a) js = lambda v: v.lstrip("-")[:1].isdigit() or v[:1] in '[{".' or v in ("true", "false", "null") given = {k: json.loads(v) if js(v) else v for k, v in given.items()} if given: print(json.dumps(batch_vs_realtime_cost(**given)))