Hello! Welcome back to our course on building AI agents.
In our last lesson, you learned the fundamental mechanics of communicating with a Large Language Model programmatically. You set up the OpenAI API, wrote Python code to send a prompt, and parsed the model's response. You now have the power to "call" an LLM from your own applications.
Today, we'll focus on what to say in those calls. The quality of an LLM's output is dramatically influenced by the quality of its input. This lesson is about "prompt engineering"—the art and science of crafting inputs that guide the model to produce the exact results you need. We'll cover three foundational techniques: zero-shot, few-shot, and chain-of-thought prompting. Mastering these is the first step toward building an agent that can reason and solve problems effectively.
The Craft of Prompt Engineering
Think of prompting as a specialized form of communication. Just asking a simple question might get you a simple answer, but to unlock the model's full potential for complex tasks, you need to provide more structure and context. As a software engineer, you can think of it as designing the input payload for a very powerful, but sometimes unpredictable, function.
A well-designed prompt often contains several key components: a role or context, a clear instruction, the data to be processed, and an indicator of the desired output format.
To get into this mindset, the following video offers an excellent checklist for designing effective prompts.
Prompt Engineering 2025 Full course | Prompt engineering course | ChatGPT Prompts
This video from Great Learning provides a practical checklist for what to include in a prompt to get better results. Thinking in terms of a checklist will be a natural fit for your engineering background.
Watch from 17:12 to 22:47. Pay attention to the different elements like defining a role, specifying the audience, giving context, and providing examples. You don't need to memorize them all, but absorb the general principle of being specific and providing rich context.
Now, let's dive into the core techniques.
Technique 1: Zero-Shot Prompting
Zero-shot prompting is the simplest form of interaction. You ask the model to perform a task directly, without providing any prior examples of how to do it. The model relies solely on its pre-trained knowledge to understand and answer your request.
This is what you did in the previous lesson:"What is the capital of France?"
For many common tasks, this is sufficient.
- Task: Sentiment classification
- Prompt:
Classify the following review as positive, negative, or neutral: "The service was a bit slow, but the food was absolutely incredible!"
The model can handle this because "sentiment classification" is a concept it has seen many times during its training. However, for more nuanced or specialized tasks, zero-shot prompting can be unreliable.
Technique 2: Few-Shot Prompting
Few-shot prompting significantly improves performance by providing the model with a few examples (the "shots") of the task within the prompt itself. This technique, also known as in-context learning, doesn't update the model's parameters; instead, it guides the model's attention to the specific pattern you want it to follow for the current request.
Let's revisit our sentiment analysis task. Imagine you want the model to classify reviews into POSITIVE, NEGATIVE, or MIXED. The "mixed" category can be tricky. A few-shot prompt makes it much clearer.
- Task: Nuanced sentiment classification
- Prompt:
Review: "I loved the movie, it was fantastic!" Sentiment: POSITIVE Review: "A complete waste of time. I was bored from start to finish." Sentiment: NEGATIVE Review: "The acting was superb but the plot was a bit confusing." Sentiment: MIXED Review: "The service was a bit slow, but the food was absolutely incredible!" Sentiment:
By showing it an example of what you consider MIXED, you've guided the model to produce a more accurate classification for your new, ambiguous review.
To get a better handle on these two fundamental approaches, the following short video provides clear, side-by-side examples.
Master AI Prompting: Zero-Shot, Few-Shot & Chain of Thought Explained
In this video, Prof. Ryan Ahmed gives a concise and clear explanation of both zero-shot and few-shot prompting.
Watch from 05:11 to 07:34. This will solidify your understanding of the difference between prompting with and without examples.
For a more in-depth exploration, the following article provides excellent detail on how to design few-shot prompts, when to use them, and common challenges.
Few Shot Prompting: Improving AI Model Performance
This article from Cognativ provides a thorough breakdown of few-shot prompting, framed for a technical audience. It includes a useful comparison table and discusses best practices.
Read the first three sections, starting from the introduction up to and including 'Designing Effective Few Shot Prompts'. Pay special attention to the comparison table contrasting zero-shot, few-shot, and fine-tuning, as well as the 'Generic Structure for Few Shot Prompts'.
Technique 3: Chain-of-Thought (CoT) Prompting
For problems that require multiple steps of reasoning (like math word problems or logical puzzles), even few-shot prompting can fail. The model might try to jump directly to the answer and make a mistake.
Chain-of-Thought (CoT) prompting addresses this by instructing the model to "think step by step." By breaking the problem down into a sequence of intermediate reasoning steps, the model is far more likely to arrive at the correct final answer.
There are two main ways to trigger a chain of thought:
-
Zero-shot CoT: Simply append a phrase like
"Let's think step by step."to your prompt. This simple addition is often enough to encourage the model to show its work. -
Few-shot CoT: Provide examples that not only show the final answer but also include the step-by-step reasoning to get there. This is a powerful combination of few-shot and CoT prompting.
Let's see this in action.
-
Standard Prompt (likely to fail):
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
A: The answer is 11. -
Few-Shot CoT Prompt (more reliable):
Q: A juggler has 6 balls. He throws 2 and drops 1. He then buys a new set of 4 balls. How many does he have?
A: The juggler starts with 6 balls. He loses 2-1=1 ball. So he has 6-1=5. He buys 4 more, so he has 5+4=9 balls.Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
A:
The model now understands that the expected output is not just the final number, but the entire reasoning process.
The following video explains this concept clearly.
Master AI Prompting: Zero-Shot, Few-Shot & Chain of Thought Explained
This final segment from Prof. Ryan Ahmed's video explains Chain of Thought prompting with a great example that shows why it's so effective for reasoning tasks.
Watch from 07:34 to 10:54. Notice how providing an example with reasoning steps enables the model to correctly solve a new problem.
This image provides a fantastic visual summary, comparing the different prompting techniques we've discussed.

