mysi Find

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.

In short

Formulacached = 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
UnitsPrices in US dollars per million tokens, rates as fractions, results in US dollars.
Defaultsinput_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 examplethe 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)))
Raw file

List prices before tax. Check the vendor's current pricing before budgeting.

Figures it needs, from llm-models

Sources read 4 Dec 2026. Give these to the function as inputs. The dataset holds the current figures.

What it takes

InputMeaningDefaultAllowed
input_usd_per_mtokThe model's list price for input, in US dollars per million tokens, from the llm-models dataset or the vendor's pricing page. No defaultrequired0 to 1000
output_usd_per_mtokThe model's list price for output, in US dollars per million tokens. No defaultrequired0 to 1000
cached_input_usd_per_mtokThe 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 defaultrequired0 to 1000
input_tokens_per_callInput tokens per call, prompt and context80000 to 10,000,000
output_tokens_per_callOutput tokens per call15000 to 10,000,000
calls_per_ideaModel calls per research idea50 to 1,000,000
retry_rateExtra calls from retries, as a fraction of calls (0.15 = 15% more)0.150 to 1
ideas_per_dayIdeas researched per day100 to 10,000,000
validation_rateFraction of ideas that become validated trades0.30.0001 to 1
cache_hit_rateFraction of input tokens read from the prompt cache0.50 to 1

What it returns

OutputMeaning
effectiveCostPerCallOne call's cost with cached input at the cache price, in US dollars
costPerIdeaCalls per idea, retries included, times the cost per call, in US dollars
costPerValidatedTradeCost per idea ÷ validation rate, in US dollars
costPerDay, costPerMonth, costPerYearCost 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

Used by

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

Cite as: mysi, LLM research loop token cost, mysi.dev/token-cost-optimizer, version 1, changed 2026-12-04. Code MIT.