Chain-of-Thought Prompting: Complete Guide and Examples
Master chain-of-thought prompting — step-by-step reasoning in prompts — best for math and logic tasks
Chain-of-Thought Prompting: Complete Guide and Examples
Master chain-of-thought prompting — step-by-step reasoning in prompts — best for math and logic tasks
Chain-of-Thought Prompting: Complete Guide What is Chain-of-Thought Prompting? Chain-of-Thought Prompting is a prompting technique that involves step-by-step reasoning in prompts. It is particularly effective for math and logic tasks. When to Use
Chain-of-Thought Prompting: Complete Guide
What is Chain-of-Thought Prompting?
Chain-of-Thought Prompting is a prompting technique that involves step-by-step reasoning in prompts. It is particularly effective for math and logic tasks.
When to Use Chain-of-Thought Prompting
Use this technique when:
How It Works
The core idea behind Chain-of-Thought Prompting:
Basic Example
python
from openai import OpenAIclient = OpenAI()
def chain_of_thought_prompting_prompt(task: str, context: str = "") -> str:
"""Apply Chain-of-Thought Prompting technique."""
# Chain-of-Thought Prompting prompt structure
system = """You are an expert AI assistant.
Apply systematic reasoning to every task.
Be precise, accurate, and well-structured."""
# Core prompt for Chain-of-Thought Prompting
prompt = f"""Task: {task}
{"Context: " + context if context else ""}
Please step-by-step reasoning in prompts to complete this task accurately."""
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": system},
{"role": "user", "content": prompt}
],
temperature=0.3,
max_tokens=1500
)
return response.choices[0].message.content
Example usage
result = chain_of_thought_prompting_prompt(
task="Analyze the pros and cons of microservices architecture",
context="For a startup with 5 developers and 1000 users"
)
print(result)
Advanced Implementation
python
from pydantic import BaseModel
from typing import Optionalclass PromptResult(BaseModel):
output: str
technique: str = "Chain-of-Thought Prompting"
confidence: Optional[float] = None
reasoning: Optional[str] = None
class ChainofThoughtPromptingPrompter:
"""Production-ready Chain-of-Thought Prompting implementation."""
def __init__(self, model: str = "gpt-4o"):
self.client = OpenAI()
self.model = model
self.technique = "Chain-of-Thought Prompting"
def run(self, task: str, **kwargs) -> PromptResult:
"""Execute Chain-of-Thought Prompting prompting."""
response = self.client.chat.completions.create(
model=self.model,
messages=self._build_messages(task, **kwargs),
temperature=kwargs.get("temperature", 0.3),
max_tokens=kwargs.get("max_tokens", 2000)
)
content = response.choices[0].message.content
return PromptResult(
output=content,
technique=self.technique
)
def _build_messages(self, task: str, **kwargs) -> list[dict]:
"""Build Chain-of-Thought Prompting specific messages."""
system = f"""You are an expert using {self.technique} to solve tasks.
Apply {desc} systematically.
Format: provide clear, structured responses."""
return [
{"role": "system", "content": system},
{"role": "user", "content": self._build_prompt(task, **kwargs)}
]
def _build_prompt(self, task: str, **kwargs) -> str:
"""Build the specific prompt for Chain-of-Thought Prompting."""
return f"""Using {self.technique}, complete the following task:
Task: {task}
Apply {desc} to arrive at a high-quality answer."""
Usage
prompter = ChainofThoughtPromptingPrompter()
result = prompter.run("Write a Python function to parse JSON safely")
print(result.output)
Real-World Use Cases
Use Case 1: Math and logic tasks
python
Specialized for math and logic tasks
prompter = ChainofThoughtPromptingPrompter(model="gpt-4o")Example: math and logic tasks task
result = prompter.run(
f"Solve this math and logic tasks problem: [your specific problem here]"
)
print(f"Solution: {result.output}")
Use Case 2: Content Generation
python
Apply to content creation
result = prompter.run(
"Write a technical blog post introduction about AI agents",
temperature=0.7, # Higher for creative tasks
max_tokens=500
)
print(result.output)
Comparison with Other Techniques
Common Mistakes
Measuring Effectiveness
python
import json
from statistics import meandef evaluate_prompt_quality(
prompter: ChainofThoughtPromptingPrompter,
test_cases: list[dict],
n_runs: int = 3
) -> dict:
"""Evaluate prompt quality with multiple runs."""
scores = []
for test in test_cases:
run_scores = []
for _ in range(n_runs):
result = prompter.run(test["task"])
# Score based on expected output
score = 1.0 if test.get("expected") in result.output else 0.5
run_scores.append(score)
scores.append(mean(run_scores))
return {
"technique": "Chain-of-Thought Prompting",
"avg_score": mean(scores),
"test_cases": len(test_cases)
}
Resources
相关工具
相关教程
Go beyond basic prompts—master the techniques that actually move model performance
Master Zero-Shot Prompting for better AI outputs
Master meta-prompting — using LLM to generate better prompts — best for prompt optimization