How does Prompt Engineering work?

Authors
How does Prompt Engineering work?

Prompt Engineering is the skill of writing clear and well-structured prompts, the instructions we give an AI model, so that the model gives us the exact output we want.

In this blog, we will learn about how Prompt Engineering works. We will also see why we need it, how a prompt shapes the answer of the model, the building blocks of a good prompt, how zero-shot, few-shot, and chain-of-thought prompting work, how system and user prompts work together, and the best practices to follow.

We will cover the following:

  • What is Prompt Engineering?
  • Why do we need Prompt Engineering?
  • How does Prompt Engineering work?
  • The building blocks of a good prompt
  • Zero-shot prompting
  • Few-shot prompting
  • Chain-of-Thought prompting
  • System prompt and user prompt
  • A step-by-step example
  • Best practices for Prompt Engineering

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What is Prompt Engineering?

Prompt Engineering is the skill of writing clear and well-structured prompts so that the AI model gives us the exact output we want.

Consider two people asking the same friend for help. One says "Help me". The other says "Help me fix the flat tire on my bicycle, I have a pump but no patch kit". The second person gets better help because the request is clear and complete. Prompt Engineering is exactly this skill, but for talking to an AI model.

Why do we need Prompt Engineering?

The AI model does not read our mind. It only reads our words.

The model is trained on a huge amount of text, and it predicts the next word based on what we give it. If our input is vague, the model has to guess, and the guess is often wrong.

Let's say we type "Tell me about apple". The model does not know if we mean the fruit, the company, or something else. So, it may pick the wrong one.

Now, suppose we type "Tell me about the apple fruit and its health benefits". The model now knows exactly what we want. The answer becomes accurate and useful.

This is why we need Prompt Engineering. It removes the guessing. It helps the model give us the right answer sooner, instead of going back and forth many times.

How does Prompt Engineering work?

When we send a prompt, the AI model does not "think" like a human. It looks at our words and predicts the most likely next words, one after another, to form a complete response.

The model has read a massive amount of text during its training. From that text, it has learned patterns. When we give it a prompt, it tries to continue our text in the most likely and most relevant way.

The model continues the pattern we start.

So, if we start a clear and detailed pattern, the model continues it in a clear and detailed way. If we start a messy pattern, the model continues it in a messy way.

Prompt Engineering works by shaping that starting pattern on purpose. We give the model enough context, clear instructions, and sometimes a few examples. The model then has a strong pattern to follow, so the response matches what we want.

We can picture the flow as below:

     Clear Prompt                 Vague Prompt
+--------------------+       +--------------------+
| Clear words        |       | Few words,         |
| and context        |       | no context         |
+--------------------+       +--------------------+
          ↓                            ↓
+--------------------+       +--------------------+
| AI Model predicts  |       | AI Model has to    |
| the next words     |       | guess what we mean |
+--------------------+       +--------------------+
          ↓                            ↓
+--------------------+       +--------------------+
| Clear, useful      |       | Random, often      |
| answer             |       | wrong answer       |
+--------------------+       +--------------------+

Here, we can see that the same model gives a very different response based on the prompt. A clear prompt gives the model a strong pattern to continue, so the answer is useful. A vague prompt forces the model to guess, so the answer is often wrong.

Let's understand this with the building blocks of a good prompt.

The building blocks of a good prompt

A strong prompt usually has a few clear parts. We do not always need all of them, but knowing them helps us.

The main parts are:

  • Instruction: what we want the model to do. For example, "Summarize this paragraph".
  • Context: the background information the model needs. For example, "The reader is a 10-year-old child".
  • Input data: the actual content the model must work on. For example, the paragraph we want summarized.
  • Output format: how we want the answer to look. For example, "Give the answer in three bullet points".

The structure looks like below:

