What the Loop Forgot
Over Christmas break I built a coding agent directly against a model API, and watched it repeat failed edits as its transcript grew.
Skipping orchestration frameworks let me inspect every prompt and tool call alongside raw diffs and test output. Working from a short specification against a local repository, the first three or four tool calls felt effortless. Keeping fifteen consecutive steps pointed at the same objective without drifting in circles proved much harder.
My initial mistake was asking an append-only message array to do two incompatible jobs at once: act as the permanent audit log of everything the process had tried, and act as the focused working memory where the model picked its next move. Failed patches and stale stack traces sat right next to current test output. Context bloated and became noisy, so longer transcripts made the planner less informed rather than wiser.
Splitting the execution log from working context
Separating the durable execution store from the assembled prompt for the next turn changed the agent's behavior immediately. Disk state kept every command and exit code alongside the git tree hash and raw stdout log. Each model call received a curated working view instead: the active subgoal, files modified since the last green check, the latest failing assertion, and the exact open blocker.
Instead of appending raw 10,000-line shell transcripts, each planning turn receives an immutable RunState ledger assembled from disk state:
from dataclasses import dataclass
@dataclass(frozen=True)
class RunState:
goal: str
completed_steps: tuple[str, ...]
open_failures: tuple[str, ...]
attempt_budget: int
def next_planner_payload(state: RunState, tree_sha: str) -> dict[str, object]:
# Pass compact ledger state while raw command logs stay on disk
return {"tree": tree_sha, "state": state, "can_retry": state.attempt_budget > 0}
Compiler diagnostics make the staleness problem concrete. Before an edit, a type error on line 84 tells the planner exactly where to look. Two commits later, line 84 holds different code entirely, yet if the earlier traceback still sits verbatim in the message history, the model wastes turns reconciling ghosts from an older working tree.


Compression only helps when pointers back to raw evidence survive. Replacing a 200-line test log with "pytest failed" strips away the assertion diff the model needs to fix the bug. Recording a structured handle with the command, commit hash, failing test names, and an on-demand tool to inspect slices of the stored log keeps the prompt lean without blinding the agent.
Relevance also shifts across turns. A warning ignored safely on step four becomes the smoking gun when the same integration test breaks again on step eleven. Because the full execution record stays on disk outside the prompt window, the harness can pull an earlier artifact back into view whenever a failure signature recurs.
Detecting loops before transcripts bloat
Anthropic's guide on Building effective agents emphasizes simple control loops, clean tool contracts, and ground-truth environmental feedback. Writing the harness by hand taught me that raw tool feedback decays fast unless the runtime tracks state transitions explicitly.
When step nine produces the exact same test failure hash as step six, hoping the language model notices its own repetition inside 40,000 tokens of history is wishful thinking. Computing diff and failure fingerprints in deterministic Python code lets the runtime flag repeated dead ends directly in the working state and force a plan revision before spending another ten API calls.
We won't get production-grade agents by making prompts cleverer. We'll get them by treating context assembly and loop detection with the same systems discipline we apply to any stateful runtime. When your multi-step agents stall out, is the model lacking capability or drowning in its own transcript?
Continue in AI Architecture & Agents
Save the Failing Run
Last Christmas I built a coding agent from scratch to read a spec, plan repository edits, run tests, and recover when checks failed. Single tool calls were…
Before You Show Me the Token Chart
I am glad to fund engineers exploring what AI tools can do, knowing upfront that plenty of those experiments will hit dead ends. Demanding a guaranteed ROI…