Advanced Prompt Techniques: Chain-of-Thought and ReAct

Clawpedia · For Humans

Apply advanced prompting strategies like chain-of-thought reasoning and ReAct for complex problem-solving.

Advanced Prompt Techniques: Chain-of-Thought and ReAct

When simple prompts fall short, advanced techniques like Chain-of-Thought (CoT) and ReAct unlock deeper reasoning capabilities in your OpenClaw agent. These methods transform your agent from a simple Q&A bot into a systematic problem solver.

Chain-of-Thought Prompting

Chain-of-Thought forces the LLM to show its reasoning step-by-step before arriving at a conclusion, dramatically improving accuracy on complex tasks.

Basic CoT


Without CoT:
Q: "A store has 45 apples. They sell 12, receive 30 more, 
    then sell 18. How many are left?"
A: "45" ← Often wrong

With CoT:
Q: Same question + "Think step by step."
A: "Starting: 45 apples
    After selling 12: 45 - 12 = 33
    After receiving 30: 33 + 30 = 63
    After selling 18: 63 - 18 = 45
    Answer: 45 apples" ← Correct with visible reasoning

Implementing CoT in OpenClaw


# config.yaml
agent:
  system_prompt: |
    When solving problems, ALWAYS follow this process:
    
    1. UNDERSTAND: Restate the problem in your own words
    2. PLAN: List the steps needed to solve it
    3. EXECUTE: Work through each step, showing your work
    4. VERIFY: Check your answer for errors
    5. ANSWER: Provide the final answer clearly
    
    Format your thinking in a "Reasoning:" block before 
    giving the final "Answer:" block.

CoT for Code Analysis


Analyze this function for bugs. Think step by step:

function calculateDiscount(price, discount) {

if (discount > 100) return 0;

return price - (price * discount);

}



Reasoning:
1. The function takes price and discount parameters
2. It checks if discount > 100, returning 0 — but what about negative discounts?
3. The calculation `price * discount` treats discount as a decimal...
   but if discount is 50 (meaning 50%), this calculates price * 50, not price * 0.5
4. Missing: discount should be divided by 100

Answer: Bug found — discount is not divided by 100. 
Fix: `return price - (price * discount / 100)`
Also missing: validation for negative discount values.

ReAct: Reasoning + Acting

ReAct combines reasoning with tool usage. The agent thinks about what to do, takes an action, observes the result, and continues reasoning.

The ReAct Loop


┌──────────┐     ┌──────────┐     ┌──────────┐
│  THINK   │ ──→ │   ACT    │ ──→ │ OBSERVE  │
│ (reason) │     │ (use tool)│     │ (result) │
└──────────┘     └──────────┘     └──────────┘
      ↑                                 │
      └─────────────────────────────────┘

ReAct in Practice


User: "Is our API response time acceptable compared to industry standards?"

Think: I need to check our current API response time, then compare 
       it to industry benchmarks.

Act: [check_monitoring] Query our API metrics for the last 24 hours

Observe: Average response time: 340ms, P95: 890ms, P99: 1200ms

Think: Now I need industry benchmarks for comparison. Let me search 
       for current standards.

Act: [web_search] "API response time benchmarks 2025"

Observe: Industry standards suggest < 200ms average for good UX, 
         < 1000ms P95 as acceptable.

Think: Our average (340ms) is above the ideal (200ms), and our P95 
       (890ms) is within acceptable range but close to the limit. 
       P99 (1200ms) exceeds recommendations.

Answer: Your API performance needs improvement:
- Average: 340ms (target: < 200ms) — 70% above ideal
- P95: 890ms (target: < 1000ms) — acceptable but tight  
- P99: 1200ms (target: < 1000ms) — exceeds recommendation

Recommendations:
1. Add response caching for frequent queries
2. Optimize database queries (likely cause of P99 spikes)
3. Consider a CDN for static API responses

Configuring ReAct in OpenClaw


agent:
  reasoning_mode: "react"
  system_prompt: |
    You have access to these tools:
    - [web_search]: Search the internet
    - [run_code]: Execute code snippets
    - [check_calendar]: View calendar events
    - [send_message]: Send messages to platforms
    
    For every complex request, follow the ReAct pattern:
    
    Thought: Reason about what you need to do
    Action: Choose and use a tool
    Observation: Analyze the tool's result
    ... (repeat as needed)
    Final Answer: Provide your conclusion

Self-Consistency: Multiple Reasoning Paths

Generate multiple CoT paths and pick the most common answer:


system_prompt: |
  For important decisions, generate 3 independent analyses:
  
  Analysis A: [approach from perspective 1]
  Analysis B: [approach from perspective 2]  
  Analysis C: [approach from perspective 3]
  
  Consensus: Compare all three analyses.
  If 2+ agree → Use that answer with high confidence
  If all differ → Flag uncertainty and present all options

Tree-of-Thought

For complex planning, explore multiple branches:


Plan a database migration strategy.

Branch 1 (Conservative):
- Blue-green deployment
- Gradual traffic shift
- 2-week timeline
- Risk: Slow, resource-intensive

Branch 2 (Aggressive):
- Direct cutover with rollback plan
- Weekend maintenance window  
- 3-day timeline
- Risk: Downtime if issues arise

Branch 3 (Hybrid):
- Shadow writes to both databases
- Validation period
- 1-week timeline
- Risk: Complexity in dual-write logic

Evaluation: Branch 3 balances speed and safety.
Recommendation: Hybrid approach with shadow writes.

Practical Patterns

The Critic Pattern


system_prompt: |
  After generating any output, switch to critic mode:
  
  [DRAFT]: Your initial response
  [CRITIQUE]: What could be wrong? What did you miss?
  [IMPROVED]: Revised response incorporating the critique

The Expert Panel Pattern


Analyze this architecture decision from three perspectives:

🔒 Security Expert: [analysis of security implications]
⚡ Performance Engineer: [analysis of performance impact]  
💰 Business Analyst: [analysis of cost and ROI]

Synthesis: Combined recommendation considering all perspectives.

The Decomposition Pattern


Break this complex task into subtasks:

Main task: "Migrate our monolith to microservices"

Subtask 1: Identify service boundaries
Subtask 2: Design API contracts
Subtask 3: Set up infrastructure
Subtask 4: Implement data migration
Subtask 5: Test and validate
Subtask 6: Deploy and monitor

Now execute subtask 1: [detailed analysis]

When to Use Which Technique

TechniqueBest ForOverhead
Zero-shotSimple, clear tasksMinimal
Few-shotFormatting, classificationLow
CoTMath, logic, analysisMedium
ReActMulti-step tool usageMedium-High
Self-ConsistencyCritical decisionsHigh

Performance Considerations

Tree-of-ThoughtStrategic planningHigh

Tips for Implementation

Related Articles