All notes
2026-11-28 5 min read

My notes from a GenAI class by Piyush Garg.

Lecture 2: Chain of Thought, Loop Engineering and Harness

Prompting recap

First, a recap of prompting.

Recap 1: Zero Shot

You give only the instruction. Example:

text
Translate "Hello" to Hindi.

That is it. No example. The model answers directly.

Recap 2: Few Shot

Along with the instruction, you also give examples:

text
Input: Cat
Output: Animal

Input: Mango
Output: Fruit

Input: Rose
Output:

Now the model has understood the pattern. It will say:

text
Flower

Recap 3: Chain of Thought

This is the interesting one. Here you do not ask the model to answer directly. You tell it:

First think. Then analyse. Then verify. Then answer.

Which means:

text
Question
   ↓
Thinking
   ↓
Reasoning
   ↓
Verification
   ↓
Final Answer

This is chain of thought.

Understanding the SYSTEM_PROMPT

The most important line:

text
You have to analyse the user's input carefully

Then:

text
breakdown the problem into multiple sub problems

This is chain of thought. You are telling the model:

  • Do not answer directly.
  • First break the problem down.
  • Then solve it.

Further down, the prompt defines these steps:

text
INITIAL
   ↓
THINK
   ↓
ANALYSE
   ↓
THINK
   ↓
ANALYSE
   ↓
OUTPUT

This is effectively your own thinking pipeline.

What each step does

Example question: the weather in Patiala.

INITIAL

text
The user wants weather of Patiala.

That is it. Identify what the user wants.

THINK

text
I need to find the weather.

What do I have to do to get it?

This is where planning happens.

ANALYSE

text
Is my plan correct?

Is anything still missing?

This is verification.

After that comes THINK again, then ANALYSE, and finally OUTPUT. Together this is the reasoning chain, which is why it is called chain-of-thought prompting.

But now there is a problem

The prompt says:

text
Always output one step at a time.

What will the model do? It returns only this:

json
{
  "step": "INITIAL",
  "text": "..."
}

Nothing more. So how did THINK happen? Then ANALYSE? Then OUTPUT?

This is where loop engineering begins.

Loop Engineering

Look at this line in the code:

