Rough stream of thoughts... no AI was used in the process

Don't give your agent tools. Give it an interpreter.

LLMs are 10x better at writing code than at any other task. To build great agents you need to lean into that - make every task a coding task, and there are two levels to it.

When we moved to a code-mode based agent at Checksum, test generation went from 19 minutes to 2 and cost per test from $1.67 to $0.25, with the same model.

Loop Level Code Mode

Your agent has tools to do the job. The most basic code-mode level is to allow the agent to execute these tools in the same way it executes code. This means that instead of calling the tool in the LLM response directly (e.g. tool calling), you provide the agent with a code interpreter (e.g. Python), and the agent calls the tools the same way it calls functions. The agent writes Python code and uses your tools as if they are Python functions. This means that the agent can store function results in a variable, parse and manipulate these variables with Python, then pass these values to another function. All in one call - just by writing code. You should also make sure that the interpreter has state, so across calls the variables and results persist and the agent can explore the data.

The result of this basic loop level is typically much better performance from the agent across the board - agents can solve more complex tasks and are more efficient (cost and speed), as they don’t need to call each tool one by one.

Monty by Pydantic AI is a great, easy implementation of code mode. It comes with the basic capabilities out of the box and allows you to defer tools as needed. On top of that, you can use the Pydantic AI harness to provide the agent with a workspace, shell, file system etc. that can be virtual, local or run in a sandbox.

I usually recommend starting with the simplest setup possible and evaluating, but for complex tasks you don’t want to tie your agent’s hands behind its back, so eventually you might want to give your agent a full sandbox (which Pydantic AI supports).

Providing a REPL-like interpreter to your agent is an easy win - especially if you use a framework that already supports it.

Task/Tool Level Code Mode

The biggest gain comes from making the entire task a coding task. That part is more nuanced and harder than loop level code mode, which is more mechanical.

On a high level, you want to think about the goal your agent is trying to achieve and how you can make achieving this task a coding exercise. Typically it means providing the data in a machine-parsable format with clear business logic.

For example, let’s take the task of browser automation. Loop level code mode means providing all the tools required to control a browser over an interpreter, so the agent can just write scripts that automate the browser. That would be equivalent to connecting your agent to Playwright MCP with code mode.

But you can take it a step further. The entire HTML of the page is parsable. So instead of just providing tools over a REPL, you can provide the full HTML page through an interpreter. That way, instead of receiving a snapshot and deciding what to do next, the agent can write code that traverses the HTML, finds relevant text and elements based on well-known JavaScript/HTML syntax, and drives the browser that way. A good example of that implementation is browser harness by browser-use.

In my experience, browser harness is significantly faster and more accurate than agent-browser and Playwright MCP. In fact, at Checksum we’ve built an equivalent harness for test automation - similar concepts, only more secure, and it can run at scale with an automatic caching layer.

Caching by design

An added benefit of using code mode, both on the loop and task levels, is the ability to cache actions. Essentially, once the agent is successful or while it’s working, depending on the task, you can have the agent write scripts that execute the task automatically. Since calling tools is just writing code, you can save this code and run it without an agent. For tasks where the state might change, you can look at it as a cache - the agent completes the task, then writes a script to accomplish the same task without AI (using the same tools provided to the agent). On a future run, you run the script, and if it ends with an error, you start an agent. If the task does not change and you control the state, then the cache can become permanent.

Cache is not just a way to save tokens, it allows you to tackle harder tasks in real time. You can send strong models with high thinking effort for a long time to create the cache, so it increases the viability of using agents on complex tasks quickly.

← All posts