Most of our agents do not fail loudly. They exit 0 with nothing useful done, or they wait until the first scheduled run to discover that cron gave them a different environment than the one we tested in. This post describes how we monitor a fleet of long-running agent jobs: what every run reports, how failures become fixes, and the failure patterns that shaped the design.
The fleet is ordinary infrastructure. A few hundred cron lines and systemd timers on a small set of Linux hosts run collectors, digests, content pipelines and repair agents. Some call a model, many do not. Every one of them reports to the same run journal, and that journal is the thing this post is about.
The architecture: one journal, three ways in
Every run that reports, success or failure, lands as one record in a run journal: job name, the loop it belongs to, start and end time, exit code, status and the last lines of output. Jobs get there in one of three ways.
- A shell for cron files. We had 229 cron lines when we started and did not want to rewrite them. Cron runs each job as $SHELL -c "<command>", so we set one SHELL= line per cron file to a small wrapper. It runs the command with the real shell exactly as cron would, keeps stdout, stderr and the exit code unchanged, then reports the run.
- A per-job wrapper for jobs that need more, such as a failure email with the last 60 log lines, throttled to one per job every six hours so a broken job cannot flood the inbox.
- A process sweep every 10 minutes for things that are not cron jobs: services in an errored or unexpectedly stopped state, restart bursts and failed systemd units. It only reports. It never restarts anything.
The reporting path is fail-open everywhere. If the journal is unreachable, the report is spooled to disk and re-sent with the next report from that host. The wrapper captures the child's exit code before it reports, and the reporter runs under a 20-second timeout, so a reporting bug cannot change the exit code or hold the job for long. The wrapper itself is the one thing that must not break: a bad SHELL= line would stop every job in that file from starting, so we keep it a few dozen lines of shell. Output tails are run through a redaction pass before they leave the host, because agents print tokens more often than you would hope.
Failures also go to a self-hosted error tracker, one fingerprint per job, so the same failure stacks into one issue instead of a thousand. The trade-off is real: two unrelated bugs in the same job share an issue, so diagnosis still starts from the individual run records.

