Error Retention and Controlled Variation in Agent Trajectories
Explore how error retention helps agents learn from failed tool calls instead of repeating them. Learn to shorten long errors without losing key evidence, vary repeated prompts to reduce pattern drift, and test reliable fallback answers across multiple runs.
We'll cover the following...
In the previous three lessons, we reduced context size and improved context organization. We pruned tool results, compacted conversation history, and maintained the current plan in a structured notes file. So far, our examples have assumed that every tool call succeeds. During real incidents, tool calls can fail for several reasons. The assistant may request a nonexistent note, encounter a timeout during a metrics query, or use an incorrect document ID. Failed tool calls may appear to be unnecessary entries in the conversation history, making them candidates for removal.
In this lesson, we'll examine how removing failed tool calls can cause the assistant to repeat unsuccessful actions. Instead, we'll retain compact error records in the context. We'll then examine a related issue that can occur even when all tool calls succeed. Repetitive prompt patterns can lead the model to reproduce previous response patterns rather than evaluate new evidence.
A failed call
Let’s give the assistant a read_note tool. It returns a note’s text, or an error if the doc ID doesn’t exist. A good error message does more than say “not found”. It says what went wrong and what to try instead.
In the code above:
Lines 3–6: We build a lookup of every note’s text and define the
read_notetool.Lines 9–13: We define
read_note(). It returns the note’s text andFalsefor a known ID. For an unknown ID, it returns an error that lists the valid IDs and suggestssearch_documents, plusTrueto mark the error.Lines 16–19: We read one note that exists and one that doesn’t.
The second call returns an error that points the way forward:
note-6: is_error=False | The payments team on-call rotation changes every Monday at 09:00. Page the secondary engineer if the primary dnote-12: is_error=True | ERROR: no note with doc_id note-12. Valid doc_ids: note-1, note-2, note-3, note-4, note-5, note-6, note-7, not
Claude’s native API has a field for this: a tool result can carry is_error: true. We send tool calls through LiteLLM in the OpenAI format, so ...