loki-mode

Cost controls

You set the ceiling. We stop at it, and the receipt tells you where the money went.

Why this page exists

“I paid for the AI’s own mistakes” is one of the sharpest complaints in this category, and it is worth being precise about how our model differs.

Our model is different in kind: you bring your own provider credentials. We never bill you, because we are never in the payment path. What we owe you instead is a hard ceiling and an honest account of what was spent – which is what this page describes.

The three caps

They bound different things, and a run that stalls needs all three.

Cap Bounds Default
LOKI_BUDGET_LIMIT Total spend, in USD none on the Loki 10 engine; 100.00 on the previous loop (breaching pauses the run)
LOKI_MAX_ITERATIONS Number of iterations 1000
LOKI_MAX_DURATION Wall-clock time unset (no cap)
LOKI_BUDGET_LIMIT=25 loki start ./prd.md
loki start ./prd.md --max-duration 90m
loki config set budget 25

Why three and not one. Spend and iterations both assume forward progress. A run that stalls – a hung provider call, a wedged subprocess – burns hours while spending almost nothing and completing no iteration, so neither of those breakers ever trips. The wall-clock cap is the one that catches it. We added it after a run burned $34 reaching an external timeout.

Time and progress (Loki 10 engine)

There is no default time limit. --max-time 90m (or LOKI_RUN_DEADLINE_S) sets an outer limit the run finishes inside; --max-duration, LOKI_E10_CAP_S and budgets.run_cap_s set a run cap. With any of them the engine budgets every stage as before. With none, the run stops on no progress:

Guard Ends Default
No-progress rounds fix rounds, proof re-runs, continues that do not improve on the best round 2 in a row (LOKI_E10_NO_PROGRESS_ROUNDS)
Silent session a session with no output and no tree change 1800 s (LOKI_E10_STALL_KILL_S)
Looping session a session that prints but changes nothing in the tree 3600 s (LOKI_E10_LOOP_KILL_S)
Silent command a test or check with no output 1800 s (LOKI_E10_CMD_SILENCE_S)
Hung worker no event and no running process 1800 s (LOKI_E10_HANG_SILENCE_S)

Each ends the run with commit, seal and a draft PR of what is done. LOKI_E10_DEFAULT_BUDGETS=1 restores the old default budgets.

Budget contract

The per-run spend cap: its default, how it is chosen, what it covers, where it is enforced, how far a run can overshoot, what happens when a price is missing, and what happens when a run is cancelled. The CLI help, the run-start line and this section must agree; tests pin them.

Default

Loki 10 engine (the default route): no default cap. Since the no-default-budget change (founder, 2026-11-18), the engine sets no dollar cap, with an API key or on a subscription, and no time, round, turn or test-count budget either. A run ends when it is done or makes no progress (see “Time and progress” below), always with a draft PR of what is done. An API-key run prints: “no cost limit is set: the run stops when it is done or makes no progress (–max-cost sets one)". A limit you set (`--max-cost`, `LOKI_BUDGET_LIMIT`, `budgets.per_run`) is enforced exactly as before, and an unusable value fails closed.

Previous loop (autonomy/run.sh): 100 USD per run.

The number lives in one file, loki-ts/data/model-pricing.json (default_max_cost_usd). Only the previous loop applies it:

This document, the JSON file, the bash default and the legacy half of the CLI help all state the same number. Change the JSON file and the document together.

Where the default applies:

Route Default applies What the cap does
engine10 (loki "<task>", the Loki 10 engine) Never: no default dollar cap, with or without an API key. A cap you set stops starting stages and seals what exists. Stops starting stages and seals what exists (see “What you keep at the cap”).
Bash loop (loki start --engine legacy, autonomy/run.sh) Always, key or not. Pauses the run (writes .loki/PAUSE and .loki/signals/BUDGET_EXCEEDED); remove both to resume.

The README and the engine guide have shown different figures in the past (100 and 20). The figure above is the code’s, and the two guides follow it.

Precedence

Highest first. The first one set wins.

  1. --max-cost USD (engine10) or --budget USD (bash route). loki start --budget is forwarded to engine10 as --max-cost.
  2. The environment variable LOKI_BUDGET_LIMIT. On the bash route an empty value (LOKI_BUDGET_LIMIT=) means unlimited.
  3. Config file: budgets.per_run in loki.yaml (engine10), or the budget setting in the settings file loaded by autonomy/run.sh (bash route).
  4. The default above (subject to the API-key rule for engine10).

Run-start line

Every start prints the active cap and where it came from.

A cap set through the bash settings file is reported as config; the --config file loader exports the variable, so it reports as env.

Scope

Enforcement point and overshoot

engine10 checks the cap before each stage starts (machine.ts, capReached), using the cost events recorded so far, and each session is handed what is left of the cap (engine10/session.ts, childBudgetLimit, exported as LOKI_BUDGET_LIMIT and passed to Claude Code as --max-budget-usd by loki-ts/src/providers/claude_flags.ts). So a session that is already running is bounded by the remaining cap, with a floor of $0.01, and the overshoot is bounded per session. The plan and wall stages can run together, so up to two sessions can be in flight when the cap is crossed. Time caps and the per-session time limit still apply inside a stage. A provider that reports no cost (opencode) has no dollar enforcement; the start line warns about it.

