Chain-of-Thought.

Arun ChauhanArun Chauhan
6 min read

What is Chain-of-Thought ( CoT ) ?

Chain of thought (CoT) is a prompt engineering technique that enhances the output of large language models (LLMs), particularly for complex tasks involving multistep reasoning. It facilitates problem-solving by guiding the model through a step-by-step reasoning process by using a coherent series of logical steps.

Why CoT prompting is effective for reasoning ?

  • Improved prompt outputs: CoT prompting improves LLMs' performance on complex reasoning tasks by breaking them down into simpler, logical steps.

  • Transparency and understanding: The generation of intermediate reasoning steps offers transparency into how the model arrives at its conclusions, making the decision-making process more understandable for users.

  • Multistep reasoning: By systematically tackling each component of a problem, CoT prompting often leads to more accurate and reliable answers, particularly in tasks requiring multistep reasoning. Multistep reasoning refers to the ability to perform complex logical operations by breaking them down into smaller, sequential steps. This cognitive skill is essential for solving intricate problems, making decisions and understanding cause-and-effect relationships.

  • Attention to detail: The step-by-step explanation model is akin to teaching methods that encourage understanding through detailed breakdowns, making CoT prompting useful in educational contexts.

  • Diversity: CoT can be applied across a broad range of tasks, including but not limited to, arithmetic reasoning, common sense reasoning and complex problem-solving, demonstrating its flexible utility.

Example: How does chain of thought prompting work for solving polynomial equations?

Input prompt:
Solve the equation: 3 + 4 10 - 4 3. Explain step by step.

CoT reasoning steps:

  1. Start with the expression: 3 + 4 × 10 – 4 × 3.

  2. Apply order of operations (multiplication before addition/subtraction).

    • 4 × 10 = 40

    • 4 × 3 = 12

  3. Substitute results back: 3 + 40 – 12.

  4. Perform addition/subtraction left to right:

    • 3 + 40 = 43

    • 43 – 12 = 31

Final Answer: 31

Without CoT prompting, the model might jump straight to an answer (and risk a mistake). With CoT, it shows the full reasoning chain, making the solution transparent and reliable.

Beginner-friendly guide with JavaScript examples you can run in a Node.js project.

This following code is an implementation of a step-by-step reasoning framework using OpenAI’s API, where the model is forced to "think" in stages (START → THINK → EVALUATE → OUTPUT) before giving the final answer.

1. Imports and Setup

Copy

import 'dotenv/config';
import { OpenAI } from 'openai';

// Make sure you are on Node 18+ or enable ES modules in package.json
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  • dotenv/config loads environment variables (like your OPENAI_API_KEY) from a .env file.

  • OpenAI is the SDK client for interacting with the OpenAI API.

  • Creates an OpenAI client instance that will send API requests.

  • It expects the OPENAI_API_KEY from your environment.

2. The System Prompt (instruction for the model)

Copy

const SYSTEM_PROMPT = `
    You are an AI assistant who works on START, THINK and OUTPUT format.
    For a given user query first think and breakdown the problem into sub problems.
    You should always keep thinking and thinking before giving the actual output.
    Also, before outputing the final result to user you must check once if everything is correct.

    Rules:
    - Strictly follow the output JSON format
    - Always follow the output in sequence that is START, THINK and OUTPUT.
    - Always perform only one step at a time and wait for other step.
    - Alway make sure to do multiple steps of thinking before giving out output.

    Output JSON Format:
    { "step": "START | THINK | OUTPUT", "content": "string" }

    Example:
    User: Can you solve 3 + 4 * 10 - 4 * 3
    ASSISTANT: { "step": "START", "content": "The user wants me to solve 3 + 4 * 10 - 4 * 3 maths problem" } 
    ASSISTANT: { "step": "THINK", "content": "This is typical math problem where we use BODMAS formula for calculation" }  
    ASSISTANT: { "step": "THINK", "content": "Lets breakdown the problem step by step" }  
    ASSISTANT: { "step": "THINK", "content": "As per bodmas, first lets solve all multiplications and divisions" } 
    ASSISTANT: { "step": "THINK", "content": "So, first we need to solve 4 * 10 that is 40" }  
    ASSISTANT: { "step": "THINK", "content": "Great, now the equation looks like 3 + 40 - 4 * 3" }
    ASSISTANT: { "step": "THINK", "content": "Now, I can see one more multiplication to be done that is 4 * 3 = 12" } 
    ASSISTANT: { "step": "THINK", "content": "Great, now the equation looks like 3 + 40 - 12" } 
    ASSISTANT: { "step": "THINK", "content": "As we have done all multiplications lets do the add and subtract" } 
    ASSISTANT: { "step": "THINK", "content": "so, 3 + 40 = 43" } 
    ASSISTANT: { "step": "THINK", "content": "new equations look like 43 - 12 which is 31" } 
    ASSISTANT: { "step": "THINK", "content": "great, all steps are done and final result is 31" }
    ASSISTANT: { "step": "OUTPUT", "content": "3 + 4 * 10 - 4 * 3 = 31" } 
  `;
  • Always respond in JSON format with { "step": "START | THINK | OUTPUT", "content": "..." }.

  • Follow a structured reasoning style:

    • START → Identify the problem.

    • THINK → Break down the reasoning in multiple steps.

    • OUTPUT → Provide the final answer.

  • Always think in several steps before outputting.

3. Initial conversation setup

Copy

const messages = [
    {
      role: 'system',
      content: SYSTEM_PROMPT,
    },
    {
      role: 'user',
      content: 'Write a code in JS to find a prime number as fast as possible',
    },
  ];

The conversation starts with:

  • A system role message (the rules).

  • A user role message (the actual query).

4. The reasoning loop

Copy

while (true) {
  const response = await client.chat.completions.create({
    model: 'gpt-4.1-mini',
    messages: messages,
  });
  • Calls OpenAI’s API repeatedly inside a loop.

  • Each call generates one JSON step of reasoning.

5. Parsing the model’s step

Copy

const rawContent = response.choices[0].message.content;
const parsedContent = JSON.parse(rawContent);
  • The model’s reply is forced into JSON format.

  • The code parses it into a JS object with step and content.

6. Feeding the model’s reasoning back

Copy

 // These api calls are stateless (Chain Of Thought) 
// so we need to pushback AI response to maintain context 
messages.push({
  role: 'assistant',
  content: JSON.stringify(parsedContent),
});
  • Adds the model’s response to the messages history.

This keeps the conversation going — the next API call builds on previous reasoning.

8. Step-based handling

Copy

if (parsedContent.step === 'START') {
  console.log(`🔥`, parsedContent.content);
  continue;
}

if (parsedContent.step === 'THINK') {
  console.log(`\t🧠`, parsedContent.content);
  continue;
}

if (parsedContent.step === 'OUTPUT') {
  console.log(`🤖`, parsedContent.content);
  break;
}
console.log('Done...');
  • If "START" → logs the start message (problem understanding).

  • If "THINK" → logs the reasoning step (🧠).

  • If "OUTPUT" → logs the final result (🤖) and breaks out of the loop.

  • After the loop ends, prints "Done...".

0
Subscribe to my newsletter

Read articles from Arun Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arun Chauhan
Arun Chauhan