+--------------------------------------------+
|                A Good Prompt               |
+--------------------------------------------+
|  Instruction   ->  what to do              |
|  Context       ->  background info         |
|  Input data    ->  the content to work on  |
|  Output format ->  how the answer looks    |
+--------------------------------------------+
                     |
                     ↓
              +-------------+
              |  AI Model   |
              +-------------+
                     |
                     ↓
              +-------------+
              |   Answer    |
              +-------------+

Here, we can see that the four parts stack together to form one complete prompt. When all four parts are present, the model knows what to do, what to work on, and how the answer must look, so there is no guessing left.

Let's see a simple prompt that uses these parts as below:

Instruction: Summarize the text below.
Context: The reader is a beginner with no technical background.
Input data: "A database is a place where applications store and organize their data so it can be found quickly later."
Output format: Answer in one short sentence.

Here, we can see that the prompt clearly tells the model what to do, who the reader is, what to work on, and how the answer must look. The model now has everything it needs.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

Zero-shot prompting

Now, let's learn about the different ways of prompting. The first one is Zero-shot prompting.

Zero-shot prompting means we ask the model to do a task without giving any example.

Let's see an example as below:

Classify this review as Positive or Negative.

Review: "The food was cold and the service was very slow."

Here, we have given no example of what Positive or Negative looks like. We simply asked the model to classify. The model uses its training knowledge to answer "Negative".

Zero-shot prompting works well for simple and common tasks. For tricky or unusual tasks, the model may not know exactly what we want. Let's see how the next approach solves this issue.

Few-shot prompting

Now, it's time to learn about Few-shot prompting.

Few-shot prompting means we give the model a few examples inside the prompt, and then ask it to follow the same pattern.

We show the model how the task is done, and then it follows that style for our new input.

Let's see an example as below:

Classify the review as Positive or Negative.

Review: "I loved the warm and tasty pizza." -> Positive
Review: "The room was dirty and smelled bad." -> Negative
Review: "The staff was friendly and helpful." ->

Here, we have given two complete examples first. The model sees the pattern, which means "Review followed by an arrow followed by the label". For the third review, the model continues the same pattern and answers "Positive".

By showing examples, we teach the model exactly how we want the answer, without any extra training.

So, when the task is unusual or needs a specific format, we use Few-shot prompting.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

Chain-of-Thought prompting

Now, let's understand a very useful technique called Chain-of-Thought prompting.

Chain-of-Thought prompting means we ask the model to explain its reasoning step by step before giving the final answer.

Let's say we have a small math problem with more than one step. When a model jumps straight to the answer, it can make mistakes. When it writes out each step first, it is more likely to get the answer right.

Let's see an example as below:

Question: A shop has 12 apples. It sells 5 in the morning and 3 in the evening.
How many apples are left? Let's think step by step.

Here, we have added the line "Let's think step by step". This small line tells the model to show its working. The model now answers like below:

Step 1: The shop starts with 12 apples.
Step 2: It sells 5 in the morning, so 12 - 5 = 7 apples are left.
Step 3: It sells 3 in the evening, so 7 - 3 = 4 apples are left.
Final answer: 4 apples are left.

Here, we can see that the model breaks the problem into small steps. Each step is simple, so the final answer is more likely to be correct. This is how Chain-of-Thought prompting improves accuracy for reasoning tasks.

System prompt and user prompt

Now, let's learn about two types of prompts that work together.

The system prompt is the instruction, usually hidden from the user, that sets the overall behavior and role of the AI model.

The user prompt is the actual message that we, the users, type.

Means, the system prompt sets the role and the rules, and the user prompt asks the question.

We can picture how they work together as below:

+----------------------+       +----------------------+
|     System Prompt    |       |      User Prompt     |
| (role and the rules) |       | (the typed question) |
+----------------------+       +----------------------+
                 ↓                   ↓
             +----------------------------+
             |          AI Model          |
             +----------------------------+
                           ↓
             +----------------------------+
             |           Answer           |
             +----------------------------+

