Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

AI

Why Does Every AI Agent Still Look Like `while (true) { ... }`?

Open any agent codebase today — Claude Code, Codex, Pi, most of the open-source ones — and you'll find the same skeleton. Something like: let state = {}; while ( true ) { const plan = await llm . plan ( state ); const results = await runTools ( plan ); state = updateState ( state , results ); if ( isDone ( state )) break ; } It's the natural first design. The model is the brain, the loop is the…

Why do all AI agents seem to have the same basic structure? The code for most AI agents, like Claude Code, Codex, or Pi, looks almost identical. It creates an empty state object and then enters a never-ending loop. Inside the loop, the agent plans a task using the current state, runs tools to execute the plan, updates the state with the results, and checks if the task is complete. If not, it repeats the loop.

This design is the natural first step for AI agents. It works well for simple demonstrations, but when you start using an agent for longer periods, problems start to appear. Interruptions are handled awkwardly. If the user stops the process in the middle of a turn, or a tool hangs or asks for clarification, the half-finished iteration is left in the state.

You either discard it or try to patch it in place, causing the state to misrepresent what actually happened. Retry mechanisms become special cases. When a tool fails, you need to write try/catch blocks, decide whether to retry or pass the error to the model, and remember to add it to the state. After a while, you end up with several ad-hoc branches for what happens when a turn doesn't finish cleanly.

Handling parallel tool calls is also challenging. If the model wants to call multiple tools at once, like read and grep, the loop has to sequence them or spawn promises and reassemble the results before the next iteration. This requires additional state to manage. And because state is just a mutable object, you can't branch from an earlier point in the conversation without reconstructing it manually. Debugging long sessions becomes difficult as well since you can't rewind or replay what the model saw based on the state.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

More from Tuesday 18 August →