Code mode that survives a crash Pi Durable, the crash-resilient variant of the Pi coding agent, resumes a code-mode script after a process crash without re-running tool calls that already completed, according to a technical post on the harness. The post explains that code mode, in which the model writes one sandboxed script that calls tools as functions and returns only the script's output, cut the amount of tool output reaching the model in a small experiment, and that Pi and OpenCode both enable code mode only once MCP tools are connected. The author notes the trade-offs: sandboxing model-written code, pausing mid-script for per-call approval, and scripts that fail partway when the model's assumptions are wrong. 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 , for example, 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. I previously 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 now, including the two most popular open sources ones - 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 I looked at codemode support in Pi the coding agent , Pi Durable and OpenCode. They differ in the runtime, in how tools appear to the script, and in what happens on a restart. | Feature | 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.