Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Prompt Engineering Techniques

Introduction

Welcome to our second lesson in the "Interacting with LLMs" module. In the previous lesson, you successfully made your first API call to an LLM, learning how to send a prompt and receive a response programmatically. We covered the mechanics of authentication, structuring the API call, and using basic parameters like model and messages.

Today, we shift our focus from the how to the what. This lesson is dedicated to prompt engineering, the art and science of crafting inputs that guide an LLM to produce the desired output. Mastering this is crucial for building reliable and effective AI applications. We will cover three fundamental techniques that form the bedrock of modern prompting:

  • Zero-shot prompting: Getting the model to perform a task with no prior examples.
  • Few-shot prompting: Providing the model with a handful of examples to steer its behavior.
  • Chain-of-thought (CoT) prompting: Encouraging the model to "think step by step" to solve complex reasoning problems.

By the end of this lesson, you'll be able to apply these techniques to elicit more accurate, formatted, and reasoned responses from the models you work with.

1. What is Prompt Engineering?

At its core, prompt engineering is about effective communication with an AI. Since LLMs are trained on vast amounts of text, they have a general understanding of many topics, but they don't know your specific intent for a given task. A well-crafted prompt provides the necessary context, instructions, and constraints to bridge that gap.

Master AI Prompting: Zero-Shot, Few-Shot & Chain of Thought Explained

Let's start with a clear definition. The following video from Prof. Ryan Ahmed introduces prompt engineering and illustrates why the quality of your prompt directly impacts the quality of the output.

Watch the first 2 minutes and 13 seconds of the video. It defines prompt engineering as the 'art of communicating' with a generative AI model and shows a simple example of how a prompt can be iteratively improved.

As you saw in the video, a vague prompt like "recommend a book" yields a generic answer, while a specific prompt like "recommend a historical fiction book set in World War II" produces a much more useful result. This is prompt engineering in its simplest form.

2. Zero-Shot Prompting: The Direct Approach

Zero-shot prompting is the most basic technique. It involves asking the model to perform a task without providing any examples of how to do it. You are relying entirely on the model's pre-existing knowledge.

In fact, the API call you made in our last lesson was a perfect example of a zero-shot prompt:




# From the previous lesson
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[
    {"role": "user", "content": "What is the difference between an API and an SDK?"}
  ]
)

You didn't provide any examples of what a good explanation looks like; you simply asked the question directly.

Master AI Prompting: Zero-Shot, Few-Shot & Chain of Thought Explained

This next segment from the same video provides a concise explanation of zero-shot prompting.

Watch from 05:11 to 06:33. The video defines zero-shot prompting and gives an example of sentiment classification where the model categorizes a headline without being shown any prior examples.

Zero-shot works well for a wide range of tasks where the instructions are straightforward and fall within the model's general capabilities, such as summarization, translation, and simple question-answering.

3. Few-Shot Prompting: Show, Don't Just Tell

While zero-shot is powerful, sometimes you need to guide the model more explicitly. Few-shot prompting does this by including a few examples (the "shots") of input-output pairs directly in the prompt. This helps the model understand the desired format, style, or logic for the task.

This is particularly useful for:

  • Structured data extraction: Pulling specific information out of unstructured text.
  • Specific formatting: Forcing the output into a required format like JSON or CSV.
  • Niche classification tasks: Where the categories might be unique to your application.
Types of Prompting Techniques Infographic
This infographic provides a great visual comparison of different prompting techniques, including zero-shot and few-shot. Notice how the few-shot example provides a template that the model can follow.

How to Structure a Few-Shot Prompt

You include the examples within the messages list, alternating between user input and the desired assistant output, before you present the final query.

Let's say you want to classify customer feedback into POSITIVE, NEGATIVE, or NEUTRAL. A few-shot prompt would look like this in code:

import os
from openai import OpenAI

client = OpenAI() # Assumes OPENAI_API_KEY is set as an environment variable




# Our new query
new_feedback = "The checkout process was a bit confusing, but the support team helped me out."

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[
    {"role": "system", "content": "You are a sentiment-analysis bot. Classify the user's text as POSITIVE, NEGATIVE, or NEUTRAL."},



    # --- Example 1 ---
    {"role": "user", "content": "I love the new interface! It's so clean and easy to use."},
    {"role": "assistant", "content": "POSITIVE"},



    # --- Example 2 ---
    {"role": "user", "content": "The app keeps crashing whenever I try to upload a photo."},
    {"role": "assistant", "content": "NEGATIVE"},



    # --- Example 3 ---
    {"role": "user", "content": "The latest update was released today."},
    {"role": "assistant", "content": "NEUTRAL"},



    # --- Final Query ---
    {"role": "user", "content": new_feedback}
  ],
  temperature=0.0
)