js
while (true) {

This is loop engineering. All the magic is in this line. Every iteration makes this call:

js
client.chat.completions.create()

Here is the sequence:

text
User
 ↓
GPT → INITIAL
 ↓ Loop
GPT → THINK
 ↓ Loop
GPT → ANALYSE
 ↓ Loop
GPT → THINK
 ↓ Loop
GPT → OUTPUT

In every iteration GPT also receives the previous history.

Where the history is stored

text
MESSAGES_DB

At the start it holds only the system prompt:

text
[ SystemPrompt ]

Then the user message is added. Then the assistant's INITIAL step is added. Then the API is called again. The messages are now:

text
System
  ↓
User
  ↓
INITIAL

Now GPT reasons: "Right, the next step should be THINK."

Then THINK is added to the history, then ANALYSE, then THINK again. The conversation keeps growing.

The most important line

js
MESSAGES_DB.push({
  role: "assistant",
  content: rawResult,
});

This line matters a lot. It puts the model's previous thinking into memory.

If you remove it, GPT starts from INITIAL every single time, because it has no memory of what it said before.

So where is the Harness?

This is the most confusing point. The board said:

text
GPT 4 = Prompt + Loop + Tools = Harness

On seeing the code, people ask: "Where is the harness?"

Answer: the whole program is the harness.

A harness is not an API. A harness is:

  • A wrapper
  • A controller
  • An orchestration layer

In simple words: GPT is an employee and the harness is its manager. The manager decides:

  • When to work
  • How many times to work
  • When to stop
  • Which tool to use
  • Which history to pass

What the harness does in this code

  1. Provides the system prompt.
  2. Provides the user prompt.
  3. Runs the loop.
  4. Maintains the history.
  5. Parses the JSON.
  6. Checks whether the output has arrived.
  7. If it has not, calls GPT again.

All of this is the harness.

Look at this line:

js
if (parsedResult.step.toLowerCase() == "output") break;

This is part of the harness too. It says: "Keep calling GPT until OUTPUT arrives." That is the manager's job. GPT does not do it on its own.

One more problem: live data

The question was:

text
What is weather of Patiala?

How would GPT know the weather? It would not. GPT does not use the internet by itself. So what does chain of thought produce?

text
INITIAL  →  User wants weather.
THINK    →  I need the internet to get the weather.
ANALYSE  →  I do not have internet access.
OUTPUT   →  I cannot provide live weather.

Or a tool call, if tools are connected.

If there were a tool

text
User
  ↓
INITIAL
  ↓
THINK
  ↓
Need Weather Tool
  ↓
Tool Execute
  ↓
Weather Data
  ↓
ANALYSE
  ↓
OUTPUT

That is what turns it into an agent.

The three concepts in one line each

Chain of Thought in short

Tell the model:

Do not answer directly. Think step by step.

Loop Engineering in short

Call the model again and again, sending the previous thoughts each time, until the final output arrives.

Harness in short

The wrapper that controls the whole process. It:

  • Gives the prompt
  • Maintains the history
  • Runs the loop
  • Parses the JSON
  • Runs the tools
  • Checks the stop condition

It manages all of this.

The whole code as one flow

text
User
  │
  ▼
System Prompt (CoT Rules)
  │
  ▼
GPT
  │
  ▼
INITIAL
  │
  ▼
Harness (history save)
  │
  ▼
Loop
  │
  ▼
GPT
  │
  ▼
THINK
  │
  ▼
Harness (history save)
  │
  ▼
Loop
  │
  ▼
GPT
  │
  ▼
ANALYSE
  │
  ▼
Harness (history save)
  │
  ▼
Loop
  │
  ▼
GPT
  │
  ▼
OUTPUT
  │
  ▼
break

Types of prompts

A prompt is an instruction or input given to an AI model to get a specific response. Here are some common types.

Type 1: Zero-Shot Prompting

Give the AI a task without providing any examples.

text
Translate this sentence into Hindi:
"How are you?"

Type 2: One-Shot Prompting

Give the AI one example before asking it to perform the task.

text
Example:
English: Hello
Hindi: Namaste

Now translate:
English: Thank you

Type 3: Few-Shot Prompting

Give the AI multiple examples so it can understand the expected pattern.

text
English: Apple → Hindi: Seb
English: Mango → Hindi: Aam
English: Banana → Hindi: Kela

English: Orange → Hindi: ?

Type 4: Chain-of-Thought Prompting

Ask the AI to solve a problem step by step.

text
Solve this mathematical problem.
Explain the main steps and give the final answer.

Type 5: Role-Based Prompting

Give the AI a specific role or identity to guide its response.

text
Act as a senior React developer.
Explain React hooks with practical examples.

Type 6: Instruction-Based Prompting

Give clear instructions about what the AI should do.

text
Explain REST APIs in simple English.
Keep the explanation short and include one example.

Type 7: Contextual Prompting

Provide background information so the AI can give a more relevant answer.

text
I am a React developer learning Node.js.
Explain Express.js from a beginner's perspective.

Type 8: Structured Output Prompting

Ask the AI to return information in a specific format, such as JSON, a table or a list.

text
Explain these programming languages.
Return the result in JSON format with:
name, useCase, and difficulty.

Type 9: Constraint-Based Prompting

Set specific rules or limitations for the response.

text
Explain Docker in:
- Maximum 100 words
- Simple English
- Include one practical example

Quick summary

Prompt typeMain purpose
Zero-ShotNo examples
One-ShotOne example
Few-ShotMultiple examples
Chain-of-ThoughtStep-by-step problem solving
Role-BasedGive AI a specific role
Instruction-BasedGive clear instructions
ContextualProvide background information
Structured OutputSpecify the response format
Constraint-BasedSet rules and limits

Whiteboard

The Eraser board for this lecture