Overview
Chain-of-thought (CoT) prompting asks the LLM to reason through a problem step-by-step before writing code. This technique dramatically improves accuracy for complex logic, algorithms, and multi-step transformations by forcing the model to plan before implementing.
Before vs After CoT
// WITHOUT CoT (often produces buggy code):
"Write a function to merge overlapping intervals."
// WITH CoT (much more reliable):
"Think step by step:
1. What does it mean for intervals to overlap?
2. What sorting is needed first?
3. How do we detect and merge overlapping pairs?
4. What edge cases exist (empty array, single interval)?
Then write the TypeScript implementation with tests."When to Use CoT
- •Algorithm implementation — sorting, graph traversal, dynamic programming
- •Complex business logic — multi-step validation, state machines
- •Data transformation pipelines — ETL, format conversion, aggregation
- •Debugging intricate issues — race conditions, memory leaks
- •Any problem where the LLM might jump to a wrong conclusion
CoT Techniques
Step-by-Step
Add "Think step by step" to your prompt. Simple but effective for most coding problems.
Requirements First
Ask the LLM to list requirements and edge cases before writing any code.
Plan Then Code
"First outline the approach in pseudocode, then implement in TypeScript."