Everyone's Building AI Agents. Almost Nobody's Keeping Them Alive.

· 8 min read
aiagentsreliabilityclaude-code

Building an agent that works in a demo is easy now. Keeping one alive for a month is not, and that gap is wider than the hype admits: Gartner expects over 40% of agentic AI projects to be canceled by the end of 2027, mostly to cost, unclear value, and weak reliability. The demo is the easy 10%. The other 90% is operational, and almost nobody posts about it.

Here's the failure mode you only meet in production. A persistent Claude Code agent accumulates tokens, turns, and transcript on every message. The JSONL file that holds the session grows without bound until it hits a limit, token bloat, context overflow, a compaction loop, and the session dies.

The death is the merciful version. Long before a session hits its limit, it gets dumber. Chroma's 2025 "context rot" study tested 18 frontier models and found every one degrades as input grows, often far below the context cap, a 200K-token model losing accuracy at 50K. Stanford's earlier "Lost in the Middle" showed models attend in a U shape, sharp at the start and end of a long context, soft in the middle. So a bloated session isn't just heading for a crash; it's quietly forgetting what you told it the whole way down. The research's own line: for coding agents, context rot is the primary failure mode.

If you run the agent through OpenClaw there's one more twist: when a session dies, the dead session ID stays pinned, and the gateway resumes it on every new message, an infinite error loop. The channel goes silent. It looks like a rate limit. It's a stale pointer.

I hit all of this running a fleet of always-on agents. The fix that worked was to stop treating a session as a conversation and start treating it as a container: something that fills up and has to be rotated, like a log file. There's a name for the broader discipline now, context engineering, which Karpathy defines as "the delicate art and science of filling the context window with just the right information for the next step." Rotation is the crude, physical end of it: when the window is full of sludge, you don't prune it, you replace it, and you make the seam invisible so the agent crosses it without noticing.

That's what I built session-warden for.

Rotate before it dies

A cron job runs every 30 seconds. When it finds a session that's failed or crossed a threshold, too many tokens, too many turns, too big a file, too many compactions, or a zombie (a dead CLI process still holding a stale session), it runs four steps: detect, rotate, summarize, restart. Back up the state, archive the JSONL (never delete it), clear the stale reference, restart the gateway. The agent is back online in under a second.

That's the easy part. Rotating a session is file shuffling. The hard part is rotating it without giving the agent amnesia.

Memory is the whole game

LLMs are stateless by design: every call processes a fresh context window and throws away everything when it ends. Which is why, across coding assistants, support bots, and research agents, memory-related failures were the single most-reported reliability problem in production agents through 2025. An agent that forgets mid-task isn't an edge case; it's the default when memory is an afterthought.

So before the restart, the warden extracts the entire conversation from the archived JSONL, not just the text, but every tool action, every file edited, every command run, every branch created, and hands it to a fast model to compress into a structured memory entry. It writes that into Claude Code's own native memory, which the agent reads automatically on its next start. The agent comes back knowing what it was doing.

Two more layers sit under that. Each channel gets its own memory file, so an agent active in five places keeps five separate contexts that don't bleed together, and each file is replaced on rotation so nothing grows forever (the whole point is to avoid the bloat we started with). And the agents are told, in their CLAUDE.md, to write important things to memory as they go, so if a session dies unexpectedly the critical context is already on disk. Underneath all three sits GBrain, Garry Tan's open-source agent-memory graph, as the durable cross-session layer, so the fleet's knowledge compounds instead of resetting. Between them, session boundaries become something you stop thinking about.

The part I didn't expect: trusting nothing

What surprised me is how much of this is backstops on backstops. Reliability for agents is a different discipline than building agents, and most of it is paranoia.

OpenClaw has a watchdog that kills a turn when its child stops making progress. Good, but it lives inside the gateway, so when the gateway itself wedges, the watchdog wedges with it and the agent hangs forever. So the warden has a stall reaper that reads the gateway's state from disk and /proc only, never through an RPC, specifically so it keeps working when the gateway loop is dead. It only kills a process if its command line and environment prove it belongs to the stuck agent, so a human's own session is never touched.

This isn't hypothetical. On 18 June one of my agents, dash, had a turn the gateway still believed was running, while the process behind it was already dead: no live child, frozen for 929 seconds. The fast in-gateway watchdog never fired, there was no live child to watch and the gateway's own state had gone stale. The reaper, reading only disk and /proc, caught it the moment it crossed the fifteen-minute cap, cleared the dead "running" flag, and handed off to recovery. Reaped one, escalated none. Without it, that channel sits silent forever, and I find out hours later when someone asks why dash went quiet.

And then there's the question of who watches the warden. It has a doctor that checks its own wiring, plus a dead-man's switch: it pings an external service on every healthy run, so if the whole host dies, the one failure no on-host check could ever report, the pings stop and I get alerted anyway. You learn to trust no single layer, including the one watching the other layers.

Am I reinventing a wheel?

I checked, carefully. There are neighbors, and the gaps between them are the whole story. There's a stateless heartbeat tool that kills and restarts stuck sessions but deliberately keeps no memory across the restart. There are memory tools that persist context beautifully between sessions but never rotate a dying one. There's a gateway watchdog that auto-recovers the process but knows nothing about a session's token budget. And Claude Code's own auto-compaction, the closest native thing, is single-session, lossy by Anthropic's own description, and carries a logged bug where the model stops obeying its memory file right after it compacts.

Nobody had put the pieces together: rotate before death, and carry the memory across, and recover the agent, and backstop the hangs. The hardest of those, the one every partial solution skips, is memory across the boundary, which is exactly the part I spent the most time on. And the demand isn't theoretical. Anthropic closed a feature request for "session auto-restart with project context persistence" as not planned, filed by an operator running four or five Claude Code agents who was tired of being, in his words, "a manual restart button for N agents." His own diagnosis: the persistence layer already exists, the only missing piece is the trigger. The need is on the record. It just hadn't been built as one thing.

The unglamorous moat

None of this shows up in a demo. A demo agent runs for ten minutes and looks magical. The difference between that and a fleet that runs for months, remembering, self-healing, surviving its own crashes and the framework's bugs, is almost entirely this boring, invisible plumbing, the same plumbing that 40% of agent projects will skip on their way to being canceled.

Which is the point. Everyone can stand up an agent now. The scarce thing is keeping one alive when no one's watching. Agents are easy to start and hard to keep, and the keeping is the moat.


References: Gartner, agentic AI cancellations; Chroma, "Context Rot"; Liu et al., "Lost in the Middle" (TACL 2024); Karpathy on context engineering; the state of AI agent memory.