- name
- reasoning-techniques
- description
- Implements advanced reasoning methodologies (Chain-of-Thought, Tree-of-Thoughts, ReAct, Self-Correction, Graph of Debates, Program-Aided LLMs) for multi-step problem-solving in complex agent tasks.
- license
- MIT
- compatibility
- opencode
- metadata
- {"version":"1.0.0","domain":"agent","role":"implementation","scope":"implementation","output-format":"code","triggers":"reasoning techniques, chain of thought, tree of thoughts, ReAct, self-correction, program-aided LLMs, how do i improve agent reasoning, GoD","related-skills":"prompt-chaining, reflection-loop, planning-patterns, multi-agent-collaboration","archetypes":"tactical, orchestration, generation","anti_triggers":["simple lookup","one-liner answer","quick fact","trivial yes/no","brainstorming first pass"],"response_profile":{"verbosity":"medium","directive_strength":"high","abstraction_level":"operational"}}
# Advanced Reasoning Techniques
Implements a suite of advanced reasoning methodologies that make an AI agent's internal thought process explicit, enabling structured multi-step problem-solving. This skill equips the model with Chain-of-Thought decomposition, Tree-of-Thoughts exploration, ReAct action loops, Self-Correction refinement, Graph of Debates collaboration, and Program-Aided Language Model execution — each applied to complex tasks requiring deeper analysis than a single-pass LLM response can provide.
## TL;DR Checklist
- [ ] Choose the right reasoning technique based on task complexity (CoT → ToT → ReAct → GoD)
- [ ] Allocate sufficient "thinking budget" per the Scaling Inference Law — more compute yields better results even from smaller models
- [ ] Make all intermediate reasoning steps explicit; never skip from problem to answer
- [ ] Interleave reasoning with external tool use (ReAct) when real-world data is needed
- [ ] Self-correct every output: draft → review against requirements → revise → final
- [ ] Offload deterministic computation (math, code execution) to PAL for accuracy
- [ ] Use GoD for high-stakes decisions requiring bias mitigation and consensus
---
## When to Use
Use this skill when:
- A problem requires **multi-step logical inference** that cannot be solved in a single pass (complex QA, math proofs, code debugging)
- The task involves **exploring multiple solution paths** before committing to an answer (strategic planning, architecture design)
- The agent must **interleave reasoning with tool use** — query databases, search the web, execute code, call APIs (ReAct paradigm)
- Output quality is critical and requires **iterative self-refinement** before final delivery (code generation, legal analysis, medical diagnosis support)
- A decision involves **significant ambiguity or bias risk**, requiring multiple perspectives to converge on a robust answer (Graph of Debates)
- The problem involves **deterministic computation** (arithmetic, data manipulation, algorithmic verification) where LLMs are unreliable
---
## When NOT to Use
Avoid this skill for:
- Simple lookup or single-step questions where direct answers suffice (e.g., "What is the capital of France?")
- Real-time latency-critical responses where thinking budget adds unacceptable delay
- Tasks with no logical decomposition value — trivial yes/no or one-line factual queries
- When you only need a creative draft without accuracy verification (brainstorming, copywriting first pass)
---
## Core Workflow
1. **Classify Task Complexity** — Determine whether the task needs basic CoT (linear steps), ToT (branching exploration), ReAct (tool-interleaved), or GoD (multi-agent debate). Apply the Scaling Inference Law: a smaller model with more thinking time often outperforms a large model with minimal reasoning. **Checkpoint:** Confirm the chosen technique matches task complexity before proceeding.
2. **Decompose Into Reasoning Steps** — Break the problem into a sequence of explicit intermediate steps (CoT) or generate multiple candidate reasoning paths at each branching point (ToT). Document each step's purpose and expected output. **Checkpoint:** Every decomposition step should be independently verifiable against the original requirements.
3. **Execute Reasoning With Appropriate Depth** — Run the selected technique: produce a thought-action-observation loop for ReAct, explore top-k branches with evaluation scoring for ToT, or generate candidate arguments for GoD nodes. Ensure each reasoning pass produces observable, checkable intermediate results. **Checkpoint:** All intermediate outputs are captured and can be audited.
4. **Self-Correction Pass** — Review every generated answer against the original requirements: accuracy (factual correctness), completeness (all aspects addressed), clarity (readable and concise), and tone alignment. Identify discrepancies, propose specific improvements, and generate a revised version. **Checkpoint:** The revised content addresses all identified weaknesses from step 3.
5. **Offload Deterministic Computation** — For any arithmetic, code execution, or data transformation within the reasoning chain, delegate to PAL: generate executable Python, run it in a sandboxed environment, and use the returned results in subsequent steps. **Checkpoint:** Code execution output matches the expected computation result; validate before incorporating into final answer.
6. **Synthesize Final Output** — Combine all validated intermediate results into a structured final answer with citations where applicable. For GoD, identify the most robust argument cluster based on verifiable knowledge or consensus strength. **Checkpoint:** Final output is coherent, complete, and traceable to explicit reasoning steps.
---
## Implementation Patterns / Reference Guide
### Pattern 1: Chain-of-Thought (CoT) Decomposition
Chain-of-Thought prompting guides the model through a step-by-step internal monologue before producing an answer. This transforms a single difficult problem into a sequence of simpler, verifiable sub-steps. Implement CoT by defining a persona, specifying the number and nature of reasoning steps, and capturing both the thought process and final answer.
```python
from typing import Any
def build_cot_prompt(query: str, persona: str, step_count: int = 5) -> str:
"""Build a Chain-of-Thought prompt with structured reasoning steps.
Args:
query: The user's question or problem to solve.
persona: The role/identity the model should adopt.
step_count: Number of explicit reasoning steps (default 5).
Returns:
A formatted prompt that enforces step-by-step reasoning.
"""
# Define step templates based on common reasoning patterns
step_templates = [
"Analyze the Query", # Understand requirements
"Formulate Approach", # Plan the solution strategy
"Execute Reasoning Step", # Perform intermediate work
"Validate Intermediate Result",# Check correctness so far
"Synthesize Final Answer", # Produce polished output
]
steps_text = ""
for i, step in enumerate(step_templates[:step_count], 1):
steps_text += f"{i}. **{step}:** Describe what you should do at this stage.\n"
prompt = f"""You are an {persona}. Your goal is to answer the user's question
comprehensively and accurately by thinking step-by-step.
Here's the process you must follow:
{steps_text}
**User Query:** "{query}"
**Agent's Thought Process (Internal CoT Output):**
"""
return prompt
def execute_cot_reasoning(
thought_process: list[str],
query: str,
) -> dict[str, Any]:
"""Validate a Chain-of-Thought reasoning trace.
Args:
thought_process: List of intermediate reasoning steps produced by the model.
query: The original user query for reference.
Returns:
Dict with 'valid' boolean, 'step_count', and 'gaps' found during review.
"""
gaps: list[str] = []
# Check 1: At least one thought step exists
if len(thought_process) < 2:
gaps.append("Insufficient reasoning depth — expected at least 2 steps")
# Check 2: Each step references the query or previous step
for idx, step in enumerate(thought_process):
if idx == 0 and not any(kw in step.lower() for kw in ["query", "question", "user"]):
gaps.append(f"Step 1 does not reference the original query")
# Check 3: Final step should lead to a conclusion
last_step = thought_process[-1].lower()
if not any(kw in last_step for kw in ["conclusion", "final", "answer", "therefore", "result"]):
gaps.append("Final step does not produce a clear conclusion")
return {
"valid": len(gaps) == 0,
"step_count": len(thought_process),
"gaps": gaps,
}
```
**BAD — Direct answer without reasoning trace:**
```
Question: What is 15% tip on $84.50?
Answer: $12.68
```
❌ No reasoning visible. Cannot verify correctness. Single-pass hallucination risk high.
**GOOD — CoT with explicit intermediate computation:**
```
Question: What is 15% tip on $84.50?
**Agent's Thought Process:**
Thought 1 (Analyze): Need to calculate 15% of $84.50 for the tip amount.
Thought 2 (Plan): Convert percentage to decimal (0.15), multiply by base amount.
Thought 3 (Compute): 84.50 * 0.15 = 12.675
Thought 4 (Round): Round to nearest cent: $12.68
Thought 5 (Validate): Check — 10% of 84.50 is 8.45, 5% is 4.225, sum = 12.675 → rounds to 12.68. Correct.
**Final Answer:** The tip is $12.68
```
✅ Each step verifiable. Rounding logic explicit. Cross-validation included.
---
### Pattern 2: Tree-of-Thoughts (ToT) Exploration
Tree-of-Thoughts extends CoT by branching at each reasoning step into multiple candidate thoughts, evaluating each branch before committing. This enables backtracking and exploration of alternative strategies — critical for tasks where the first obvious path may be suboptimal.
```python
from typing import Any
class ThoughtNode:
"""A single node in a Tree-of-Thoughts reasoning tree.
Attributes:
thought: The reasoning content at this node.
score: Evaluation score (0.0-1.0) of this thought's promise.
children: List of child nodes generated from this thought.
parent: Reference to the parent ThoughtNode, or None for root.
"""
def __init__(self, thought: str, parent: "ThoughtNode | None" = None) -> None:
self.thought: str = thought
self.score: float = 0.0
self.children: list["ThoughtNode"] = []
self.parent: ThoughtNode | None = parent
def add_child(self, child: "ThoughtNode") -> None:
"""Add a child node and link parent reference."""
child.parent = self
self.children.append(child)
def to_path(self) -> list[str]:
"""Trace this node's ancestry back to root as a complete reasoning path."""
path: list[str] = []
node: ThoughtNode | None = self
while node is not None:
path.append(node.thought)
node = node.parent
return list(reversed(path))
class TreeOfThoughts:
"""Tree-of-Thoughts reasoning engine for exploring multiple solution paths.
Implements breadth-first exploration with evaluation and pruning at each depth level.
"""
def __init__(
self,
problem: str,
branches_per_step: int = 3,
max_depth: int = 4,
) -> None:
self.problem: str = problem
self.branches_per_step: int = branches_per_step
self.max_depth: int = max_depth
self.root: ThoughtNode | None = None
def generate_candidates(
self,
parent_node: ThoughtNode,
depth: int,
) -> list[ThoughtNode]:
"""Generate candidate thoughts branching from a parent node.
In production, this would call an LLM with the problem context plus
the parent's thought. Here we demonstrate the structure.
Args:
parent_node: The ThoughtNode to branch from.
depth: Current depth in the tree.
Returns:
List of new ThoughtNode candidates.
"""
if depth >= self.max_depth:
return []
# Production: call LLM with prompt like:
# "Given problem '{self.problem}' and parent thought: {parent_node.thought}
# Generate {self.branches_per_step} candidate next thoughts."
candidates: list[ThoughtNode] = []
for i in range(self.branches_per_step):
child = ThoughtNode(
thought=f"[Branch {i+1}] Consider an alternative approach...",
parent=parent_node,
)
candidates.append(child)
return candidates
def evaluate_thought(
self,
node: ThoughtNode,
depth: int,
) -> float:
"""Score a thought's promise of leading to a correct solution.
Production evaluation uses heuristics or an LLM judge that considers:
- Logical coherence with parent and problem statement
- Diversity from sibling thoughts
- Alignment with known constraints
- Progress toward solvable sub-problems
Args:
node: The ThoughtNode to evaluate.
depth: Current tree depth.
Returns:
Score between 0.0 (dead end) and 1.0 (highly promising).
"""
# Production: implement real evaluation heuristics
score = 0.5 # Placeholder — replace with actual evaluation logic
return score
def solve(self) -> list[str] | None:
"""Execute the full Tree-of-Thoughts reasoning process.
Returns:
The best reasoning path as a list of thought strings, or None if no path found.
"""
self.root = ThoughtNode(thought=f"Problem: {self.problem}")
# BFS-level exploration
current_level: list[ThoughtNode] = [self.root]
for depth in range(1, self.max_depth + 1):
next_level: list[ThoughtNode] = []
for node in current_level:
candidates = self.generate_candidates(node, depth)
for candidate in candidates:
score = self.evaluate_thought(candidate, depth)
candidate.score = score
node.add_child(candidate)
next_level.append(candidate)
if not next_level:
break
# Prune: keep only top-k branches at each level
next_level.sort(key=lambda n: n.score, reverse=True)
current_level = next_level[: self.branches_per_step]
# Find best leaf and trace its path
if not current_level:
return None
best_node = max(current_level, key=lambda n: n.score)
return best_node.to_path()
```
**BAD — Linear CoT on a problem requiring backtracking:**
```
Problem: Plan a 3-day trip to Tokyo on $1500 budget.
→ Day 1: Visit Shibuya, Shinjuku, Akihabara (assumes all fits in one day)
→ Day 2: Visit Asakusa, Ueno, TeamLab (assumes no travel time)
→ Day 3: Day trip to Nikko (misses Tokyo attractions entirely)
```
❌ No exploration of alternatives. No budget verification at each step. One path only.
**GOOD — ToT with branching and pruning:**
```
Problem: Plan a 3-day trip to Tokyo on $1500 budget.
Branch A (Geographic clustering): Group by neighborhoods → score: 0.82
→ Sub-branch A1: Day 1 (West Tokyo), Day 2 (East Tokyo), Day 3 (Day trips)
→ Budget check: hotels $600, food $240, transit $60, activities $200 = $1100 ✓
Branch B (Thematic clustering): Group by interest type → score: 0.71
→ Sub-branch B1: Culture Day, Food Day, Tech/Shopping Day
→ Budget check: hotels $600, food $300, transit $80, activities $250 = $1230 ✓
Branch C (Temporal optimization): Morning/evening split → score: 0.65
→ Higher complexity, marginal benefit over A or B
Decision: Follow Branch A → geographic clustering with budget buffer ($400 remaining)
```
✅ Explores 3 distinct strategies. Scores each objectively. Validates constraints. Selects best path with justification.
---
### Pattern 3: ReAct (Reason + Act) Loop
ReAct interleaves reasoning thoughts with concrete tool actions, forming a Thought → Action → Observation cycle. This enables agents to dynamically gather information, verify assumptions, and adapt plans based on real-world feedback — essential for research, debugging, and any task requiring external data.
```python
from typing import Any
class ReActStep:
"""A single step in the ReAct reasoning loop.
Attributes:
step_number: Sequential step index (1-based).
thought: The agent's internal reasoning at this step.
action_name: Name of the tool/action to execute.
action_input: Arguments passed to the action.
observation: Result returned from the action execution (None if not yet executed).
"""
def __init__(self, step_number: int) -> None:
self.step_number: int = step_number
self.thought: str = ""
self.action_name: str = ""
self.action_input: dict[str, Any] = {}
self.observation: str | None = None
def run_react_loop(
goal: str,
available_tools: dict[str, callable],
max_steps: int = 10,
) -> dict[str, Any]:
"""Execute a ReAct reasoning loop with tool-interleaved action.
Args:
goal: The task the agent must accomplish.
available_tools: Mapping of tool names to executable functions.
max_steps: Maximum number of Thought-Action-Observation cycles.
Returns:
Dict with 'final_answer', 'steps' (list of ReActStep), and 'terminated_early' bool.
"""
steps: list[ReActStep] = []
current_step_idx: int = 1
terminated_early: bool = False
while current_step_idx <= max_steps:
step = ReActStep(step_number=current_step_idx)
# --- THOUGHT Phase: Reason about what to do next ---
all_obs = [s.observation for s in steps if s.observation is not None]
context = f"Goal: {goal}\nPrevious observations:\n" + "\n".join(all_obs)
step.thought = _generate_thought(context, available_tools, current_step_idx)
# Check if the thought indicates a "finish" action
if _is_finish_thought(step.thought):
step.action_name = "finish"
step.action_input = {"answer": _extract_final_answer(step.thought)}
step.observation = None
steps.append(step)
terminated_early = True
break
# --- ACTION Phase: Select and execute a tool ---
action_name, action_input = _select_action(
step.thought, available_tools, current_step_idx
)
step.action_name = action_name
step.action_input = action_input
# Execute the tool (production: use proper sandboxed execution)
if action_name in available_tools:
try:
result = available_tools[action_name](**action_input)
step.observation = str(result)
except Exception as e:
step.observation = f"ERROR: {type(e).__name__}: {e}"
else:
Ver en GitHub