Optimizing Multi-Step Agents - Keeping a Running Log to Prevent Action Loops
The Loop Problem in Multi-Step Agents
If you've built a multi-step AI agent, you've probably seen this: the agent completes step 3, then goes back and does step 2 again. Or it checks the same file three times. Or it runs the same terminal command in a loop, each time acting like it's the first attempt.
This is the action loop problem, and it kills both token budgets and user trust.
Why Agents Loop
LLMs don't have built-in state tracking. Each decision is based on the current context window, and if the context doesn't clearly show what's already been done, the model has no reason to skip a step. It sees "file needs to be updated" in the instructions and updates it again, even if it already did.
Long-running tasks make this worse. As the context fills up with tool outputs and intermediate results, earlier actions get pushed further from the model's attention. The agent literally forgets what it did ten minutes ago.
The Running Log Fix
The solution is straightforward: maintain a running log of completed steps and inject it into the agent's context before each decision point.
The log doesn't need to be fancy. A simple list works:
- Completed: Checked git status
- Completed: Created feature branch
- Completed: Updated config.json
- Completed: Ran test suite - all passing
- Current: Preparing commit message
This gives the agent a clear picture of where it is in the workflow. It won't re-run tests if it can see they already passed. It won't recreate a branch that already exists.
Implementation Details
Write the log to a file that persists across context resets. If the agent's context window fills up and gets trimmed, the log file survives. The agent can read it fresh and pick up exactly where it left off.
Keep log entries concise but specific. "Updated file" is too vague - the agent might think a different file needs updating. "Updated src/config.json - added API endpoint for staging" is clear enough to prevent repeats.
The Broader Lesson
Most agent reliability problems come from poor state management, not model capability. The model is smart enough to do the work. It just needs to know what work has already been done.
- Agent Session State Management
- Agent Execution Is Harder Than Planning
- Context Management Is 90 Percent of AI Coding Skill
Fazm is an open source macOS AI agent. Open source on GitHub.