$npx -y skills add muratcankoylan/Agent-Skills-for-Context-Engineering --skill interleaved-thinkingDebug and optimize AI agents by analyzing reasoning traces, context degradation, tool confusion, instruction drift, repeated task failures, and performance regressions.
| 1 | # Reasoning Trace Optimizer |
| 2 | |
| 3 | Debug and optimize AI agents by analyzing their reasoning traces. This skill uses MiniMax M2.1's interleaved thinking to provide deep insight into agent decision-making and generate concrete improvements. |
| 4 | |
| 5 | ## When to Activate |
| 6 | |
| 7 | - Agent reasoning traces need debugging, analysis, or prompt optimization |
| 8 | - Agent task fails and user wants to understand why |
| 9 | - User mentions "context degradation", "tool confusion", or "instruction drift" |
| 10 | - Request to improve agent performance or reduce errors |
| 11 | - User wants to generate shareable learnings from debugging sessions |
| 12 | - After repeated failures on similar tasks |
| 13 | |
| 14 | ## Core Concepts |
| 15 | |
| 16 | ### Interleaved Thinking |
| 17 | |
| 18 | Unlike standard reasoning models that think once at the start, interleaved thinking allows reasoning BETWEEN each tool interaction. This is critical because: |
| 19 | |
| 20 | 1. **Long-horizon tasks** require maintaining focus across many turns |
| 21 | 2. **External perturbations** (tool outputs, environment changes) need real-time adaptation |
| 22 | 3. **Debugging** requires seeing HOW decisions were made, not just WHAT was output |
| 23 | |
| 24 | ### The Optimization Loop |
| 25 | |
| 26 | ``` |
| 27 | Execute Agent → Capture Traces → Analyze Patterns → Optimize Prompt → Re-run |
| 28 | ↑____________| |
| 29 | ``` |
| 30 | |
| 31 | Each iteration improves the prompt based on detected patterns until convergence. |
| 32 | |
| 33 | ### Pattern Detection |
| 34 | |
| 35 | Common failure patterns the analyzer detects: |
| 36 | |
| 37 | | Pattern | Description | |
| 38 | |---------|-------------| |
| 39 | | `context_degradation` | Model loses track of information over long contexts | |
| 40 | | `tool_confusion` | Model misunderstands tool capabilities or outputs | |
| 41 | | `instruction_drift` | Model gradually deviates from original instructions | |
| 42 | | `goal_abandonment` | Model stops pursuing the original goal | |
| 43 | | `circular_reasoning` | Model repeats similar actions without progress | |
| 44 | | `premature_conclusion` | Model concludes before completing the task | |
| 45 | |
| 46 | ## Usage Modes |
| 47 | |
| 48 | ### Mode 1: M2.1 Agent Debugging |
| 49 | |
| 50 | Run a task through M2.1 and analyze its reasoning: |
| 51 | |
| 52 | ```python |
| 53 | from reasoning_trace_optimizer import TraceCapture, TraceAnalyzer |
| 54 | |
| 55 | capture = TraceCapture() |
| 56 | trace = capture.run( |
| 57 | task="Search for Python tutorials and summarize them", |
| 58 | system_prompt="You are a research assistant.", |
| 59 | tools=[search_tool], |
| 60 | tool_executor=execute_search |
| 61 | ) |
| 62 | |
| 63 | analyzer = TraceAnalyzer() |
| 64 | analysis = analyzer.analyze(trace) |
| 65 | |
| 66 | print(f"Score: {analysis.overall_score}/100") |
| 67 | for pattern in analysis.patterns: |
| 68 | print(f"Found: {pattern.type.value} - {pattern.suggestion}") |
| 69 | ``` |
| 70 | |
| 71 | ### Mode 2: Full Optimization Loop |
| 72 | |
| 73 | Automatically iterate until the prompt is optimized: |
| 74 | |
| 75 | ```python |
| 76 | from reasoning_trace_optimizer import OptimizationLoop, LoopConfig |
| 77 | |
| 78 | config = LoopConfig( |
| 79 | max_iterations=5, |
| 80 | min_score_threshold=80.0, |
| 81 | ) |
| 82 | |
| 83 | loop = OptimizationLoop(config=config) |
| 84 | result = loop.run( |
| 85 | task="Analyze this codebase and suggest improvements", |
| 86 | initial_prompt="You are a code reviewer.", |
| 87 | tools=[read_file_tool, search_tool], |
| 88 | tool_executor=execute_tool |
| 89 | ) |
| 90 | |
| 91 | print(f"Improved: {result.initial_score} → {result.final_score}") |
| 92 | print(f"Final prompt:\n{result.final_prompt}") |
| 93 | ``` |
| 94 | |
| 95 | ### Mode 3: Universal Session Analysis |
| 96 | |
| 97 | Analyze any agent's previous thinking (works with Claude, GPT, etc.): |
| 98 | |
| 99 | When this skill is activated in Claude Code, it can analyze the current session's thinking blocks to identify issues and suggest improvements. |
| 100 | |
| 101 | ``` |
| 102 | /reasoning-trace-optimizer analyze-session |
| 103 | ``` |
| 104 | |
| 105 | ### Mode 4: Generate Shareable Skills |
| 106 | |
| 107 | Convert optimization learnings into reusable Agent Skills: |
| 108 | |
| 109 | ```python |
| 110 | from reasoning_trace_optimizer import SkillGenerator |
| 111 | |
| 112 | generator = SkillGenerator() |
| 113 | skill_path = generator.generate( |
| 114 | result=loop_result, |
| 115 | skill_name="web-search-best-practices", |
| 116 | output_dir="./skills" |
| 117 | ) |
| 118 | ``` |
| 119 | |
| 120 | ## CLI Commands |
| 121 | |
| 122 | ```bash |
| 123 | # Capture reasoning trace |
| 124 | rto capture "Search for Python tutorials" -s "You are a helpful assistant." |
| 125 | |
| 126 | # Analyze a task |
| 127 | rto analyze "Debug this code" -o analysis.txt |
| 128 | |
| 129 | # Run optimization loop |
| 130 | rto optimize "Research AI papers" --max-iterations 5 --generate-skill |
| 131 | |
| 132 | # Generate skill from artifacts |
| 133 | rto generate-skill my-skill-name --artifacts-dir ./optimization_artifacts |
| 134 | ``` |
| 135 | |
| 136 | ## Integration with Claude Code |
| 137 | |
| 138 | ### Auto-trigger on Failure |
| 139 | |
| 140 | Add to your hooks to automatically analyze failures: |
| 141 | |
| 142 | ```json |
| 143 | { |
| 144 | "hooks": { |
| 145 | "post_tool_error": { |
| 146 | "command": "rto analyze-session --last-error" |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | ``` |
| 151 | |
| 152 | ### On-demand Analysis |
| 153 | |
| 154 | Use the slash command to analyze current session: |
| 155 | |
| 156 | ``` |
| 157 | /reasoning-trace-optimizer |
| 158 | ``` |
| 159 | |
| 160 | This will: |
| 161 | 1. Extract thinking blocks from the current session |
| 162 | 2. Identify patterns and issues |
| 163 | 3. Suggest prompt improvements |
| 164 | 4. Optionally update the system prompt |
| 165 | |
| 166 | ## Guidelines |
| 167 | |
| 168 | 1. **Preserve full context**: M2.1 requires full response history including thinking blocks for o |