Search⌘ K
AI Features

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.

read_note_tool.py · Python 3.14.0
from note_metadata import ALL_NOTES
TEXTS = {note["doc_id"]: note["text"] for note in ALL_NOTES}
READ_NOTE_TOOL = {"type": "function", "function": {
"name": "read_note", "description": "Return the full text of one note by its doc_id.",
"parameters": {"type": "object", "properties": {"doc_id": {"type": "string"}}, "required": ["doc_id"]}}}
def read_note(doc_id):
if doc_id in TEXTS:
return TEXTS[doc_id], False
valid = ", ".join(TEXTS)
return f"ERROR: no note with doc_id {doc_id}. Valid doc_ids: {valid}. Use search_documents to find one.", True
if __name__ == "__main__":
for doc_id in ["note-6", "note-12"]:
content, is_error = read_note(doc_id)
print(f"{doc_id}: is_error={is_error} | {content[:110]}")

In the code above:

  • Lines 3–6: We build a lookup of every note’s text and define the read_note tool.

  • Lines 9–13: We define read_note(). It returns the note’s text and False for a known ID. For an unknown ID, it returns an error that lists the valid IDs and suggests search_documents, plus True to 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 d
note-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
The unknown doc ID returns an error that lists the valid IDs and suggests a search

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 ...