The bash route checks after each iteration (check_budget_limit), so the overshoot is up to one iteration.

What you keep at the cap

When the cap is reached no later stage starts, but the closing stages (commit and seal) still run, so the work done so far is kept and the run ends with a receipt that names the cap:

Missing prices

Cost is never reported as 0 when it was not measured.

Cancellation

SIGINT or SIGTERM during an engine10 run:

  1. The supervisor forwards SIGTERM to the worker process group and keeps reading its output.
  2. The worker stops every live provider session. All providers share one spawn path (createSessionRunner), so one rule covers them all: SIGTERM to the session’s process group, then SIGKILL after 2 seconds. No new session starts.
  3. Each stopped session records the usage measured so far, with its unmeasured flag when no dollar figure exists (usd: null).
  4. If the worker has not exited after the cancel grace (10 seconds, set with LOKI_E10_CANCEL_GRACE_MS), the supervisor SIGKILLs the group. A second signal escalates immediately.
  5. The supervisor opens no PR and makes no backstop commit, restores the branch the run started on, and appends run.completed with cancelled: true, outcome: "CANCELLED" and the cost folded from the events. loki status shows the run as cancelled, distinct from FAILED.
  6. The run summary is printed with Outcome: CANCELLED and the process exits 130.

This is driven with a stub provider that sleeps.

Hitting a cap is a FAILURE, not a success

This is the part that matters for anyone automating against us.

All three caps produce a terminal failure: exit 20 under LOKI_DURABLE_STATE=1, with a distinct status (budget_exceeded, max_iterations_reached, max_duration_reached) so the receipt and loki why can tell you which ceiling you hit and therefore which one to raise.

budget_exceeded used to exit 0, on the reasoning that a human would raise the cap and resume. That is true at a terminal and false inside a Kubernetes Job, where there is no human: the Job went Complete, the pipeline went green, and an incomplete build looked finished. Exit 0 now means the work is finished or a person deliberately stopped it – never that we ran out of money mid-task.

See exit codes for the full contract.

Preview the cost before spending anything

Every loki "<task>" start line carries one quote, printed before any session starts:

... cap $5.00 (--max-cost); estimate: about $0.19 (range $0.12-$0.39), about 1 min (range 1-5 min), first run in this repo (benchmark prior, loki-default)

loki estimate prints the same quote without starting a run (--model M, --provider P and --json are accepted). Where the numbers come from:

Switches: LOKI_COST_PREVIEW=0 turns every quote surface off, LOKI_COST_PRIOR=0 drops the first-run prior (the line then reads NOT AVAILABLE until history exists), and LOKI_COST_CONFIRM=1 asks y/N before a run starts.

How accurate the quote was: loki stats --outcomes prints a Quote accuracy row, how many of the last 50 sealed runs landed inside the quoted dollar range and the median error against the quoted middle (quote_accuracy in --json).

What the quote is not:

For the older loop, loki plan ./prd.md --json reports complexity, estimated iterations, token usage and cost without executing. No provider call, no spend. LOKI_CONFIG_DUMP=1 loki start ./prd.md prints the resolved configuration and exits, so you can confirm your caps are actually set before a real run.

What you are charged for, stated plainly

Every iteration calls your provider, including iterations spent re-running after a quality gate fails. We do not exempt our own gate failures from your budget, and pretending otherwise would be dishonest – those calls really do consume your tokens.

What we do instead:

If a run cost more than you expected, loki proof show <id> tells you where it went.

Backend capability contract (LM-004)

One table in loki-ts/src/providers/provider_guard.ts (CAPABILITIES) records what each provider guarantees on engine10. loki provider matrix prints it (all engines), loki provider info <name> --json prints one provider’s rows with the source files each value was read from, and loki start prints one disclosure line before the run when the provider falls short of Claude. Claude prints nothing extra. On the legacy bash engine only the invocation and sandbox flags are confirmed from code; every other value there is unknown, which means not confirmed, not absent.

provider engine cost budget kill cancel (pgroup) sandbox flags receipt resume guard scan kill blocking
claude engine10 measured post-iteration yes none yes yes native native
opencode engine10 none none yes none yes no post-iteration none

Parallel units and groups

A decomposed run (loki backlog --dag, or a spec that splits into a unit group) shares one total. The resolved cap is exported to every unit as LOKI_BUDGET_LIMIT, and the backlog runner stops launching pending units with BUDGET_STOP once the summed measured cost of finished units reaches that cap. Units already running when the cap is crossed finish, so the total can overshoot by at most the in-flight units. budgets.per_run is accepted as an alias of budgets.per_run_usd in loki.yaml.

A budget stop survives a rerun

When the Bun engine stops a run on its dollar cap it writes .loki/runs/<id>/budget.json with the cap, the amount spent, where the cap came from, and the outcome budget_stopped. loki status shows the latest stop, and the run summary’s next step reads “raise with –max-cost, then rerun the same task”.

Rerunning the same task does not reset the meter. The supervisor finds the newest budget stop for that exact task text and hands its spend to the worker as LOKI_E10_SPENT_USD, so the cap counts the earlier spend too. To continue, raise the cap with --max-cost.

When a dollar cap is set but the provider reports no cost, the start output adds a warning: the cap cannot be enforced, because unmeasured sessions never count toward it.