Skip to content
Leashr
Tested

Spending caps

A cap blocks the next tool call once the mission or the day has spent its estimated budget. The kill switch blocks everything at once.

Set a cap

Leashr never picks an amount. Without .leashr/cap.json the cap hooks only enforce the kill switch.

shell
node .leashr/bin/cap.js set --mission 2 --day 10 --dir .
output
Caps written to .leashr/cap.json: mission $2, day $10, warning at 80%.
json
{
  "version": 1,
  "mission_usd": 2,
  "day_usd": 10
}
  • set --mission <usd|off> --day <usd|off> --agent <name>=<usd|off> --warn <ratio>. agent_usd caps one sub-agent name such as lsh:expert.
  • Also in the file: warn_ratio (default 0.8), stale_seconds (default 15, at most 300), otel_wait_ms. Any other key, a bad value or invalid JSON blocks every call (config_error): a typo must not lift a cap.
  • The mission cap needs a current mission; without one only the day cap applies. The day is the local day of the machine, all missions of the project.

See what is left

shell
node .leashr/bin/cap.js status --dir .
output
Kill switch: off
Caps: mission $2, day $10, warning at 80%, state stale after 15 s
Current mission: checkout-refactor
  mission checkout-refactor: $0.000000 of $2.000000
  day 2026-11-23: $0.000000 of $10.000000
Signals: collector never seen, OpenTelemetry sessions 0, statusline sessions 0

status exits 3 when a cap is reached or the kill switch is on. --json gives the same data.

Kill switch

shell
node .leashr/bin/cap.js kill --reason "runaway loop" --dir .
node .leashr/bin/cap.js resume --dir .
output
Kill switch ON (runaway loop): every tool call and prompt is now blocked in this project.
Leashr blocked this tool call (cap rule kill_switch): Leashr kill switch is on (runaway loop). Every tool call and prompt is blocked until a human runs "cap.js resume".
Kill switch off.

The kill switch stops the agent before its next tool call and blocks new prompts, in every session of the project. The default rules ask a human before an agent runs cap.js set or resume or edits .leashr/; the agent can still turn the kill switch on.

How it works

  1. Spend comes from Claude Code's own estimate at list price: the statusline cost (interactive sessions, refreshed every 2 s) and OpenTelemetry (lsh-cost collector, also for claude -p). The larger of the two counts. Never a price table.
  2. The PreToolUse hook denies the call if spent is at or above the cap, with exit 2 and the reason shown to the agent.
  3. From 80% of a cap, the call goes through with a warning message. Every decision is written to the audit journal.

Fail closed

With a cap set, a tool call is refused when the cost signal is missing (signal_missing), older than stale_seconds (state_stale), or unreadable (state_unreadable). A cap that lets calls through when it cannot tell guarantees nothing. Real output right after init, with no statusline state and no collector:

output
Leashr blocked this tool call (cap rule signal_missing): Leashr spending cap cannot see this session's spend, failing closed: no cost signal for this session: no Leashr statusline state (interactive sessions) and no running lsh-cost collector (OpenTelemetry).

What was observed

  • Real interactive Claude Code session (2.1.310), cap of $0.03: eight reads went through, then the next one was denied before it ran, with the reason on screen.
  • Subagents share the session id: their cost is in the session total. Resuming a session restores its cost and the cap holds.
  • Proof: bench/data/cap-bench.json (real sessions, summarized in bench/RESULTS.md) and skills/lsh-cap/test (73 tests passing on 25/11/2026).

Limits

  • Overshoot: only tool calls and prompts are blocked. Text the model is already writing, the refused call itself and background requests still cost: +16% over a $0.03 cap in the real test, about 2 to 4 requests.
  • A request already in flight cannot be cancelled.
  • One request of lag: the statusline is refreshed after the response, OpenTelemetry after its export interval (5 s as installed).
  • /clear starts a new session at zero cost. A mission or day cap adds up sessions, so it holds; a per-session cap would not.
  • claude -p has no statusline: start the collector, or every call is refused. --max-budget-usd is the native per-run limit there.
  • The statusline needs a trusted folder. Another statusline in .claude/settings.local.json, or disableAllHooks, stops the state: calls then fail closed.
  • A script called by name or a computed path can still reach .leashr/ files: the cap is not tamper-proof.
  • The cost is an estimate at list price. On a subscription there is no per-token bill.
  • Outside Claude Code there is no cost signal: with a cap set, every tool call of Codex, Cursor and Antigravity is refused. See Limits by agent.

Not proven yet

  • A denial inside a subagent in a real session (proven in simulation).
  • Several concurrent sessions in the same folder.

Status

FunctionStatusProof
Cap per mission, day, agentTested, Claude Code 2.1.310skills/lsh-cap/test/cap.test.js
Kill switchTestedskills/lsh-cap/test/cap.test.js
Fail closed on missing or stale signalTestedskills/lsh-cap/test/cap.test.js
Cap on Codex, Cursor, AntigravityNot supported (refusal only)skills/lsh-cap/test/agents.test.js