Code Mode: how AI agents call tools through generated code
Checking which of dozens of packages in a monorepo depend on a given one is a routine task people hand to AI agents. A video walkthrough published on October 14, 2026 demonstrates a new mode of the Pi 1.0 agent called Code Mode: the model first writes a small piece of JavaScript, and that code performs the tool calls inside a QuickJS sandbox, handing back only the finished result. In the test, both approaches — plain step-by-step tool calling and Code Mode — made exactly the same 18 tool invocations (1 bash run and 17 file reads) and found the same 2 packages with a direct dependency. The difference is not the outcome but the path: in plain mode the model had to swallow file listings and their contents, while in Code Mode it saw only the final summary.
Where plain tool calling burns tokens
The classic agent loop works like this: the model decides to call a tool, the platform returns the result, the model decides on the next call — and so on until the answer. For bulk tasks this is expensive. To find dependent packages, the agent reads dozens of files, and all of that content flows through the model's context. Every intermediate result costs tokens, time and money, while the useful work is often one percent: the model does not need the file itself, just the answer to "is package X mentioned here".

What Code Mode changes
Code Mode flips the loop around. The model writes a JavaScript program once, with tool calls wrapped as ordinary functions. From that point the code works instead of the model: inside the QuickJS sandbox it reads files concurrently, parses JSON, filters and aggregates the data — and returns only a compact summary to the model. In the test both variants made the same set of tool invocations, but in Code Mode all 17 reads happened inside the sandbox in parallel, and the model received just the summary: two packages with a direct dependency. Fewer tokens in context, lower latency, and fewer chances for the model to lose a detail in the stream of intermediate data.
How to replicate it
- Expose the agent's tools as functions callable from code, not only as JSON schemas for step-by-step invocations.
- Add a rule to the system prompt: for bulk tasks — "first write code that calls the tools and returns a summary"; keep step-by-step calls for one-off actions.
- Run the generated code only inside an isolated sandbox (for example, an engine like QuickJS) with a time limit and no network or filesystem access beyond the explicitly provided tool functions.
- Return a structured result plus the execution log to the model, not raw data: from the log the model can fix the code if it crashed.
When it pays off — and when it does not
Code Mode wins where there are many similar calls: filtering hundreds of records, aggregation, walking dependency trees, reconciling large lists. For single calls — "read one file", "send one message" — there is almost no difference; the code just adds a layer. A separate trap is error handling: the generated program must survive a failed tool call and retry it, otherwise one glitch brings down the whole bulk task.
Why retries are mandatory: our own numbers
Failures are a normal part of any generative workload, and a code orchestrator has to survive them. According to anonymized aggregates from our service's production database (no prompts, no identifiers), over the 30 days from September 4 to October 4, 2026 the platform handled 12,689 generations: 9,907 images, 2,334 videos and 448 music tracks. Over the 90 days from July 6 to October 4, 2026, the share of successful terminal generations was 92.2% for gpt-image-2 out of 3,518, 91.9% for seedream-45 out of 1,127, and 79.7% for the video model kling-3.0 out of 374. Even for stable models, roughly every eighth to tenth call can end in failure — for video, every fifth: if you orchestrate such calls with code, build retries directly into the generated program.
Source — the video walkthrough "Pi Agent 1.0 Code Mode" on Bilibili (published October 14, 2026): bilibili.com/video/BV1rNJk6BEYg. More hands-on breakdowns in our blog.