To go deeper, this article provides a comprehensive look at CoT, including code examples that we will adapt in the next section.
Chain of Thought Prompting Explained (with examples)
This Codecademy article is dedicated to Chain of Thought prompting. It clearly defines the different types (Zero-shot CoT and Few-shot CoT) and explains their benefits.
Read the sections 'What is chain of thought prompting?,' 'Zero-shot chain-of-thought (Zero-shot CoT) prompting,' and 'Few-shot chain-of-thought (Few-shot CoT) prompting.' This will give you a solid theoretical and practical understanding of this powerful technique.
Putting It into Practice with Python
Now let's apply these concepts to the Python code from our last lesson. The key is to structure the messages list to reflect these new prompting patterns.
Implementing Few-Shot Prompting
To create a few-shot prompt, you add examples to the messages list by alternating between the "user" role (for the example input) and the "assistant" role (for the example output).
import os
from openai import OpenAI
client = OpenAI() # Assumes OPENAI_API_KEY is set as an environment variable
# Our new review to classify
new_review = "I'm not sure how I feel about the ending, but the visuals were stunning."
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
# This system message sets the overall context
{"role": "system", "content": "You are a sentiment analysis expert. Classify reviews into POSITIVE, NEGATIVE, or MIXED."},
# Example 1
{"role": "user", "content": "Review: 'I loved the movie, it was fantastic!' Sentiment:"},
{"role": "assistant", "content": "POSITIVE"},
# Example 2
{"role": "user", "content": "Review: 'A complete waste of time. I was bored from start to finish.' Sentiment:"},
{"role": "assistant", "content": "NEGATIVE"},
# Example 3
{"role": "user", "content": "Review: 'The acting was superb but the plot was a bit confusing.' Sentiment:"},
{"role": "assistant", "content": "MIXED"},
# Our actual prompt for the new review
{"role": "user", "content": f"Review: '{new_review}' Sentiment:"}
],
temperature=0 # Low temperature for classification tasks
)
print(f"Review: '{new_review}'")
print(f"Sentiment: {response.choices[0].message.content}")
except Exception as e:
print(f"An error occurred: {e}")
Run this code. You'll see that by providing examples, you get a much more reliable and well-formatted answer.
Implementing Zero-Shot Chain-of-Thought
Implementing zero-shot CoT is even simpler. You just add the magic phrase to your user prompt.
import os
from openai import OpenAI
client = OpenAI()
word_problem = "If a group of 5 friends wants to share 25 cookies equally, but one friend is allergic and can't have any, how many cookies does each of the other friends get? Let's think step by step."
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that reasons through problems."},
{"role": "user", "content": word_problem}
],
temperature=0.7
)
print(f"Problem: {word_problem}\n")
print(f"Solution:\n{response.choices[0].message.content}")
except Exception as e:
print(f"An error occurred: {e}")
When you run this, you will see the model output its reasoning process before giving the final answer, reducing the chance of a simple calculation error.
Conclusion
You've now moved beyond simple questions and learned how to engineer prompts that guide an LLM toward more accurate, nuanced, and well-reasoned outputs. This is a critical skill for building reliable AI agents.
Here are your key takeaways:
- Prompt Engineering is the practice of carefully designing inputs to control an LLM's output.
- Zero-Shot Prompting involves asking a direct question without examples and works best for common, unambiguous tasks.
- Few-Shot Prompting provides examples within the prompt to guide the model on specific formats or patterns, dramatically improving accuracy for specialized tasks.
- Chain-of-Thought (CoT) Prompting instructs the model to break down problems into steps, significantly boosting its performance on tasks requiring logic and reasoning.
In our next lesson, we'll tackle a crucial next step. While we can now get better textual responses, an agent often needs data in a reliable, machine-readable format like JSON. We will learn how to implement structured output parsing, ensuring the LLM's response can be directly used by other parts of your program.