Skip to content

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:

Ο„=(s0,a0,o0,s1,a1,o1,…,st)

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 (G0) 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 of query_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 ​

ResourceCanonical Reference & LinkSpecific Focus Areas
Primary Curriculum BookAI 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 PlatformLangFuse Sessions & ThreadsVisualizing execution DAGs, multi-step agent trajectories, and doom loop entry points.
Reasoning BenchmarksOpenAI HumanEval & Terminal Bench 2.0Standard 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 LoopDetector class into an agent tool execution loop.
  • Test Case:
    • Mock a tool fetch_data(id=404) that always returns a 404 Not Found error.
    • 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.

Master AI Architecture Training Program