Runs report to the journal; failures stack in the error tracker, and only a person merges what the repair agent proposes.
What a run reports beyond its exit code
An exit code tells you how a process ended. It does not tell you whether an agent did its job. So a job can print a few marker lines near the end of its output, and the reporter picks them up:
| Marker | What it records |
|---|---|
LOOP_RUN_MODEL | Which model the run used, if any |
LOOP_RUN_TOKENS_IN, LOOP_RUN_TOKENS_OUT, LOOP_RUN_COST_USD | Spend per run, so a prompt change that triples cost shows up the next morning |
LOOP_RUN_OUTCOME | What the run achieved, separate from whether it exited cleanly |
LOOP_RUN_CRITERIA | Results against the loop's declared success criteria, as JSON |
LOOP_RUN_ID | A run id the job stamps on anything it writes, so an output can be traced to the run that made it |
LOOP_RUN_OUTCOME is the one that earns its keep. A digest that collected 86 messages, drafted a summary and then skipped the send (because we ran it with a no-email flag) exits 0 and reports no_progress. For a dry run that is correct. For a production job, a week of green runs that are all no_progress is a failure, and it is the kind of failure exit codes never show. So alerting compares the outcome with what the job was supposed to do, not just with zero.
The expected outcome is declared per job entry, next to its schedule, not inferred by the alerting. Each entry states what counts as progress (for the digest, the mail server accepting the message) and whether that entry is meant to make progress at all. A dry run is its own entry, with the no-email flag on its command line and an expected outcome of no_progress, so a dry run that reports no_progress stays green. The production entry expects progress, and it reports per-criterion results through LOOP_RUN_CRITERIA, such as messages collected but digest not sent. One no_progress run does not alert, because a quiet day can leave nothing to send. A string of them on the production entry does, and so does any run where a criterion the job marked as required comes back unmet.
Markers are plain text on stdout. Any job in any language can emit them, nothing has to import a library, and a job that prints none still reports its exit code and tail. Tracing tools for LLM observability cover the model calls inside a run; the markers cover the job around them, which is where most of our failures turned out to live.
Failure cases that shaped the design
The journal tells us that something failed. To learn why things keep failing, a weekly lookback job clusters the last four weeks of repair attempts into root causes with one model call, and marks each cause new or recurring against the previous week. The most recent run grouped 87 attempts.
Exit 1 by design, reported as failure. Several jobs exit 1 on purpose to say "there are findings a human should look at": a repair left unfinished, a quote held for review. The wrapper reported each of these as a job failure, and the repair agent dutifully tried to fix them. Lesson: a job has to declare which non-zero codes mean findings rather than failure, and a lint should flag jobs that exit non-zero by design without declaring it.
A deploy that skipped the migration. One app's deploy script restarted the server without running the database migration. A release that read a new column shipped against the old schema, crashed server renders, and caused a second wave of client-side errors downstream. Three separate issues, one cause. Lesson: clustering by root cause matters more than counting issues, and the deploy should compare applied migrations with the expected list after every release.
The host's Python was older than the developer's. Scripts written on a laptop used annotations such as str | None, which need Python 3.10 when they are evaluated at import time. The host's system Python was 3.9. They passed every local test and failed on their first scheduled run. Lesson: test scheduled jobs under the interpreter that runs them, in CI, and pin that interpreter.
Data that outgrew a fixed limit. Two jobs worked fine, then failed on every run once their input grew past a hard cap: Linux's 128 KiB per-argument exec limit on systems with 4 KiB pages in one case (the call fails with E2BIG), a backup tool's 128 MiB snapshot limit in the other. Lesson: pass large payloads through stdin or files, and alert on growth while there is still headroom.
A new job missing an environment variable. A weekly digest we added this month read its credentials from a secrets store. The store's address was exported by the wrapper script of the job it was copied from, not by cron. A dry run under the cron environment caught it two days before the first scheduled run would have failed. Lesson: shared helpers should set their own defaults instead of trusting the caller's environment, and every new job gets one run in the same environment cron will give it.
Cleanup that outran the cache. A deploy kept the last five releases on disk. The CDN cached HTML for 24 hours, and the content pipeline deploys about hourly, so cached pages kept pointing at JavaScript and CSS in releases that had already been deleted, and those assets returned 404. Lesson: retention has to outlive every cache in front of it. We now keep 30 releases, which covers a normal day of deploys. The stronger rule is to keep old assets by age rather than by count.
Rules we now follow
- Monitoring never changes the job. Wrappers pass output and exit codes through untouched, and every reporting error falls back to running the job as if the wrapper were not there.
- Report outcomes, not just exits. A run that did nothing useful has to be able to say so.
- Advance state only after the side effect is confirmed. Our digests keep a cursor of what they have processed. It moves only after the mail server accepts the message, so a failed send means the next run covers the same window instead of silently skipping it. The cost is the opposite failure: a crash between the server accepting the message and the cursor being saved sends the same digest twice. We take duplicates over gaps.
- Agents propose, people merge. It is the same split we use for every agent action, sorted into low and high risk tiers: anything public or hard to undo waits for a person. A daily repair agent takes the top error-tracker issues, works each in a scratch clone and opens a pull request or marks it noise or needs-human. It never merges. Merges, closed PRs and reactions in the team channel are read back the next day and written into a lessons file the agent sees in its prompt.
- Change the prompt only when it beats the old one. A change to the repair agent's instructions or its reviewer is replayed against past cases, on clones that hold no history after each case, and pinned only if it does better.
- Cluster before you fix. Three issues with one root cause are one problem. The weekly lookback is what turned a stream of individual fixes into the systemic changes above.
What is still open
Four problems are not solved. First, the journal only sees runs that report: a job that never starts, or a host that dies mid-run, leaves no record. Catching those needs the expected schedule and a start record with a deadline, which we have not built yet. Second, no_progress depends on each job telling the truth about its own outcome, and a job that misreports looks healthy. Third, cost markers show spend per run, but not whether the spend bought anything, which is the harder half of AI cost management. Fourth, the lookback finds recurring causes, yet the systemic fixes it proposes still wait for a person to schedule them.
The broader point is simple. Long-running agents fail in the gaps between processes: the environment cron gives them, the state they forget to commit, the cache in front of them. Put one journal across all of it, make every run say what it achieved, and let the failures you collect teach the next fix.
Share
Author

Thomas Prommer
Founder of We The Flywheel, working on agent-based systems and operations.
View All Posts



