Sub topic 10
Errors and Test Output as Feedback
This chapter assumes you have read the chapters on tool calls and the agent loop. You should know that the harness runs a tool and appends its result to the messages as text. Here we look at what happens when that result is an error or a failing test.
Why errors are useful
A model cannot see your machine. It only sees text that the harness puts into the conversation. When a tool fails, the error text is often the most useful information the model gets. A stack trace names the file and line. A failing test names the expected and actual values. A "file not found" message tells the model that its guess about the path was wrong.
So the harness should not crash when a tool fails. It should turn the failure into a tool result and let the model try again. The agent loop from the earlier chapter already does this:
try:
result = TOOLS[call["name"]](**call["arguments"])
except Exception as e:
result = f"error: {e}"The error becomes the next message the model reads, and the loop continues.
Kinds of feedback
| Source | What the model learns | Typical next step |
|---|---|---|
| Bad arguments (missing field, wrong type) | The call did not match the schema | Resend with corrected arguments |
| Missing file or command | A path or name was wrong | Search or list the directory |
| Compiler or linter output | Exact location of a syntax or type problem | Edit that line |
| Failing test | Which behavior is wrong, with expected and actual values | Read the code, then edit |
| Timeout | The command ran too long | Try a narrower command |
Test output is the strongest signal. It is an objective check that the agent did not write from its own beliefs. A common pattern is: run the tests, read the failure, edit, run again, until they pass.
Writing error messages the model can use
The model acts on what the message says, so be specific.
- Poor:
error: failed - Better:
error: read_file: no such file 'src/utils.py'. Did you mean 'src/util.py'?
Include the tool name, the cause, and a hint when you have one. Return the exit code and both stdout and stderr from commands, as run_command does. A command that exits with code 1 and prints nothing leaves the model guessing.
Keep errors short too. The run_command tool keeps only the last 5,000 characters, because a long build log can fill the context window. The end of a log usually holds the real failure.
Common failure patterns
- Repeating the same call. The model sends the same failing call again and again. The step limit stops this, but a harness can also detect identical consecutive calls and add a note such as "this call already failed with the same arguments."
- Hiding the error. If the harness catches an exception and returns an empty string, the model assumes success and moves on. Always return something that shows failure.
- Fixing the symptom. The model edits a test to make it pass instead of fixing the code. Instructions in the system prompt, and human review of diffs, guard against this.
- Trusting stale output. The model reasons from an earlier test run after it has edited files. Rerunning the tests after each change keeps its picture current.
Exercise
A run_command tool returns an empty string when a command fails with a non-zero exit code. Why is this a problem for the agent loop, and how would you fix it?
Show answer
The model sees an empty result and cannot tell failure from a command that printed nothing, so it may assume success and continue with a wrong picture. Fix it by including the exit code and stderr in the returned text, for example "exit code 1" followed by the error output, still truncated to a fixed length.