Skip to the content.

Session budget cap

A coding agent stuck in a loop, or fanning out sub-agents, spends money (API keys) or quota (subscriptions) with nothing to stop it. Aegis can cap that spend per agent session and per project per day, in estimated dollars or in tokens, across Claude Code, Codex, Gemini CLI, OpenCode and Copilot CLI (premium requests). The cap is written into a signed policy file, so an agent cannot raise its own limit by editing a file.

← back to the README · design: dev/DESIGN-v1.0-budget-cap.md

What it guarantees, and what it does not

Aegis denies covered tool calls and prompts when the usage available at that hook reaches the limit. It does not impose a finite bound on additional model spending: in-flight requests, delayed usage records, retries after a denial, and tool-free activity can add cost.

Hooks run before tool calls and prompts, never before a model request, and an agent writes a request’s usage to its log only after the request has been paid for. So a session can end above its limit — by the request that crossed it, by the model’s replies to the denials, by sub-agents that were already running, and, in OpenCode and Copilot CLI (which have no prompt hook that can block), by chat that uses no tools. aegis budget status shows any usage over a limit as overshoot. If you must never exceed an amount, set the limit below it, and put a spend limit in front of the model requests themselves as well (a gateway, your vendor’s spending limits); those are separate layers whose guarantees you need to check.

The budget is a guard against loops and mistakes. An agent runs as you and can rewrite its own session log; Aegis does not defend against an agent that does that on purpose.

Set it up

aegis init .aegis                          # once; writes budget.example.yaml among the examples
cp .aegis/budget.example.yaml .aegis/budget.yaml
$EDITOR .aegis/budget.yaml                 # your limits
aegis sign --key file:KEY .aegis/budget.yaml
aegis install claude                       # and codex, gemini, opencode, copilot: reinstall
aegis budget status

Reinstalling matters: with a budget.yaml in the policy directory, aegis install widens each agent’s hook from shell commands to every tool call and adds the prompt hook where the agent has one (--budget / --no-budget choose explicitly). The Claude Code plugin and the Gemini extension already carry those entries; they start no extra process in projects without a budget.

budget.yaml

version: 1
principal: admin            # must hold the 'budget' class in authority.yaml
unit: usd                   # usd | tokens — required
session:     {limit: 20,  warn_at: 0.8}          # per agent session
project_day: {limit: 100, warn_at: 0.8, tz: America/Chicago}   # all sessions in the project, per day
agents: [claude, codex, gemini, opencode, copilot]   # only listed agents are measured
on_unknown_log: allow       # allow (warn) | deny
on_unknown_price: estimate  # estimate (warn) | deny — usd only
pricing:                    # signed overrides, USD per million tokens
  claude-opus-5-5: {input: 4, output: 20, cache_read: 0.2, cache_write: 8}
copilot: {premium_requests: 50, warn_at: 0.8}       # per Copilot CLI session

What happens near and at the limit

  Tool calls New prompts You see
Below warn_at allowed allowed nothing
Past warn_at allowed allowed one warning per session per threshold
At the limit denied, every tool denied where the agent can block them what was spent, which limit, how to continue

The infrastructure policy still decides first: a command the policy blocks stays blocked whatever the budget says. To continue past a limit: start a new session (session limit), wait for the next day in tz (project limit), or raise the limit in budget.yaml and re-sign it.

Agent Tool hook Prompt hook Warning shows as A turn’s usage is seen from
Claude Code PreToolUse, every tool UserPromptSubmit a message in the transcript its own tool calls
Codex CLI PreToolUse, every tool UserPromptSubmit a UI warning the next turn
Gemini CLI BeforeTool, every tool BeforeAgent a message in the terminal not yet measured live
OpenCode plugin, every tool — a toast the next step
Copilot CLI preToolUse, every tool (premium requests) — (it cannot block) stderr only the next turn

The last column is when each agent writes a model response’s usage to its log, measured in the live test: Claude Code before the response’s tool calls run, the others only after them. So in Codex, OpenCode and Copilot CLI the turn that crosses a limit still runs its own tool calls, and the cap takes hold from the next one; a single Copilot CLI -p prompt cannot be stopped. That is part of the overshoot described above.

Getting the hooks to run. Codex runs project hooks only in a trusted project and new hooks only after you review them (/hooks); Copilot CLI loads repository hooks only in trusted folders (a --user install always loads); Gemini CLI asks to trust the folder. An unattended setup (CI, a headless run) needs the agent’s own trust option, e.g. Codex’s --dangerously-bypass-hook-trust or Gemini’s GEMINI_CLI_TRUST_WORKSPACE=true.

Where the numbers come from

No hook payload carries token usage, so Aegis reads each agent’s own session log, incrementally: Claude Code’s transcript (and its sub-agents’), Codex rollouts, Gemini CLI chats, OpenCode’s database (read-only; only its session and message tables), and Copilot CLI’s events. A sub-agent is counted inside its parent session. Nothing is sent anywhere.

Dollars are estimates from a dated table of list prices (aegis_core/budget/prices.yaml; Anthropic, OpenAI and Google). On a subscription they are an API-equivalent figure, not your bill; unit: tokens may suit you better there. A model the table does not know is priced, per category, at the highest rate of its vendor (or of the whole table) — a guess that can still be low — and flagged; with on_unknown_price: deny its sessions are denied until you add a signed pricing: entry for it.

The day is the calendar day in project_day.tz, from the time each log record carries.

What Aegis keeps is derived from the logs and can be rebuilt: a cache under ~/.cache/aegis/budget/, and a small durable state under ~/.local/state/aegis/budget/ (a per-session revision counter, locks, and a list of the sessions seen each day, so a rebuild can tell when history is missing). Deleting the cache resets nothing while the logs exist.

When usage cannot be verified

Some usage can only be counted as a lower bound: a log Aegis cannot find or read, records it does not recognise, usage with no recorded time, a project day rebuilt after sessions’ logs were deleted, or the first day of a budget when other sessions had already run that day. on_unknown_log decides what that means:

Commands

aegis budget status [--project DIR] [--agent A] [--json]
aegis budget check  [--project DIR] [--agent A --session ID]
aegis budget reset  --day [YYYY-MM-DD] [--project DIR] [--key KEY]

Cost

Measured with scripts/budget_latency.py (results/budget-latency.json): the budget adds about 9 ms to one hook call (Apple M4 Pro, Python 3.12: 74 ms median with a budget, 65 ms without), against a design target of 20 ms. Projects without a budget.yaml pay nothing.