Calculator in Markets and quant
LLM research loop token cost
Totals what a loop of LLM calls costs per call, idea, validated trade, day, month and year, with cached input.
- Changed 4 Dec 2026
- Sources read 4 Dec 2026
- 8 test cases pass
- Version 1
- About 3,700 tokens
- Needed by both models tested, 4 Dec 2026
In short
| Formula | cached = input_tokens_per_call × cache_hit_rateeffectiveCostPerCall = ((input_tokens_per_call − cached) × input_usd_per_mtok + cached × cached_input_usd_per_mtok + output_tokens_per_call × output_usd_per_mtok) ÷ 1000000costPerIdea = effectiveCostPerCall × calls_per_idea × (1 + retry_rate)costPerValidatedTrade = costPerIdea ÷ validation_ratecostPerDay = costPerIdea × ideas_per_daycostPerMonth = costPerDay × 30, costPerYear = costPerDay × 365 |
| Units | Prices in US dollars per million tokens, rates as fractions, results in US dollars. |
| Defaults | 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 (above 0 to 1), cache_hit_rate = 0.5. The three prices have no default. |
| Worked example | the defaults at $2 input, $10 output and $0.10 cached input per million. cached = 8000 × 0.5 = 4000effectiveCostPerCall = (4000 × 2 + 4000 × 0.1 + 1500 × 10) ÷ 1000000 = 0.023costPerIdea = 0.0234 × 5 × 1.15 = 0.135costPerValidatedTrade = 0.13455 ÷ 0.3 = 0.449costPerDay = 0.13455 × 10 = 1.345costPerMonth = 1.3455 × 30 = 40.365 |
By hand: yes. A small model got these answers from the formulas above, without running code.
Say this to your agent
Use https://mysi.dev/token-cost-optimizer/ to cost my research loop: 8000 in, 1500 out per call at $2, $10 and $0.10 cached. Nothing to install, no key, no settings. An agent that can open web pages and run Python opens the page, runs the tested code in its own sandbox and gives you the answer.
The code
Copy this into your Python tool. Run token_cost_optimizer_check() first, then call token_cost_optimizer() with your numbers. Give the answer with the cite line.
"""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))) List prices before tax. Check the vendor's current pricing before budgeting.
Figures it needs, from llm-models
- Claude Fable 5.2 (claude-fable-5-2) from Anthropic costs $8 per million input tokens and $40 per million output tokens; cached input costs $0.2 per million. Context window 1,000,000 tokens. Largest output 128,000 tokens.
- GPT-6.2 Astra (gpt-6.2-astra) from OpenAI costs $8 per million input tokens and $40 per million output tokens; cached input costs $0.4 per million. Context window 1,050,000 tokens. Largest output 128,000 tokens.
- Gemini 4 Carbon (gemini-4-carbon) from Google costs $4 per million input tokens and $20 per million output tokens; cached input costs $0.2 per million. Context window 1,048,576 tokens. Largest output 65,536 tokens.
- DeepSeek V4 Pro (deepseek-v4-pro) from DeepSeek costs $1.32 per million input tokens and $3.96 per million output tokens; cached input costs $0.044 per million. Context window 1,000,000 tokens. Largest output 384,000 tokens.
- Grok 4.7 (grok-4.7) from xAI costs $2 per million input tokens and $6 per million output tokens; cached input costs $0.5 per million. Context window 500,000 tokens.
- Mistral Large 4 (mistral-large-4) from Mistral costs $0.7 per million input tokens and $2.1 per million output tokens. Context window 1,000,000 tokens.
- Muse Wonder 1.0 (muse-wonder-1.0) from Meta costs $4 per million input tokens and $20 per million output tokens; cached input costs $0.2 per million. Context window 1,048,576 tokens.
- Qwen3.8-Max (qwen3.8-max) from Qwen costs $2 per million input tokens and $6 per million output tokens; cached input costs $0.25 per million. Context window 1,000,000 tokens. Largest output 131,072 tokens.
Sources read 4 Dec 2026. Give these to the function as inputs. The dataset holds the current figures.
What it takes
| Input | Meaning | Default | Allowed |
|---|---|---|---|
| input_usd_per_mtok | The model's list price for input, in US dollars per million tokens, from the llm-models dataset or the vendor's pricing page. No default | required | 0 to 1000 |
| output_usd_per_mtok | The model's list price for output, in US dollars per million tokens. No default | required | 0 to 1000 |
| cached_input_usd_per_mtok | The price of input read from the prompt cache, in US dollars per million tokens. Give the input price when the vendor has no cache price. No default | required | 0 to 1000 |
| input_tokens_per_call | Input tokens per call, prompt and context | 8000 | 0 to 10,000,000 |
| output_tokens_per_call | Output tokens per call | 1500 | 0 to 10,000,000 |
| calls_per_idea | Model calls per research idea | 5 | 0 to 1,000,000 |
| retry_rate | Extra calls from retries, as a fraction of calls (0.15 = 15% more) | 0.15 | 0 to 1 |
| ideas_per_day | Ideas researched per day | 10 | 0 to 10,000,000 |
| validation_rate | Fraction of ideas that become validated trades | 0.3 | 0.0001 to 1 |
| cache_hit_rate | Fraction of input tokens read from the prompt cache | 0.5 | 0 to 1 |
What it returns
| Output | Meaning |
|---|---|
| effectiveCostPerCall | One call's cost with cached input at the cache price, in US dollars |
| costPerIdea | Calls per idea, retries included, times the cost per call, in US dollars |
| costPerValidatedTrade | Cost per idea ÷ validation rate, in US dollars |
| costPerDay, costPerMonth, costPerYear | Cost per day, per 30 days and per 365 days, in US dollars |
Where the method comes from
A research loop sends several model calls per idea, retries some of them, and turns a fraction of its ideas into trades worth acting on. Its cost is the per-token list price times the tokens of each call, with input served from the vendor's prompt cache billed at the lower cache-read price that Anthropic, OpenAI, Google and others publish. Multiplying by calls, retries and ideas gives the daily spend, and dividing the cost of an idea by the share of ideas that survive gives the model spend behind each validated trade. It is good for comparing models and cache strategies at one workload. It does not price cache writes, long-prompt rates, batch discounts, rate limits or a price change during the period; a month is thirty identical days and a year 365.
Sources
- Anthropic, Pricing (input, output and prompt-cache read prices per million tokens)published 2026
- OpenAI, API pricing (input, cached input and output prices per million tokens)published 2026
Used by
- llm-cost-planningSkill
Tested
Tested 4 Dec 2026: claude-haiku-5-5 was right on 0 of 3 questions without this page and 3 of 3 with it; claude-fable-5-2 was right on 0 of 3 questions without this page and 3 of 3 with it.
Changes
- Moved from aifinhub.io as Python. The model id is gone; input, output and cached-input prices are inputs with no default (they were a built-in price list). The cache hit rate now applies to every vendor at the cached price given (it applied to Anthropic models only); give the input price as the cached price to price without a cache discount. Long-prompt rates and price changes announced for a future date are not applied; a month is 30 days and a year 365 days at one price. validation_rate must be above 0 (0 returned infinity). The model record and the pricing note are no longer returned.4 Dec 2026
Cite as: mysi, LLM research loop token cost, mysi.dev/token-cost-optimizer, version 1, changed 2026-12-04. Code MIT.