Appearance
Module 2.5: Trajectory Analysis & Doom Loop Recovery β
Curriculum Alignment:
docs/plan/02_phase2_engineering_of_reasoning.md
Topic Scope: Multi-Step Reasoning Traces, Reasoning Deviations, Incorrect Tool Selection, Doom Loop Detection & Recovery
Level: Advanced AI Engineering / Architecture
1. What is a Reasoning Trajectory? β
In an autonomous multi-step agent, a Reasoning Trajectory is the historical chronological sequence of states, thoughts, actions, and observations:
Unlike traditional deterministic applications that immediately fail with stack traces when an error occurs, an agentic reasoning pipeline typically degrades through subtle trajectory pathologies:
text
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β TRAJECTORY PATHOLOGIES β
βββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ€
β 1. REASONING DEVIATION β Gradual drift away from user β
β β objective over multi-turns. β
βββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β 2. INCORRECT TOOL β Selecting a destructive tool β
β SELECTION β when read-only sufficed. β
βββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ€
β 3. THE DOOM LOOP β Repeating identical failing β
β β actions in an infinite loop. β
βββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ2. Diagnosing Pathologies in Multi-Step Traces β
1. Identifying Reasoning Deviations β
Over long multi-turn sessions (10+ steps), context drift can cause the agent's internal state to subtly alter the original goal.
- Symptom in LangFuse: Early thoughts cite the primary user objective, but later thoughts branch into solving sub-problems that do not advance the original goal.
- Remediation: Periodically re-anchor the agent by prepending the immutable root goal (
) at the start of each thought prompt.
2. Incorrect Tool Selection β
The agent selects a high-impact tool or incorrect tool when an alternative was appropriate:
- Symptom in LangFuse: The model executes
drop_database_table()instead ofquery_table(). - Remediation: Granular tool privilege tiers (read-only vs write) and requiring human confirmation for destructive actions.
3. The "Doom Loop" Entry Point β
The agent executes a tool with invalid parameters, receives an error observation, and then repeatedly generates the exact same failing action:
3. Loop Detection & Recovery Heuristics β
To break doom loops autonomously without human intervention:
python
"""
Doom Loop Detector Heuristic
Intercepts repetitive failed actions and triggers an autonomous step-back.
"""
import hashlib
from typing import NamedTuple
class ToolCall(NamedTuple):
tool_name: str
arguments_hash: str
class LoopDetector:
def __init__(self, repetition_threshold: int = 2):
self.history: list[ToolCall] = []
self.threshold = repetition_threshold
def record_and_check(self, tool_name: str, args_str: str) -> bool:
"""Returns True if a doom loop is detected."""
args_hash = hashlib.sha256(args_str.encode()).hexdigest()[:8]
current_call = ToolCall(tool_name, args_hash)
self.history.append(current_call)
# Check if the last N calls are identical
if len(self.history) >= self.threshold:
recent_calls = self.history[-self.threshold:]
if all(call == current_call for call in recent_calls):
return True
return False
def generate_step_back_directive(self) -> str:
"""System prompt injected to force the model to break the loop."""
return (
"[SYSTEM DIRECTIVE: REPETITIVE FAILURE DETECTED]\n"
"You have attempted the exact same failing tool action multiple times. "
"Take a step back. Do NOT repeat this action. "
"Analyze why the previous action failed and formulate an alternative strategy."
)Conceptual Mindmap: Trajectory Analysis β
4. Curated Reading & Canonical References β
| Resource | Canonical Reference & Link | Specific Focus Areas |
|---|---|---|
| Primary Curriculum Book | AI Agents and Applications (Google Drive) | Chapter 11, Section 11.6.1 (p. 307) & Section 11.9.3 (p. 316): Step-by-step trajectory debugging, inspecting state transitions, and observing execution graphs. |
| Observability Platform | LangFuse Sessions & Threads | Visualizing execution DAGs, multi-step agent trajectories, and doom loop entry points. |
| Reasoning Benchmarks | OpenAI HumanEval & Terminal Bench 2.0 | Standard datasets for measuring multi-step reasoning accuracy and trajectory fidelity. |
5. Active Recall (Module 2.5 Flashcards) β
Trajectory DebuggingClick or press Space to flip βΊ
What causes an agent to enter a 'Doom Loop' during multi-turn tool execution?
Trajectory Debugging β’ AnswerClick to flip back β»
An agent enters a doom loop when a tool returns an error or unexpected observation, but the model lacks sufficient context, recovery heuristics, or diverse sampling to alter its plan, causing it to emit identical failing tool calls repeatedly.
π‘ Architect Takeaway: Implement action-history hashing and loop breakers that force a step-back upon repeated failures.
Trajectory MonitoringClick or press Space to flip βΊ
How can reasoning deviation be identified in LangFuse execution trees?
Trajectory Monitoring β’ AnswerClick to flip back β»
By comparing intermediate generated thoughts against the root user objective. When semantic similarity between thoughts and the original user goal steadily decreases over successive turns, reasoning deviation is occurring.
π‘ Architect Takeaway: Re-anchor multi-turn prompts with the root user goal at the start of each thought step.
6. Hands-on Engineering Exercises β
Exercise 2.5: Doom Loop Breaker Drill β
- Task: Implement the
LoopDetectorclass into an agent tool execution loop. - Test Case:
- Mock a tool
fetch_data(id=404)that always returns a404 Not Founderror. - Verify that upon the second consecutive failure, the loop breaker intercepts execution, injects the step-back directive, and forces the model to choose an alternative tool or report failure.
- Mock a tool