Chain of Thought (CoT) is a prompting technique that encourages the LLM to break down complex reasoning tasks into a series of intermediate steps. By explicitly showing the model how to "think step by step," it dramatically improves performance on tasks requiring multi-step reasoning like math problems, logical puzzles, and complex decision-making.
Implementation
Use explicit prompting with phrases like "Let's think step by step" or "Let's solve this problem by breaking it down."
Encourage detailed reasoning by asking the model to explain its thought process for each step.
Structure matters - use numbered steps or clear paragraph breaks to help the model organize its thoughts.
For complex problems, consider combining with few-shot examples that demonstrate the desired reasoning pattern.
Request verification by asking the model to check its work after reaching a conclusion.
Any task where the path to the answer matters as much as the answer itself
Build This Pattern
Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.
Build a chain-of-thought prompting system for complex reasoning.
ROLE: You are a reasoning system that guides LLMs through step-by-step thinking before providing final answers.
CONSTRAINTS:
- Support both zero-shot CoT (trigger phrase) and few-shot CoT (2-3 examples)
- Output must have clear Reasoning section (numbered steps) and Answer section
- Reasoning-to-answer ratio must be at least 2:1 for complex problems
- Maximum 10 reasoning steps per problem; longer reasoning indicates decomposition needed
- Configurable trigger phrase for zero-shot mode
TOOL CALLING:
- Use function calling for: build_cot_prompt(problem, mode?, examples?), extract_reasoning(output), extract_answer(output), validate_reasoning(reasoning, answer)
- Each tool returns structured JSON with reasoning data and metadata
STRUCTURED OUTPUT:
- CoT prompt must return JSON: { problem: string, mode: 'zero_shot' | 'few_shot', trigger_phrase?: string, examples?: [{ problem: string, reasoning: string, answer: string }], full_prompt: string }
- Extracted reasoning must return JSON: { steps: [{ step: number, content: string }], total_steps: number, reasoning_length: number, has_numerical_validation: boolean }
- Extracted answer must return JSON: { answer: string, confidence: number, reasoning_steps_used: number, answer_type: 'text' | 'numeric' | 'multiple_choice' }
- Validation result must return JSON: { valid: boolean, reasoning_supports_answer: boolean, missing_steps?: string[], inconsistencies?: string[] }
CHAIN OF THOUGHT:
- Prompt construction: analyze problem → select mode → build prompt with trigger/examples → format
- Reasoning extraction: parse output → identify reasoning section → extract numbered steps → validate structure
- Answer extraction: locate answer section → parse answer → validate against reasoning → return
- Validation: check reasoning supports answer → identify gaps → verify numerical consistency
FEW-SHOT EXAMPLES:
Problem: 'If a train travels 60 mph for 2.5 hours, then 80 mph for 1.5 hours, what is the total distance?'
Reasoning: [
{ step: 1, content: 'First segment: 60 mph × 2.5 hours = 150 miles' },
{ step: 2, content: 'Second segment: 80 mph × 1.5 hours = 120 miles' },
{ step: 3, content: 'Total distance: 150 + 120 = 270 miles' }
]
Answer: { answer: '270 miles', confidence: 0.98, reasoning_steps_used: 3, answer_type: 'numeric' }
EVALUATION CRITERIA:
- Reasoning clarity: percentage of reasoning steps that are clear and logical
- Answer accuracy: percentage of answers that are correct based on reasoning
- Step completeness: percentage of necessary reasoning steps that are included
- Format compliance: percentage of outputs that follow Reasoning/Answer structure
The system should: 1) Implement prompt builder that takes reasoning problem and constructs prompt with explicit reasoning instructions, 2) Output parser extracts Reasoning section (numbered steps) and Answer section using regex or LLM-based extraction, 3) Support both zero-shot CoT (prepend trigger phrase) and few-shot CoT (dynamically insert 2-3 examples), 4) Include config flag for which mode to use and examples library for few-shot mode, 5) If output lacks clear Reasoning or Answer sections, use second LLM call to reformat, 6) If answer extraction regex fails, fall back to LLM-based extraction, 7) Handle cases where LLM answers before reasoning by post-processing reorder, 8) Handle math problems needing numeric validation against reasoning, 9) Support multi-line reasoning steps, 10) Detect and filter off-topic reasoning steps.