I Read My Agent's Log Backwards, and the Bug Was in Line Two
Normal debugging reads the log from the top: what did it try, what happened, where did it stop. That works when the failure is at the end, which is not where most of mine are. Now I read the last line first, then the li
Normal debugging reads the log from the top: what did it try, what happened, where did it stop. That works when the failure is at the end, which is not where most of mine are.
Now I read the last line first, then the line that produced it, then walk backwards to the first decision that was wrong. The run usually failed at the top.
Why backwards is faster here
An agent that goes wrong at step two keeps going. It spends the rest of the run working on a false premise, and the last thing it does is the least informative thing in the file - an error on an already-wrong task.
Reading forwards, I walked through twenty correct-looking steps before reaching the failure. Reading backwards, I ask "what produced this state" at each step, and I arrive at the bad decision in three or four hops.
What I look for on the walk back
- The first step whose input does not match what I expected. This is almost always the one, and it is usually a file read, a listing, or a state check.
- The first step that silently changed something. Agents write files and start services; the log should say so. If a step has a side effect that is not in the log, that is a logging bug as well as a real one.
- The last plausible command. Not the last command. The last one whose output would be different if the world were what I thought it was.
What this changed in the setup
Once I was debugging this way, I stopped logging so much of the middle and started logging more at the boundaries: every read of external state, every side effect, the exact input that produced each decision. The middle steps are mostly derivable. The boundaries are not.
That is a small change with a large effect: my logs got shorter, and my time to find the wrong step went down, because the wrong step is almost always a boundary read that returned something I did not expect.
If you have done this long enough to have a rhythm, I am curious whether you walk forward or backwards. Mine was forwards for two months and it was costing me more than I noticed.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.