← Writing

My build agents knocked over production.

Running many AI coding agents on the server that also serves customers: a CPU cap from my host, an out-of-memory reboot, and moving every build elsewhere.

Sakshyam Baral··5 min readinfraaigo

I lean heavily on AI coding agents. During the Go migration I often had several running at once, each porting its own set of routes or building its own admin pages. For a while, all of them ran on the same server that serves Laayo's customers, restaurants and riders. That worked until it didn't, and it failed in three different ways before I fixed it properly.

Every agent is a compiler

An agent that writes code also checks it. For the Go side that means go build, go vet and the tests. For the Next.js consoles it means a TypeScript type check and a next build. Each of those is CPU heavy for a while, and the TypeScript checker in particular takes a lot of memory. One agent doing this now and then is fine. Many agents doing it at the same time, on the box that answers every app launch, is not.

The first sign was the endpoint every app calls on launch getting noticeably slower while the load average climbed. Builds had piled up behind a shared file lock I had added so they would run one at a time, and the type checks that didn't take the lock ran in parallel anyway. Part of the slowness wasn't even ours: on a virtual server, a busy neighbour or a busy host shows up as CPU steal, and that was high too.

My first fixes were rules for the agents:

  • fewer build-heavy agents at once, the rest queued
  • incremental type checks, and one build at a time under the lock
  • every heavy command under nice and ionice so customer traffic wins
  • check the load before resuming a batch of agents

A surprise from the same investigation: one of the biggest CPU users was git status. Every coding session polls it, and my monorepo had a huge number of files and had never been committed as a whole. Turning on git's untracked cache helped a bit. The real fixes are fewer open sessions and committing the repository properly.

The host stepped in

Then my hosting provider capped the server's CPU because of sustained high load. Support lifted the cap when I asked, with a clear warning: if the load stayed high, the cap would come back, and a manual reset was not something they would do often. A capped production server would mean slow orders for days, so this changed the problem from "builds are a bit slow" to "builds can slow down every order".

I moved builds to another machine. A small wrapper script does it:

# build-remote SRC -- COMMAND...
# Mirror the project to the build machine, run the command there,
# stream the output back, and exit with the command's status.
rsync -a --delete \
  --exclude node_modules/ --exclude .next/ --exclude .git/ --exclude '.env*' \
  "$SRC/" "$BUILD_HOST:$REMOTE/"
ssh "$BUILD_HOST" "cd $REMOTE && $*"
# build-remote SRC -- COMMAND...
# Mirror the project to the build machine, run the command there,
# stream the output back, and exit with the command's status.
rsync -a --delete \
  --exclude node_modules/ --exclude .next/ --exclude .git/ --exclude '.env*' \
  "$SRC/" "$BUILD_HOST:$REMOTE/"
ssh "$BUILD_HOST" "cd $REMOTE && $*"

There is more to the real one: it runs a fresh dependency install when the lock file changes, it can copy a named build output back, and it limits how many builds run on the far side at once. Environment files are never copied. The build machine sits on a private network with the server and cannot reach the database or Redis, so it can compile, type check, lint and run unit tests, and that is all.

At first I kept a local overflow slot: one Go-only build could still run on the production server when the server was nearly idle, at the lowest priority and with limited cores. I added a small "load brake" next to it, a job that runs regularly, turns local builds off when the load stays high and turns them back on when it drops. It only re-enables local builds if it was the one that disabled them, so a manual off switch stays off.

Then it ran out of memory

None of that stopped the next failure, because I was watching the wrong resource. One night the server ran out of memory and rebooted. The memory was split between several coding sessions and their worker threads, the database, and a production build that happened to run at the same moment. Several sessions were each running their own agents. Each session's agents were reasonable on their own. Nobody had the total in view.

The rules got stricter after that: my own agents run only a couple at a time, no build starts while another session is building, and idle sessions get closed. I also turned local builds off for good, by hand, so the load brake can never turn them back on. The Go build cache had started filling the production disk at an uncomfortable rate as well. Now every build, test run and type check goes through the wrapper, and the production server only runs the binaries that come out of it. The one exception is a release script that builds production binaries directly, and that exception is deliberate.

The first build machine was a small spare box. It handled the queue until the migration got busy, then it started swapping, and builds waited so long that agents sat idle. I rented a dedicated build server with more cores and memory, pointed the wrapper at it, and nothing else had to change. Android compile checks moved there as well, including the Kotlin daemon, which needs a lot of memory on its own.

Compilers, type checkers, Android toolchains and coding agents have the same shape: bursty, memory hungry, and easy to start in parallel without noticing. They now share a machine with each other and not with customers. Because every build already went through the wrapper, no agent and no script had to change when the target moved.

Get new posts by email

One email when something new goes up. No tracking pixels, unsubscribe in one click.