Stateful Code Execution for Long-Running Agent Tasks
Persistent micro-VMs let coding agents accumulate work across hours without resetting.

A single-turn API call ends when the model responds. A long-running agent task is a different thing entirely: an execution loop of planning, acting, observing, and iterating that unfolds across many steps, over hours or days, with each step depending on what the previous one left behind. The infrastructure that serves the first case breaks down in the second.
Why long-running agent tasks are structurally different from single-turn calls
Consider a coding agent at work. The edits must stay on disk between the edit and the next build. This is not a sequence of independent requests. It's one continuous workspace evolving over time.
Long-running agent work breaks into three subtypes, and each one needs its own engineering. Long-horizon reasoning involves multi-step dependent planning where model quality is the binding constraint. Long-running execution involves processes with many model invocations over hours, where runtime architecture is the binding constraint. With persistent agency, the agent's identity and memory outlive any single task. These are related but not identical problems, and an infrastructure choice that serves one may fail the others.
The stakes rise with duration because failure probability compounds over time. Most agent frameworks in wide use were designed around tasks that complete in seconds. The engineering challenge this article addresses is what keeps agents productive when tasks take minutes or hours instead.
What stateful execution actually means at the infrastructure level
Stateful execution is not a mode or a setting. It is a specific set of infrastructure properties that together let work accumulate rather than reset. Serverless functions work this way by design: each invocation stands alone, and nothing carries over. A stateful runtime keeps the environment alive between executions. Environment variables hold their values.
Maritime achieves this by deploying each agent inside its own persistent Firecracker micro-VM with its own kernel and disk, so the agent's workspace accumulates work across steps rather than resetting between invocations. The properties that make this possible are precise. With a real kernel, package installation, arbitrary shell commands, and environment mutation run on a proper operating system, not in a sandboxed function.
The distinction between a sandbox built for agents and one built to run a single snippet is exactly this continuity. The agent works against a continuous workspace rather than a fresh machine each time it acts. It is environment-level persistence that lives entirely outside the model's context window.
Why an ephemeral runtime fails progressively worse as task length grows
The overhead an ephemeral runtime imposes is not a one-time penalty. On an ephemeral runtime, a coding agent re-clones and reinstalls dependencies on every action. In a ten-step task, environment setup is paid ten times. In a hundred-step task, it is paid a hundred times. The repo clone and package installation from the definition section recur at every turn, and each recurrence burns time.
Setup cost is not just latency. Billing models that charge wall-clock time for every second the sandbox is up, including setup, compound this tax at every step. Infrastructure designed around persistent VMs with snapshot-restore scheduling can pause the entire agent state, kernel, disk, running processes and all, when idle and resume in under a second, eliminating the setup tax altogether and making long-horizon tasks economically viable. The compounding cost of re-cloning, reinstalling, and restarting processes on every step is not merely inconvenient. It becomes economically and architecturally prohibitive as task horizons lengthen.
Beyond setup cost, ephemerality destroys intermediate progress. A failure at step 47 of 100 means restarting from zero, not resuming from 46. When task execution and task-state management share the same growing LLM context, the agent's ability to track state degrades as execution history lengthens. This remains an unsolved architectural problem even in current production systems. The argument is not that ephemeral is wrong in general. It is optimal for one-shot, stateless work such as a single calculation or a discrete query. The issue is that it is the wrong primitive for multi-step autonomous execution.
The isolation model that makes a stateful environment safe to run agent-generated code in
Persistence alone is not enough. A stateful environment that shares a kernel with other workloads cannot safely run untrusted or self-generated agent code, so persistence and isolation are jointly required. Isolation levels differ concretely. Plain containers share the host kernel, which means a kernel-level exploit inside the container can reach the host or other workloads. User-space kernels such as gVisor intercept syscalls in user space, putting a real kernel boundary between the workload and the host.
For production agents that execute untrusted or self-generated code, plain container-only isolation generally falls short. gVisor or a micro-VM is the baseline. Firecracker's hardware virtualization path does not yet support VFIO device passthrough, and GPU access via VFIO remains under active development without having shipped in mainline Firecracker. Full GPU passthrough typically requires bare metal or a cloud VM with vIOMMU and VFIO support, because cloud VMs with nested virtualization add overhead per VM operation, reduce isolation guarantees, and complicate PCIe passthrough. True agent autonomy requires an environment where the agent can install packages, break things, and rebuild freely. A micro-VM with its own kernel is the only isolation boundary that enables this without risk to the host.
How snapshot-restore makes stateful micro-VMs fast enough for interactive agent loops
You might think micro-VM isolation costs boot speed, but snapshot-restore changes that. Snapshots are the only free lunch: they move a sandbox along the latency axis without moving it along the isolation axis. Isolation does not decrease, but resume time does.
Content-addressable, copy-on-write storage makes snapshots cheap to store. Only dirty chunks are hashed and uploaded. The same snapshot mechanism serves two distinct agent needs: fast sandbox creation by restoring a pre-warmed snapshot instead of booting cold, and idle-cost elimination by capturing a running agent, tearing down the micro-VM to free host compute, and restoring it when the agent is needed again.
Memory hotplugging extends this further. Firecracker supports scaling memory on demand within a running microVM without downtime via its virtio-mem hotplugging feature, added in v1.14.0. Runtimes built on Firecracker can use this capability so the agent pays only for what it actually uses during a session.
The memory architecture that lets agents accumulate knowledge across steps and sessions
A persistent execution environment is necessary but not sufficient. Long-running agents also need a layered memory architecture, so knowledge stays accessible but does not overwhelm the context window. Four memory layers serve different time horizons. Working memory holds the current task in the LLM context window and is volatile, limited by window size. Short-term memory keeps recent history in-context or compressed, still window-bound. Long-term memory stores user preferences, facts, and learned context in a vector database or key-value store, so it survives across sessions. Episodic memory records past task completions in structured logs plus vector search, so an agent can learn from prior runs.
Episodic memory is an important missing capability for long-lived agents. Most general-purpose orchestration frameworks as of 2026 still offer relatively basic built-in episodic memory, though a growing set of dedicated memory frameworks, including Letta, Zep, LangMem, and Mem0, now provide sophisticated mechanisms for storing and querying past events. The Ralph Loop pattern illustrates the practical approach: task state lives entirely on the filesystem outside the model's context window. An agent can crash, restart, even switch models, and pick up from where it left off because state is external and durable.
The checkpointing discipline that works in practice is to save after every meaningful unit of work, per document in a batch or per stage in a pipeline, not on a time interval. Granularity should match the unit of failure recovery, not a clock. What to checkpoint is the minimum state needed to resume: processed items, intermediate results, current position, and accumulated decision context. Do not checkpoint the entire model context or conversation history, because it grows too fast and makes recovery slow.
Durable execution frameworks and how they automate fault tolerance for long-running agents
Durable execution frameworks wrap the checkpointing discipline in an automatic guarantee. The agent developer writes normal code, and the runtime handles persistence and replay on failure. The mechanism intercepts each step, persists its result, and replays completed steps on recovery. The developer writes loops and function calls, and the framework makes them fault-tolerant.
Durable execution also handles the waiting problem. When an agent needs to pause for a tool response, a human approval gate, or a timer, the framework snapshots the full execution state including memory, call stack, and pending continuations to durable storage rather than blocking a running process. Temporal is the most mature option in this space. Inngest and Cloudflare Workflows are lighter alternatives for teams that don't need Temporal's full capability surface.
LangGraph encodes flow engineering as first-class abstractions: agents defined as directed graphs with typed state, conditional edges, checkpointing for persistence and resumability, and explicit interrupt points for human oversight. What durable execution does not solve is the sandbox layer. It persists execution flow, not the filesystem and process state of the agent's working environment. Pair a durable execution framework with an ephemeral sandbox, and you still lose environmental state on resume. The two are complementary, not substitutes.
The economics of running persistent agent environments at scale
The cost structure of agent infrastructure is almost entirely determined by what is paid during idle time. Idle-cost design is the variable that separates economically viable per-user agent deployment from approaches that break at scale. The biggest hidden variable in sandbox pricing is the billing basis. Wall-clock charges meter every second the sandbox is up, including idle wait time. Active-CPU charges meter only what the code actually burns. For agents that spend most of their time waiting on model responses or human input, the difference is substantial.
The snapshot-and-restore scheduler is the mechanism that makes idle cost approach storage cost rather than compute cost. This is the only mechanism that reduces latency without reducing isolation, and it is what makes per-user agent deployment economically viable because most agents are idle most of the time. Maritime has built its entire pricing model around this insight, so sleeping machines are free and wake latency is around one second.
The build-versus-rent crossover follows from this. Sustained utilization above a threshold in the range of roughly fifteen to twenty-five percent of a slot favors owning infrastructure. Below that, managed sandboxes win. The exact crossing moves with resume-latency requirements and GPU needs.
What the operational cost of running Firecracker yourself actually involves
Firecracker's components are open and well-documented, but running micro-VMs in production is a substantial operational commitment that most agent-building teams underestimate. The full list of what self-managing micro-VMs requires includes kernels, root filesystems, images, snapshots, block devices, networking, host capacity, rate limits, metrics, and cleanup. Unlike Docker, which provides mature CNI plugins, Firecracker requires manually managing TAP interfaces, IP tables, and routing on the host. Networking alone is a non-trivial operational layer.
The alternative is a managed runtime that provides Firecracker isolation without the operational overhead, but you trade infrastructure control for developer velocity. Maritime's position is that infrastructure management should not be the problem agent developers spend time on. The platform handles kernels, snapshots, networking, and scheduling, so developers push code and get a live agent endpoint.
What properties a runtime must deliver for stateful agent execution to be real in production
If a runtime claims to support long-running agents, it must deliver a specific set of properties jointly. Any gap in the set breaks the value of the others. The foundational requirement is a persistent filesystem and process space, because work must compound across steps rather than reset. This is the one most generic runtimes fail on. You also need snapshot and restore at sub-second wake latency, since persistence without fast resume is not viable for interactive or near-real-time agent products. Container-only isolation is not enough for production agents running untrusted or self-generated code.
A real kernel with full package and shell access is what lets agents install, break, and rebuild freely within their environment. An idle-cost model based on storage rather than compute makes the economics of per-user deployment work, and this requires a snapshot-restore scheduler, not just a long-lived process. A simple deployment path is what lets developers go from code to a live agent endpoint without managing kernels, images, networking, or scheduling infrastructure.
Maritime delivers all seven properties. Each agent runs in its own Firecracker micro-VM on bare metal, with a persistent disk, a real kernel, package installation, browser sessions, sub-second wake from snapshot, and a deployment model that requires no infrastructure overhead, at a flat rate that makes per-user deployment economically viable. The runtimes that make stateful agent execution real in production are the ones that treat each agent's environment as a persistent place, not a function call.


