Ghost Mode
Ghost Mode is a Rust library that brings Code Mode to agents. With normal tool calls, every result goes back to the model before it can ask for the next one. Ghost Mode lets the model write a short Python script that chains the calls itself, so intermediate results never go back to the model.
Think about it like this: normal tool calling is like going back to the store 30 times to get 30 items. Code Mode is one trip with a shopping list.
Say the model needs to know why a pull request's checks failed. Each step needs the one before it. The pull request gives the commit, the commit gives the check runs, and each failed run gives a log. With normal tool calls, that's a round trip to the model per step, and every full result goes through the model. With Ghost Mode, the model sends one script:
=
=
=
The script runs inside your process. The model only gets the last 500 characters of each failed log, not the pull request, every check run, and every full log.
Why Ghost Mode
- Fewer tokens. Anthropic cut one workflow from 150,000 tokens to 2,000 (98.7% less) by having the agent write code against its tools instead of loading every tool definition up front (post). Cloudflare fits its whole API, 2,500+ endpoints, into about 1,000 tokens, where plain MCP tools would take 1.17 million (post).
- API bills up to 70% lower. Across eight models, code mode cut cost by 70.6% for GPT-5.4 and 40.7% on average (benchmark). Intermediate results never reach the model, so you aren't billed for them as input on every later step.
- Fewer steps, better results. Models have read way more code than tool-call JSON. Across 17 models, CodeAct's code actions needed up to 30% fewer actions and scored up to 20 points higher success than JSON or text tool calls (paper).
Savings depend on the model. In the eight-model benchmark, weaker models retried failed scripts until some runs cost more than plain tool calls, and Gemini models passed fewer tasks. A pre-registered study measured 70% to 90% fewer tokens with three open-weight models. In Ghost Mode, the
Limitsyou build theSandboxwith cap how much any one result can add (40,000 characters by default).
Those are other teams' Code Mode numbers. Ghost Mode brings the same idea to Rust.
What it takes: give a Sandbox your tools, or point it at an MCP server, and hand the model only two function definitions, execute and poll. The Monty backend runs the script inside your process, so there's no container or extra service. The script can only reach the outside world through your tools.
How it works
Every arrow inside the caller's process is a plain Rust function call. The model never runs code and only sees the definitions of execute and poll. Writing a host names each part and follows one execute from start to end.
Usage
Write a host, build a Sandbox, and give the model the execute and poll definitions. When the model calls execute, pass its code to Sandbox::execute and send the Status JSON back. If the run is still going, the model calls poll with the run_id, and you pass that to Sandbox::poll.
The crate docs on docs.rs open with a full example: a host with one tool, and a Sandbox that runs one script.
Pass Sandbox::tools to schema::mcp_definitions for the pair in MCP shape, or to schema::openai_definitions for OpenAI functions. With the rmcp feature on, pass an mcp::Server to Builder::new instead of writing your own host.
Monty, from the monty feature that is on by default, runs the script inside your process and isn't crash-isolated. For untrusted scripts, turn on the monty-pool feature and pass a backend::MontyPool to Builder::new, which runs each session in its own worker process. On Tokio, read Where scripts run first.
Sandbox::dump saves an idle session as bytes, and Builder::restore loads them into a new sandbox. Before you store dumps where others can write, set Builder::signing_key. Restore then refuses bytes the key didn't sign.
Build from source
You need rustup. It installs the Rust version that rust-toolchain.toml pins.
The default build includes Monty. Add --all-features to also build mcp::Server, which the rmcp feature turns on, and backend::MontyPool, which the monty-pool feature turns on. The monty-pool feature's dependencies need a C compiler. CONTRIBUTING.md lists every check that CI runs, and how to install the worker binary that the monty-pool tests need.
Contributing
Issues and pull requests are welcome. CONTRIBUTING.md says how.
License
Ghost Mode is under the MIT license.