This is the operational layer under the fleet described in Shipping a SaaS product with a fleet of AI coding agents. The briefs decide what the agents build; the gate and the monitor decide when it is safe to start them and when to intervene. The gate is built on Claude Code hooks: a PreToolUse hook denies the launch command at the tool boundary while quota is low.
Why does an agent fleet need a quota gate?
A quota gate exists because subscription capacity is shared and finite, while a fleet consumes it in bursts. The delegated CLI draws from a subscription with a weekly allowance and a shorter rolling window, not a per-token bill. A fleet that empties the weekly bucket on a Thursday leaves the whole team without the tool until the reset.
That is a different failure from an API bill. An unmetered-feeling tool invites a large fan-out at exactly the wrong moment: twenty agents launched at 10 a.m. on a Thursday, each with a 90-minute timeout, is a decision about the rest of the week. The gate turns that decision into a visible number before the launch, not an error message during it.
Two properties matter. The check has to be cheap enough to run before every launch, and it has to be enforced somewhere the launcher cannot forget. A rule in a document is not a gate.
How does the quota gate block a launch?
The gate is a small program plus a hook in front of the launcher, and both read the same two numbers.
- Read the buckets. A headless usage call returns the remaining percentage for the weekly and 5-hour windows, plus their reset times. It takes about two seconds, returns no model output and is cached for a minute.
- Apply the threshold. Exit 0 means both buckets are at or above 10%. Exit 2 means one is below. Exit 1 means the check could not run, which is not a clear pass.
- Deny at the tool boundary. A pre-tool hook runs the check before any launch command and blocks the call while either bucket is under 10%. The denial happens in the harness, so an agent cannot decide to proceed anyway.
- Deny again in the launcher. The launch script runs the same check, so a launch from a script is gated even without the hook.
- Fall back, do not retry. When the gate trips, the same brief goes to a subagent instead. Same brief, same review, same output contract. Retrying the CLI or routing around the gate is forbidden, because the gate exists to protect the rest of the week.
The threshold is deliberately conservative. Ten percent is not “almost out”; it is the reserve that keeps small, urgent work possible while the weekly window refills.
What do the two quota buckets mean for planning?
The weekly bucket decides whether a large wave is affordable; the 5-hour bucket decides whether it can start now. They behave differently, and a single average hides that.
| Window | What it controls | What to watch |
|---|---|---|
| Weekly | Total capacity for the week, including every agent, every retry and unmonitored bridges | Headroom before a wave of 10 or more; reset day and time |
| Rolling 5-hour | Burst capacity inside the current window | Whether a queued wave will hit the wall mid-run |
The measured spend is the useful planning number: roughly 0.3–0.5% of the weekly bucket per 10–40 minute run. A wave of twenty runs is therefore about 6–10% of the week, which is fine on Monday and reckless on Thursday afternoon.
Two habits follow. First, put the quota line at the end of every fleet check-in, so the trend sits next to the agent states every 15 minutes. Second, when the weekly figure is low, shrink the wave rather than betting on the reset.
Why is the agent log a bad progress signal?
The agent log is empty until the process exits. The CLI writes its report at the end, so a 0-byte log after 30 minutes means the run is still going, not that it is stuck.
This is the opposite of the intuition most operators bring from web servers, where silence usually means trouble. Here, silence is the normal state. The failure mode is worse than a missing signal: a log-based monitor will call a healthy run stuck and kill it, or call a dead run healthy because the file exists.
Use the log for the final report and the sentinel. Use the filesystem and git for progress.
How does stall detection work?
Stall detection combines three cheap measurements per agent: commits ahead of main, uncommitted file count, and files modified in the last 15 minutes.
snapshot() {
for name in "$@"; do
pid=$(cat "$LOGS/$name.pid")
kill -0 "$pid" 2>/dev/null && state="running" || state="stopped"
commits=$(git -C "$WT/$name" log --oneline main..HEAD | wc -l)
dirty=$(git -C "$WT/$name" status --short | wc -l)
touched=$(find "$WT/$name" \
-path '*/node_modules' -prune -o -path '*/.git' -prune -o \
-type f -mmin -15 -print | wc -l)
echo "$name $state commits=$commits dirty=$dirty touched15m=$touched"
done
}
Each number answers a different question. Commits ahead of main say whether the work is durable. Uncommitted files say whether work exists that a timeout could strand. Files touched in the last 15 minutes say whether the process is doing anything at all.
The main..HEAD range is the right count, because it lists commits reachable from the branch but not from main (git-log). The find command prunes node_modules and .git so dependency churn cannot fake activity.
A reading-only task can legitimately touch nothing for 15 minutes. That is why the rule is two consecutive check-ins, not one, and why the operator looks at the process and the task log before killing anything.
How does the check-in script avoid constant polling?
The check-in runs as one background job that wakes on an event or on a 15-minute heartbeat, rather than a loop that asks every minute.
The script records how many agents were alive at the start. Every minute it recounts. When the count changes, it prints an event and a full snapshot, then exits. If nothing changes for 15 minutes, it prints a heartbeat and a snapshot, then exits. The operator restarts it after each wake, which keeps the model simple: one job, one event, one report.
Two refinements earned their place. Mark agents already handled with a reported file and skip them in the snapshot, or a long run fills the list with finished work and a stalled agent hides in it. Print the quota line in every snapshot, so capacity and progress are read together.
When should you kill a run, wait, or send a continuation?
The right action depends on whether work exists and whether the process is moving.
| State | What the snapshot shows | Action |
|---|---|---|
| Clean finish | Stopped, commits ahead, sentinel present | Review, merge, mark reported |
| Timed out with work | Stopped, no commits, many uncommitted files | Send a continuation brief in the same worktree |
| Alive but stalled | Running, touched15m=0 for two check-ins |
Inspect the task log, kill stray servers, then continue or relaunch |
| Waiting on a background task | Running, no file changes, task log shows an idle wait | Kill the run and relaunch with the foreground rule |
| Silent no-start | Dead PID, 0-byte log, no commits | Treat as a failed start; stagger and relaunch |
Which should you choose?
Continue when the worktree holds real work; the continuation brief preserves it and costs one run. Kill when two check-ins show no file changes, or when a single background task is holding the run open, because waiting only burns the timeout. Restart from scratch only when the worktree is clean and nothing worth preserving exists.
What broke and what we changed
The agent went idle waiting on its own background task. It started a build or a dev server as a background task, then sat waiting for it until the print timeout. This happened four times in one day; one run hit its deadline at 60 minutes, waiting on a hung probe after all the real work had passed. The common rules now say to run every command in the foreground, give every long step a timeout, and kill every server before finishing.
The task dispatcher re-dispatched the same sub-task. One run sent the same sub-task 6–20 times in two minutes, each attempt reporting “completed” with nothing changed. The monitor showed no files touched for 15 minutes and an empty task log, so the run was killed and relaunched with a narrower brief. One area per agent made this rarer.
A timeout left 32 uncommitted files. The work was done; the agent ran out of time before committing. A fresh start would have discarded it. The fix was a continuation brief that tells the next run to review the existing changes, finish them, verify and commit.
A silent no-start looked like a finished run. Five image agents launched in the same second all exited with nothing: empty log, no commits, sometimes no PID file. Launches are now staggered by a few seconds and checked with a zero signal 10 seconds later.
The monitor counted one agent too many. A process-name search matched the waiter loop as well as the agent. Counting now comes from PID files written by the launcher, never from a pattern match.
The laptop was the bottleneck. Seven parallel browser QA agents plus a local dev server crashed a 16 GB machine. Laptop fleets are capped at four or five agents, and heavy verification moved to a separate Linux box with its own test databases.
Related
- Shipping a SaaS product with a fleet of AI coding agents — the pillar post for this cluster.
- Writing briefs that AI coding agents can’t misread — the foreground rule and the continuation brief start there.
- Running AI coding agents on remote Linux machines — where the monitor and PID files run when the laptop cannot carry the fleet.
- Waves, not one big run — why runs stop in waves and what the next wave needs.
- Mass agent orchestration case study — the systems these controls were built for.
Frequently asked questions
What is a quota gate for AI agents?
A quota gate is a pre-launch check that reads the remaining capacity of the subscription the agent draws from and blocks the launch when either quota window is below a threshold. It runs in the launcher and in a tool hook, so both interactive and scripted launches are covered.
How do you detect a stalled AI agent run?
Watch the worktree, not the log. Three signals: commits ahead of main, uncommitted file count, and files modified in the last 15 minutes. A live process with zero touched files across two 15-minute check-ins is stalled, unless the brief was a read-only investigation.
Why not just read the agent log?
Because the CLI writes its report on exit, so the log is empty for the entire run. A zero-byte log is the normal state, not a failure. The log is useful for the final result and the AGENT DONE sentinel, and useless as a heartbeat.
Should you restart or continue a timed-out agent?
Continue, if the worktree contains work. A continuation brief tells the next run to review the uncommitted changes, finish what is missing, verify and commit, and never start over. Restarting is only correct when the worktree is clean and the previous run produced nothing worth keeping.
How much quota does one agent run use?
Roughly 0.3–0.5% of the weekly bucket for a 10–40 minute run, measured across one overnight fleet. That number is a planning guide, not a guarantee: a run that retries a failing build or loops on a browser test will use more.
Can an agent bypass the quota gate?
Not through the normal paths. The hook denies the launch command at the tool boundary, and the launcher repeats the check because a scripted launch may never reach the hook. When the gate trips, the same brief goes to a subagent instead; retrying the gated CLI is explicitly disallowed.
