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:
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:
Input: Cat
Output: Animal
Input: Mango
Output: Fruit
Input: Rose
Output:Now the model has understood the pattern. It will say:
FlowerRecap 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:
Question
↓
Thinking
↓
Reasoning
↓
Verification
↓
Final AnswerThis is chain of thought.
Understanding the SYSTEM_PROMPT
The most important line:
You have to analyse the user's input carefullyThen:
breakdown the problem into multiple sub problemsThis 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:
INITIAL
↓
THINK
↓
ANALYSE
↓
THINK
↓
ANALYSE
↓
OUTPUTThis is effectively your own thinking pipeline.
What each step does
Example question: the weather in Patiala.
INITIAL
The user wants weather of Patiala.That is it. Identify what the user wants.
THINK
I need to find the weather.
What do I have to do to get it?This is where planning happens.
ANALYSE
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:
Always output one step at a time.What will the model do? It returns only this:
{
"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:
while (true) {This is loop engineering. All the magic is in this line. Every iteration makes this call:
client.chat.completions.create()Here is the sequence:
User
↓
GPT → INITIAL
↓ Loop
GPT → THINK
↓ Loop
GPT → ANALYSE
↓ Loop
GPT → THINK
↓ Loop
GPT → OUTPUTIn every iteration GPT also receives the previous history.
Where the history is stored
MESSAGES_DBAt the start it holds only the system prompt:
[ 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:
System
↓
User
↓
INITIALNow 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
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:
GPT 4 = Prompt + Loop + Tools = HarnessOn 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
- Provides the system prompt.
- Provides the user prompt.
- Runs the loop.
- Maintains the history.
- Parses the JSON.
- Checks whether the output has arrived.
- If it has not, calls GPT again.
All of this is the harness.
Look at this line:
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:
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?
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
User
↓
INITIAL
↓
THINK
↓
Need Weather Tool
↓
Tool Execute
↓
Weather Data
↓
ANALYSE
↓
OUTPUTThat 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
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
│
▼
breakTypes 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.
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.
Example:
English: Hello
Hindi: Namaste
Now translate:
English: Thank youType 3: Few-Shot Prompting
Give the AI multiple examples so it can understand the expected pattern.
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.
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.
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.
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.
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.
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.
Explain Docker in:
- Maximum 100 words
- Simple English
- Include one practical exampleQuick summary
| Prompt type | Main purpose |
|---|---|
| Zero-Shot | No examples |
| One-Shot | One example |
| Few-Shot | Multiple examples |
| Chain-of-Thought | Step-by-step problem solving |
| Role-Based | Give AI a specific role |
| Instruction-Based | Give clear instructions |
| Contextual | Provide background information |
| Structured Output | Specify the response format |
| Constraint-Based | Set rules and limits |