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/
antoineMoPa•53m ago
what does "write code against its tools" mean in practice?
legend-of-alan•29m ago
Especially for dependent calls.
So in code, this is roughly the difference between:
```
// old:
loop x4:
// do something with the result```
And
```
// new (no loop!)
script_to_run = ask_llm(my_tools_list)
result = run_sandbox(script_to_run)
// do something with the result
```
In the new version we’ve mostly eliminated the need for the loop, and you’ll notice we only needed to call ask_llm() once! The LLM is batching multiple dependent tool calls into a little script that you run in a secure sandbox so you don’t have to go to the oracle of Delphi every time you need to call a tool.
In practice it allows the LLM to “think” in workflows instead of in terms of individual function calls. Not only does it save LLM calls, but it saves context because the LLM doesn’t often need to know every intermediate result.
Another example is from the README. In code mode, the LLM generates this script. It’s one call to the LLM and never leaves your pc. In contrast, normal tool calling would turn each of these calls into a call to the LLM.
```
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]
```