本文へ移動
AI NEWS JOURNAL
Hacker News Show HNAI NEWS / JOURNAL

Show HN: Open-Sourced Code Mode to Cut ~60% of My Rust Agent's Token Burn

For those who are tracking, Anthropic released an announcement last week about their experimental code mode for Claude Code, claiming some bonkers token savings when it comes to tool calls (95% on some workloads(1)). I haven't benchmarked it myself, but independent studies come in somewhere closer to 60~70% savings (2), but I digress. TLDR; I built an open-source version for the community.Context: I'm writing an AI RPG engine called Spookery, and I immediately wanted to try this technique for myself when I heard of it. Agent deployment is currently dominated by token costs, especially during tool calling (single prompt generating tens or hundreds of tool calls). Unfortunately, it looks like the only ones who are doing this seriously are big agentic products like Claude Code and Codex. So, I thought it'd be fun to try it myself and bring it to you all!The idea of Code Mode is simple but powerful. Instead of passing back tool call results back to the LLM one at a time, let the LLM batch calls by letting it write a short script using your tools. Basically taking 30 trips to the grocery store for 30 items and turning it into one trip to the store with a list in-hand.There's a few near-ish open-source options, but they used TypeScript backends, we're a little boilerplate heavy, and I wanted to have something a little faster and Rust friendly for my game engine. Pydantic's Monty seemed a good candidate--their self-reports are around 5x faster on execution and 100x faster on startup (3). I wouldn't take the numbers to the bank, but it's written on a Rust framework and the python backend is entirely built from Rust too.Anyway, I had a couple of days off unexpectedly. So, I went temporarily insane and built a Rust tool to bring code mode to the greater community for free. In enough days to fit on one hand (and just as many hours of sleep), I came up with the implementation linked. I call it Ghost Mode. It's MIT licensed. It's a drop-in for your agent that already makes traditional tool calls. With ~20 lines of setup, you can convert your existing agent to a Code Mode agent.I hope you get something out of this, and maybe I can save you some money. Considering how limiting token economics are right now, I think this could be a game changer for the industry. If there's interest, I can imagine we can build also add some great new features, language compatibility, and other great things. Thanks for reading :)Sources: (1) https://www.marktechpost.com/2026/11/17/anthropic-claude-cod... (2) https://arxiv.org/abs/2611.19123 (3) https://pydantic.dev/docs/monty/get-started/ Comments URL: https://news.ycombinator.com/item?id=50665796 Points: 1 # Comments: 0

翻訳サービスが停止中のため原文を表示しています。

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: pr = get_pull_request(number=42) runs = list_check_runs(commit=pr["head_sha"]) failed = [run for run in runs if run["conclusion"] == "failure"] [{"check": run["name"], "log": get_log(run_id=run["id"])[-500:]} for run in failed] 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 Limits you build the Sandbox with 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. cargo add ghostmode 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. git clone https://github.com/legend-of-alan/ghostmode.git cd ghostmode cargo build --locked cargo test --locked 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. Ghost Mode is under the MIT license.

出典

原文を読む ↗

関連テーマ

← すべてのニュースへ