print(f"Feedback: '{new_feedback}'")
print(f"Sentiment: {response.choices[0].message.content}")




# Expected Output: POSITIVE (or similar, depending on model interpretation of mixed feedback)

By providing examples, you condition the model to respond only with one of the desired labels, making the output more reliable and easier to parse in your application.

Prompt Engineering

For a more detailed look at the structure of few-shot prompts and some considerations, let's turn to Lilian Weng's excellent blog post on prompt engineering.

Read the sections 'Basic Prompting', 'Tips for Example Selection', and 'Tips for Example Ordering'. Pay attention to how few-shot examples are presented and the key takeaway that the choice and order of your examples can significantly impact performance. You don't need to dive deep into the linked research papers, just grasp the high-level concepts.

The key insight from that reading is that high-quality, relevant, and diverse examples are crucial for effective few-shot prompting.

4. Chain-of-Thought (CoT) Prompting: Guiding the Reasoning Process

For complex tasks that require logic, calculation, or multi-step reasoning, even few-shot prompting can fail. The model might try to "guess" the answer in one step and get it wrong. Chain-of-thought (CoT) prompting solves this by explicitly instructing the model to generate a series of intermediate reasoning steps before giving the final answer.

This technique is remarkably effective, especially with more capable models, because it mimics how a human would break down a problem.

There are two main variants of CoT:

A. Few-Shot CoT

Here, you provide examples that not only show the final answer but also the step-by-step reasoning to get there.

Chain-of-Thought Prompting | Prompt Engineering Guide

The Prompt Engineering Guide website provides a crystal-clear example of few-shot CoT for a simple math problem.

Read the first section, 'Chain-of-Thought (CoT) Prompting'. Notice how the prompt includes not just the question and answer, but the rationale: 'Adding all the odd numbers... gives... The answer is...' This teaches the model the process of solving the problem.

B. Zero-Shot CoT

Amazingly, you can often trigger a reasoning chain without providing any examples at all. Simply appending a phrase like "Let's think step by step" to your prompt is often enough to make the model show its work and arrive at a more accurate conclusion.

Chain-of-Thought Prompting | Prompt Engineering Guide

Let's look at another example from the Prompt Engineering Guide that shows the power of this simple trick.

Read the section 'Zero-shot COT Prompting'. Observe how a simple word problem fails with a standard prompt but succeeds when 'Let's think step by step.' is added. This is one of the most powerful and low-cost techniques in prompt engineering.

Let's implement this in Python. We'll use the same word problem from the resource.

Without Zero-Shot CoT:




# Without CoT
response_fail = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[
    {"role": "user", "content": "I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with?"}
  ],
  temperature=0.0
)
print("--- Standard Prompt ---")
print(response_fail.choices[0].message.content) # Often gives an incorrect answer like 11

With Zero-Shot CoT:




# With CoT
response_success = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[
    {"role": "user", "content": "I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with? Let's think step by step."}
  ],
  temperature=0.0
)
print("\n--- Zero-Shot CoT Prompt ---")
print(response_success.choices[0].message.content) # Shows reasoning and gives the correct answer

When you run this code, you'll see a dramatic difference. The second call not only gets the correct answer (10) but also lays out the entire logical sequence, making the result more trustworthy and easier to debug.

Conclusion

You have now learned and applied three of the most essential prompt engineering techniques. These methods are not mutually exclusive; they are tools in your toolbox to be combined as needed to solve a problem.

Key Takeaways:

  • Prompt Engineering is the practice of designing inputs to effectively steer LLM behavior.
  • Zero-Shot Prompting is the simplest form, relying on the model's general knowledge. Use it for straightforward tasks.
  • Few-Shot Prompting provides examples to guide the model on format, style, or task logic. Use it when you need structured or customized output.
  • Chain-of-Thought (CoT) Prompting encourages step-by-step reasoning, dramatically improving performance on complex logical or mathematical tasks. The "Let's think step by step" trick is a powerful and easy-to-implement form of Zero-Shot CoT.

Preview of the Next Lesson:

Now that you have more control over the content of your prompts, the next step is to gain finer control over the generation process itself. In the next lesson, we will dive deeper into sampling parameters, focusing on explaining the effect of temperature and other key settings on the model's output, allowing you to tune the trade-off between creativity and predictability.

Can't find a good explanation? Sign up and we'll make it for you

Sign up