Code Mode That Survives a Crash: Durable Scripts in Pi Durable Pi Durable's code mode lets a model-written tool script resume after a process crash without re-running tool calls that already completed, according to a Pi blog post. The post explains that code mode keeps intermediate data inside the script so only its output returns to the model, and that Pi Durable treats the commit as the unit of persistence, saving transcript, tasks and documents together. In the author's small experiment with two MCP tools, the code mode tool made the first prompt larger rather than smaller in both Pi and OpenCode, which enable code mode only once MCP tools are connected. Code Mode That Survives a Crash: Durable Scripts in Pi Durable Code mode lets the model write a script that calls tools. Pi Durable makes that script resume after a restart without rerunning finished calls. What code mode is, how durable replay works, and when to reach for it. 1. 1. Managing Context Windows with pi /tree: Branches, Summaries, and Subagent-like Workflows https://stacktoheap.com/blog/2026/02/26/pi-tree-context-window-management 2. 2. Pi Durable: A Look at the Future of Agent Harnesses https://stacktoheap.com/blog/2026/10/05/pi-durable 3. 3. Code Mode That Survives a Crash: Durable Scripts in Pi Durable 4. 4. Two Harnesses Walk into a Claude Subscription https://stacktoheap.com/blog/2026/10/30/claude-subscriptions-two-harnesses 5. 5. DeepSeek Harness on a Claude Subscription in 120 Lines https://stacktoheap.com/blog/2026/11/09/claude-subscription-deepseek-harness 6. 6. Pi Durable ♥ Actors: One Agent, One Owner, Any Worker https://stacktoheap.com/blog/2026/11/19/pi-durable-heart-actors Most agent harnesses run tools the same way. The model asks for one or more calls, the harness runs them, and every result is written into the context before the model takes its next turn. Parallel calls shorten that loop when the calls are independent. They do not help when the next call depends on what the last one returned. Note: Pi does not write very large tool results into the context whole. Large outputs from bash and from MCP tools tools served by an external process over the Model Context Protocol are trimmed, and the full text goes to a temp file the model can read. The model still pays for whatever it reads back. Code mode changes that. The model writes one script that calls the tools, and only the script’s output comes back. The intermediate data stays in the script. In the small experiment later in this post, that was the main benefit: in the Pi coding agent, much less tool output reached the model. Here is a list lookup that feeds a detail lookup, run with direct calls and then in code mode: But what happens to that script when the process dies halfway through? In Pi Durable, it resumes, and the tool calls that already finished do not run again. In my previous post https://stacktoheap.com/blog/2026/10/05/pi-durable , I looked at how Pi Durable treats the commit as the unit that matters: the transcript, tasks, and documents are saved together or not at all, so what you see is what was saved. Code mode puts that idea to the test, because a script is work in flight that the commit has to cover too. This post covers what code mode is and what it costs, how three harnesses implement it, a small experiment, how Pi Durable replays a script after a crash, a crash test of that replay, and when I would use it. What code mode is Code mode is a way of running tools in which the model writes a script and the host runs it in a sandbox. The script calls tools as functions, and only its output goes back to the model. The argument for it is that models are better at writing code against an API than at making tool calls one at a time. Several agent harnesses ship code mode, including Pi and OpenCode. Why use it Less context. The script can filter, join, or count intermediate data in code. That data stays in the script, and the model only sees the answer. Chaining without round trips. Models can already make parallel tool calls, so parallelism is not the win. The win is chaining: one call’s result feeds the next without a trip back to the model. Smaller tool definitions. With many MCP servers connected, the tool definitions alone can take tens of thousands of tokens before the model reads the request. Code mode can load only the definitions it needs, by browsing them as files or searching for them. That saving depends on how many tools you have. My experiment had only two MCP tools, and in both harnesses the code mode tool made the first prompt larger, not smaller. Note: Both Pi and OpenCode turn code mode on only once MCP tools are connected. With just the built-in tools, code mode adds its own description to the prompt and has few tool definitions to save. What it costs Sandboxing. You are now running model-written code. The host has to decide what that code can reach, and running a sandbox is operational overhead that direct tool calls avoid. The implementations below make very different choices here. Approval per call. A host that asks a person before a sensitive tool call now has to pause in the middle of a script, because one script can call many tools. A script that waits on a person is also a script that has to survive while it waits. Half-done work. With direct calls, the model can recover from a bad result on the next step. A script cannot do that. It runs to the end on whatever the model assumed when it wrote the script, and if that assumption is wrong, it fails partway. For example, the model may test a script on a few items, see it work, and then run it on the full list, where the server caps how many items it returns. By then some calls have finished and some have not, and the harness has to decide what happens to the rest. Restarts. A script is in-flight work. If the process dies mid-script, something has to decide what happens next. The rest of this post is about that question. Three implementations Pi appears twice below. The Pi coding agent is a command-line harness with code mode built in. Pi Durable is a library for building agent apps whose state survives a restart, and it adds code mode through an extension. OpenCode is a separate command-line harness. All three ship code mode now. They differ in the runtime, in how tools appear to the script, and in what happens on a restart. | | Pi coding agent | Pi Durable | OpenCode | |---|---|---|---| | How to get it | Built-in codemode tool since 0.99.0; on automatically when an MCP server connects, or set "defaultTools": "+codemode" | @earendil-works/pi-durable/codemode extension | execute tool, on by default for MCP tools | | Runtime | JavaScript in QuickJS | JavaScript in QuickJS @earendil-works/pi-codemode , or a remote sandbox on Cloudflare | Its own interpreter for a JavaScript-like language | | Tools in scripts | tools.