/00 — boot sequence

Hello.

Article

Running AI Coding Agents Continuously for Days Without Losing Context

April 19, 2026•4 min read
AI Programming

_Imagine a development workflow where an autonomous coding assistant can keep working for weeks, never forgetting what it was doing. That scenario used to sound like science‑fiction, but Simon Last, co‑founder of Notion, recently demonstrated a technique that kept an AI coding agent alive for 13 straight days—far beyond the usual hour‑long limits. In this post we break down the underlying ideas, walk through a practical implementation, and discuss operational tips you can adopt today.

Why most AI agents stop after an hour

The hidden constraints

Most large‑language‑model (LLM) based agents hit a hard ceiling after a short burst of activity. The primary culprits are:

  • Token limits – each request consumes part of the model's context window; once the window fills, older messages are dropped.
  • API rate limits – providers cap the number of calls per minute, forcing a pause.
  • Cost considerations – continuous prompting can quickly become expensive.
  • Stateless design – many quick‑prototype agents treat each turn as independent, discarding valuable state.

These factors combine to make a typical “run‑once” agent last only a few minutes to an hour before it either runs out of context or the developer manually restarts it.

Simon Last’s 13‑day method at a glance

Simon’s approach hinges on three pillars:

  1. External state persistence – store the agent’s internal representation (variables, plan steps, partial code) in a durable medium such as a database or file system.
  2. Token budgeting – monitor token usage per request and trim or summarize older messages before they exceed the model’s window.
  3. Periodic checkpointing – every few minutes the agent writes a snapshot of its current plan and receives a fresh “heartbeat” prompt that re‑injects the saved state.

By decoupling the agent’s memory from the LLM’s context window, the system can survive crashes, rate‑limit pauses, or even a full service restart without losing the plot.

Building a resilient long‑running agent: step‑by‑step

1. Define a robust state schema

Create a JSON‑serializable structure that captures everything the agent needs to resume work:

  • Current goal – high‑level description of the task.
  • Task queue – ordered list of subtasks.
  • Partial artifacts – snippets of code, configuration files, or test results.
  • Metrics – elapsed time, token consumption, cost so far.

2. Choose a durable storage backend

For most projects a lightweight SQLite database or a Redis instance works well. The key is atomic write‑and‑read operations so that a checkpoint is never partially persisted.

3. Implement token‑aware prompting

Before each LLM call, calculate the total tokens of the prompt. If you’re approaching the model’s limit (e.g., 8 k tokens for GPT‑4), compress older messages:

  • Summarize previous steps into a concise paragraph.
  • Drop irrelevant debug logs.
  • Keep only the most recent interactions that affect the next decision.

4. Set up a watchdog timer

A simple loop can act as a heartbeat:

python

If the loop detects an exception (network outage, rate‑limit error), it retries after a back‑off period and continues from the last saved state.

5. Periodic checkpoint and resume logic

When the agent restarts, it reads the latest checkpoint and reconstructs the in‑memory state. The first prompt after a restart should include a brief “You are resuming a previous session” instruction so the model aligns its context.

Sample implementation (Python)

Below is a minimal illustration that ties the pieces together. It uses OpenAI’s chat endpoint, SQLite for persistence, and a token‑budget helper.

python

This skeleton can be expanded with error handling, cost tracking, and richer state management, but it demonstrates the essential pattern: persist, trim, and heartbeat.

Operational considerations for production use

  • Rate‑limit awareness – respect provider quotas; implement exponential back‑off.
  • Cost monitoring – log token usage per step and set alerts when daily spend exceeds a threshold.
  • Logging & observability – ship checkpoint diffs to a log aggregation service so you can audit the agent’s decisions.
  • Security – encrypt stored checkpoints if they contain proprietary code or secrets.
  • Graceful shutdown – catch termination signals (SIGTERM) and force a final checkpoint before exiting.

Key takeaways

  • Decouple the agent’s memory from the LLM’s context window by persisting state externally.
  • Implement token budgeting to keep prompts within model limits.
  • Use a regular heartbeat loop that checkpoints state, enabling recovery after interruptions.
  • Monitor rate limits, costs, and security implications when running agents for extended periods.
  • A modest amount of engineering effort can stretch an AI coding assistant’s runtime from minutes to weeks.

Source: How to Run AI Coding Agents Continuously for Days Without Losing the Plot

Resources & Links