Self-improving AI agents: when a recursive harness actually pays off
Updated
Illustrative photo
The problem with a fixed tool list
Almost every agent harness used today, including coding ones, shares the same problem: it is bloated and not task-specific. Developers try to guess every tool an agent might need upfront and connect a whole bunch of MCP servers. When 30 or 40 generic tools sit in the context window on every turn, the agent often cannot pick the right tool for a specialized problem, or that tool is missing entirely. The alternative is to start with a tiny three-tool harness—bash, read, and write—and let the model recursively evolve its own task-specific harness while it works.
The architecture splits into three zones. Zone 1 is the Gemini Interactions API, which keeps conversation state on Google's server and lets the harness inject an updated tool list and system prompt every turn without restarting the session. Zone 2 runs inside a locked-down macOS sandbox, where the agent starts with a base kernel of three tools—shell, read file, and write file—plus a toolbox folder with recursive primitives. Zone 3 holds the guardrails outside the sandbox. The base tools are blocked inside the sandbox so the model can only write inside its project folder and cannot read the developer's keys.
Tools that pass their own tests
To grow the harness, the model calls create_tool with five things: a name, a description, a parameter schema, Python code, and at least one unit test. Before registering the tool, the harness loads the code and runs the unit test inside the sandbox. Only if every test passes does the tool get saved into the toolbox folder and added to the next turn's tool list.
This gate saved the first run: the model wrote a screenshot tool, its first test failed because the browser binary it tried to launch was not installed, and the harness rejected the tool and sent back an error. The model located the installed Chrome binary, fixed the tool, passed the test, and on its next turn called the new tool to inspect its own web page.
Because custom tools can return images straight back to the model as image input, that single tool let the text-only harness give itself eyes.
Hooks and a definition of done
Tools give the agent new capabilities, but they do not change how the harness loop itself behaves. That is what hooks do. A hook attaches one of the model's own tools to a specific moment in the harness loop. The first hook point is session_start, which runs before turn one of a task and shows its output to the model right away.
The second is before_done, which runs when the model says it is finished; if the attached tool reports a failure, the harness blocks the finish and sends the failure back to the model. That means the model can write its own task-specific definition of done, and the harness enforces it automatically on every future task.
A short note file carries knowledge across tasks, and a reflection session lets the model fix, merge, or prune tools and hooks.
Two guardrails worth copying
Letting an agent write its own tools and its own definition of done creates two immediate ways it can cheat or break the system. First, the model never sees how it is graded. Every task is scored by hidden checks that run outside the sandbox, and the model only gets back the names of the checks that passed or failed. Second, tools inside the sandbox never hold secrets. If a custom tool needs to call a model, it goes through a small broker running outside the sandbox that holds the API key and enforces a strict budget. The author says these two guardrails made the biggest difference in the runs and recommends copying both.
Where the recursive harness wins, and where it bloats
Round one was a tie: both harnesses passed every hidden check on every app and took almost the exact same amount of time. Round two removed the image helper and the external broker, and the frozen agent still rebuilt a pricing page down to the pixel by writing its own scripts. Round three used a custom binary file format, where every task needs a complex parser.
There the recursiveharness paid off: it spent a few extra minutes turning its parser into a library on Task 1, then called that parser right away on the next four tasks and finished significantly faster than the frozen agent, which had to write a new parser from scratch each time. But a second evolving harness that started with an empty toolbox kept adding checks to its before_done hook until, by Task 5, the growing checklist ate 100% of the time saved from reusing the parser.
The takeaway: a recursiveharness does not make the model smarter, since a strong model builds throwaway scripts inline anyway. It pays off when a capability is expensive to rebuild, when tasks repeat in a domain family, and when the evolution stays lean.
Affiliate links: we may earn a commission from qualifying purchases, and it never changes our data or rankings. They open Amazon's search results: prices and availability are shown there.