Developing Sophisticated AI: The Role of Chain-of-Thought in AI Reasoning


Have you ever noticed how ChatGPT sometimes goes into a “thinking” mode before responding—taking a few seconds or even minutes to craft a thoughtful, detailed answer?
That kind of step-by-step reasoning is often powered by a concept known as Chain-of-Thought Prompting (CoT).
In this article, we’ll break down:
What Chain-of-Thought Prompting is
Why it’s powerful
How you can implement it in Node.js with OpenAI (or Google Gemini models)
What is Chain of Thought Prompting ?
Chain-of-thought (CoT) prompting is a technique in prompt engineering that enhances the reasoning capabilities of large language models (LLMs) by guiding them to break down complex problems into a series of intermediate steps before providing a final answer.
This approach:
Simulates how humans reason
Reduces mistakes
Improves transparency in how the AI arrives at its answer
Think of it as teaching the AI to “show its work” before revealing the final answer.
How Chain-of-Thought Works
When prompted correctly, the AI will:
Start by understanding the problem.
Think through the reasoning step-by-step.
Produce a clear, final answer.
Example (math problem):
User: A shopkeeper buys 20 pens at ₹5 each and sells them at ₹8 each. What is the total profit?
AI (CoT style):
{ "step": "START", "content": "The user wants me to calculate the total profit from selling pens." }
{ "step": "THINK", "content": "First, I need to find the cost price and selling price." }
{ "step": "THINK", "content": "Cost price = 20 × ₹5 = ₹100." }
{ "step": "THINK", "content": "Selling price = 20 × ₹8 = ₹160." }
{ "step": "THINK", "content": "Profit = ₹160 - ₹100 = ₹60." }
{ "step": "OUTPUT", "content": "Total profit is ₹60." }
Implement this approach using Nodejs
Step 1 - Install Dependency
pnpm add openai dotenv
Step 2 - Set up your env File
OPENAI_API_KEY=your_api_key_here
Note :- make sure you name exact like this
Step 3 - Create a index.js
file
import OpenAI from "openai";
const client = new OpenAI();
async function main() {
const response = await client.chat.completions.create({
model: "gpt-4.1-mini",
messages: [
{
role: "system",
content: `Your chain of thought prompt`,
},
{
role: "user",
content:
"If 5 workers build 2 walls in 6 days, how long will 3 workers take to build 1 wall?",
},
],
});
console.log(response.choices[0].message.content);
}
main()
Example Code
import "dotenv/config";
import { OpenAI } from "openai";
const client = new OpenAI();
async function main() {
try {
const response = await client.chat.completions.create({
model: "gpt-4.1-mini",
messages: [
{
role: "system",
content: `
You are an expert problem solver.
When answering, think step-by-step and explain your reasoning in detail before giving the final answer.
Follow this format strictly:
1. Reasoning (show all intermediate thinking steps clearly)
2. Final Answer should in json format
3. json format consist of "step" and "content" where step can be "START" | "THINK" | "OUTPUT" and content will be string
Json format example -
{
step: START | THINK | OUTPUT
content: string
}
For Example -
USER: A shopkeeper buys 20 pens at ₹5 each and sells them at ₹8 each. What is the total profit?
ASSISTANT: { "step": "START", "content": "The user wants me to calculate the total profit from selling pens." }
ASSISTANT: { "step": "THINK", "content": "First, I need to find the cost price and selling price." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "THINK", "content": "Cost price per pen = ₹5. For 20 pens, cost price = 20 × 5 = ₹100." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "THINK", "content": "Selling price per pen = ₹8. For 20 pens, selling price = 20 × 8 = ₹160." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "THINK", "content": "Profit = Selling price - Cost price = ₹160 - ₹100 = ₹60." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "OUTPUT", "content": "Total profit is ₹60." }
USER: The radius of a circle is 7 cm. Find its area using π = 3.14.
ASSISTANT: { "step": "START", "content": "The user wants the area of a circle given the radius and value of π." }
ASSISTANT: { "step": "THINK", "content": "Formula for area of a circle is π × r²." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "THINK", "content": "Substitute r = 7 cm, π = 3.14." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "THINK", "content": "r² = 7 × 7 = 49." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "THINK", "content": "Area = 3.14 × 49 = 153.86 cm²." }
ASSISTANT: { "step": "EVALUATE", "content": "Alright, going good." }
ASSISTANT: { "step": "OUTPUT", "content": "The area of the circle is 153.86 cm²." }
`,
},
{
role: "user",
content:
"If 5 workers build 2 walls in 6 days, how long will 3 workers take to build 1 wall?",
},
],
});
console.log(response.choices[0].message.content);
} catch (err) {
console.log(err);
}
}
main();
You have to give as many as example you can.
now run
node index.js
Final Thoughts
Chain-of-Thought Prompting is one of the simplest yet most effective ways to enhance AI reasoning. By structuring prompts to include intermediate steps, you’ll get answers that are not only correct but also well-explained.
Subscribe to my newsletter
Read articles from Rahul Aggarwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