Here, we can see that the system prompt is set once and shapes the overall behavior, while the user prompt changes with every message. The model reads both together, so each answer follows the fixed rules and still replies to the new question.

Let's say a company builds a support assistant. They set a system prompt like below:

You are a polite customer support assistant for a shoe store.
Always answer in a friendly tone. Never discuss other topics.

Here, we can see that the system prompt fixes the role and the rules once. After that, every user message is answered in that polite, on-topic style. So, the system prompt shapes the behavior, and the user prompt drives each conversation.

This is how real AI products control the model's tone and keep it focused. We have a detailed blog on Prompt Injection that explains how attackers try to break these rules and how we defend against it.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

A step-by-step example

The best way to learn this is by taking an example. Let's improve a weak prompt step by step and watch the output get better.

Step 1: We start with a weak prompt.

Write about dogs.

The issue with this prompt is that it is too open. The model does not know the length, the audience, or the purpose. The answer will be random and may not help us. Let's see how the next step solves this issue.

Step 2: We add a clear instruction and an audience.

Write a short paragraph about dogs for a 7-year-old child.

This is better. Now the model knows the length and the reader. But, we can have a better prompt than this too.

Step 3: We add the output format and the tone.

Write a short and cheerful paragraph about dogs for a 7-year-old child.
Use simple words. Keep it under four sentences.

Here, we can see that the prompt now controls the topic, the audience, the tone, the simplicity, and the length. The model has a strong pattern to follow, so the output is much closer to what we want.

This is how Prompt Engineering works in practice. We start simple, then we keep adding clarity until the prompt fully describes what we want.

To master Prompt Engineering, Chain of Thought (CoT) Prompting, and Context Engineering from the ground up, check out our AI and Machine Learning Program at Outcome School.

Best practices for Prompt Engineering

Now that we have understood how it works, let me share a few best practices that help us write strong prompts.

  • Be specific: clearly state the task, the audience, and the format. Vague prompts give vague answers.
  • Give context: tell the model the background it needs to answer well.
  • Show examples: when the task is tricky, use Few-shot prompting to demonstrate the pattern.
  • Ask for steps: for reasoning and math, use Chain-of-Thought prompting with "Let's think step by step".
  • Set the format: tell the model whether we want bullet points, a table, or a single sentence.
  • Iterate: if the answer is not perfect, refine the prompt and try again. Prompt Engineering is a cycle of small improvements.

Note: A common mistake we do is writing a long but unclear prompt. A prompt does not need to be long. It needs to be clear.

We have a detailed blog on Context Engineering that covers how to design everything the model sees, beyond the wording of one prompt.

Now, we have understood how Prompt Engineering works.

Frequently Asked Questions

Does a longer prompt give a better answer?

No. A prompt does not need to be long, it needs to be clear. A common mistake is writing a long but unclear prompt. A short prompt that clearly states the task, the audience, and the format gives the model a strong pattern to follow, so the answer matches what we want.

When should we use few-shot prompting instead of zero-shot prompting?

We use few-shot prompting when the task is tricky, unusual, or needs a specific format. Zero-shot prompting works well for simple and common tasks, where the model can answer from its training knowledge alone. For harder tasks, a few examples inside the prompt show the model exactly how the answer must look.

Does few-shot prompting train the model?

No. Few-shot prompting does not change the model. We only put a few examples inside the prompt, and the model follows the same pattern for our new input. This way, we teach the model exactly how we want the answer, without any extra training.

Can the user see the system prompt?

Usually, no. The system prompt is usually hidden from the user. It is set once and sets the overall role and rules of the AI model, while the user prompt is the message the user types. The model reads both together, so every answer follows the fixed rules.

What should we do if the answer from the model is not good enough?

We should refine the prompt and try again. Prompt Engineering is a cycle of small improvements. We start with a simple prompt, then we keep adding clarity, such as the audience, the tone, and the output format, until the prompt fully describes what we want.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.