"""LLM research loop token cost: what a loop of model calls costs per call, per idea, per validated trade, per day, per 30-day month and per 365-day year. Inputs: input_usd_per_mtok, output_usd_per_mtok, cached_input_usd_per_mtok (list prices in USD per million tokens, 0 to 1000, no default); input_tokens_per_call (default 8000) and output_tokens_per_call (default 1500), 0 to 10000000; calls_per_idea (0 to 1000000, default 5); retry_rate (0 to 1, default 0.15); ideas_per_day (0 to 10000000, default 10); validation_rate (0.0001 to 1, default 0.3); cache_hit_rate (0 to 1, default 0.5). Returns effectiveCostPerCall, costPerIdea, costPerValidatedTrade, costPerDay, costPerMonth, costPerYear, all in USD. mysi, LLM research loop token cost, mysi.dev/token-cost-optimizer, version 1, changed 2026-12-04. MIT. """ def _token_cost_optimizer_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 token_cost_optimizer(input_usd_per_mtok, output_usd_per_mtok, cached_input_usd_per_mtok, input_tokens_per_call=8000, output_tokens_per_call=1500, calls_per_idea=5, retry_rate=0.15, ideas_per_day=10, validation_rate=0.3, cache_hit_rate=0.5): num = _token_cost_optimizer_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) p_cached = num("cached_input_usd_per_mtok", cached_input_usd_per_mtok, 0, 1000) t_in = num("input_tokens_per_call", input_tokens_per_call, 0, 10000000) t_out = num("output_tokens_per_call", output_tokens_per_call, 0, 10000000) calls = num("calls_per_idea", calls_per_idea, 0, 1000000) retry = num("retry_rate", retry_rate, 0, 1) ideas = num("ideas_per_day", ideas_per_day, 0, 10000000) valid = num("validation_rate", validation_rate, 0.0001, 1) hit = num("cache_hit_rate", cache_hit_rate, 0, 1) cached = t_in * hit # prices per million tokens; cache-read price from the vendor's pricing page (an input) per_call = ((t_in - cached) * p_in + cached * p_cached + t_out * p_out) / 1000000 per_idea = per_call * calls * (1 + retry) per_day = per_idea * ideas return { "effectiveCostPerCall": per_call, "costPerIdea": per_idea, "costPerValidatedTrade": per_idea / valid, "costPerDay": per_day, "costPerMonth": per_day * 30, # by definition: 30-day month, 365-day year "costPerYear": per_day * 365, } TOKEN_COST_OPTIMIZER_CASES = [ ({"input_usd_per_mtok": 2, "output_usd_per_mtok": 10, "cached_input_usd_per_mtok": 0.1}, {"effectiveCostPerCall": 0.0234, "costPerValidatedTrade": 0.4485, "costPerMonth": 40.365}), ({"input_usd_per_mtok": 5, "output_usd_per_mtok": 25, "cached_input_usd_per_mtok": 0.5, "input_tokens_per_call": 12000, "output_tokens_per_call": 2000, "calls_per_idea": 6, "retry_rate": 0.1, "ideas_per_day": 20, "validation_rate": 0.25, "cache_hit_rate": 0.7}, {"effectiveCostPerCall": 0.0722, "costPerValidatedTrade": 1.90608, "costPerMonth": 285.912}), ({"input_usd_per_mtok": 0.75, "output_usd_per_mtok": 4.5, "cached_input_usd_per_mtok": 0.075, "input_tokens_per_call": 5000, "output_tokens_per_call": 800, "calls_per_idea": 4, "retry_rate": 0.2, "ideas_per_day": 50, "validation_rate": 0.1, "cache_hit_rate": 0}, {"effectiveCostPerCall": 0.00735, "costPerValidatedTrade": 0.3528, "costPerMonth": 52.92}), ] def token_cost_optimizer_check(tolerance=1e-3): for given, want in TOKEN_COST_OPTIMIZER_CASES: got = token_cost_optimizer(**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 token_cost_optimizer_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(token_cost_optimizer(**given)))