Why code mode could cut your agent bill
Blog post from Pydantic
Code mode allows AI agents to write sandboxed programs that call tools through loops, branches, and parallel operations rather than having the model individually manage each tool call and result. A benchmark of 5,276 runs across five small CRUD applications and eight models found that its benefits grow substantially with task length: on long multi-step tasks, code mode reduced average cost by 58% and wall-clock time by 51% while improving success rates by 16 percentage points, and at 200-item workloads it was typically 47% to 75% cheaper while maintaining higher correctness. The results suggest that savings come less from reducing model requests, since modern models can already batch many tool calls, and more from reducing the tokens spent generating calls and processing intermediate outputs. Code mode was less advantageous for small tasks because its instructions impose a fixed overhead, and it can introduce risks such as buggy generated code, duplicated writes after retries, and harder debugging. The described Pydantic AI implementation runs generated code in the Monty sandbox with restricted host access, explicit tool permissions, and time and memory limits, while retaining traces for individual tool calls.
| Trend | Post Mentions | Total Month Mentions | Posts | Companies | MoM |
|---|---|---|---|---|---|
| LLM | 3 | 6,578 | 1,243 | 236 | +9% |
| AI Agents | 2 | 7,036 | 1,672 | 272 | +25% |
| MCP | 2 | 11,940 | 1,236 | 235 | +13% |
| AI Coding Assistant | 1 | 1,966 | 547 | 149 | +23% |
| Observability | 1 | 3,086 | 746 | 186 | +11% |
| OpenTelemetry | 1 | 860 | 177 | 65 | +21% |
| Real-time | 1 | 4,182 | 1,111 | 242 | -14% |
Use this post, company, and trend context to find content marketing opportunities, perform competitive analysis, or address product feature gaps via the Plushcap MCP server or the Plushcap API.