What is Prompt Engineering? How and Why Does It Work?
10 minutes
Today, we rely on generative AI models and tools to create everything from text and images to videos and code. These tools make AI more accessible by allowing us to interact with it using everyday language instead of complex code commands. The rise of chat-based AI has transformed how people use AI, making it available to a much broader audience through intuitive, easy-to-use interfaces.
An example of an AI-generated image of a cat created using ChatGPT and DALL-E by just typing a simple sentence like "Create an image of a cat."
Not long ago, AI models were mainly used by researchers and developers who had to write code to interact with them. Now, anyone can type a plain-language prompt and receive an immediate response. This shift is why Andrej Karpathy, OpenAI co-founder and research scientist, famously called English "the hottest new programming language."
In this article, we'll show that prompt engineering is a skill anyone can learn. We'll break down how it works, explain why researchers are studying it, and discuss why there's no one-size-fits-all approach for every use case.
Table of Contents
- Why Do Prompts Matter?
- How Does Prompt Engineering Work?
- Why Do Scientists Study Prompts?
- No One-Size-Fits-All Approach
- Beginner's Corner
- Resources
What is Prompt Engineering?
As researchers and everyday users began experimenting with AI tools, a new field of study emerged: prompt engineering. At its core, prompt engineering involves writing prompts - natural language inputs that instruct AI models to produce specific outputs. While this may seem highly technical, the principles behind prompt engineering are designed to make AI more user-friendly.
Prompt engineering is less about coding and more about how you phrase your instructions to get the best results. Sometimes, it's as simple as asking a clear, specific question. Other times, it's about structuring your request in a way that aligns with how the AI understands language. Learning these techniques can make AI responses much more accurate and useful.
Why Do Prompts Matter?
The way you phrase prompt can drastically affect the quality and specificity of output generated by the model. Let's look at the two examples below where we ask the model to describe photosynthesis in two different ways.

Example 1
Describe photosynthesis
Result: A detailed, technical explanation including molecular formulas and stages of the process.

AI Output
Photosynthesis is the process by which green plants, algae, and some bacteria convert light energy into chemical energy. It primarily takes place in the chloroplasts of plant cells, using the green pigment chlorophyll to absorb sunlight.
Steps of Photosynthesis
Photosynthesis occurs in two main stages:
-
Light-dependent Reactions (Occurs in the Thylakoid Membrane)
- Sunlight is absorbed by chlorophyll.
- Water molecules (H₂O) are split into oxygen (O₂), protons, and electrons.
- The energy from light is used to produce ATP (adenosine triphosphate) and NADPH (an electron carrier), which store energy for the next stage.
-
Light-independent Reactions (Calvin Cycle, Occurs in the Stroma)
- ATP and NADPH from the light-dependent stage are used to convert carbon dioxide (CO₂) into glucose (C₆H₁₂O₆).
[Truncated for brevity]

Example 2
Explain photosynthesis like I am 5.
Result: A playful, accessible explanation aimed at young learners.

AI Output
Okay! Imagine a plant is like a little chef that makes its own food. But instead of using a stove, the plant uses sunlight! 🌞
Here's how it works:
- The plant takes in sunlight with its green leaves. 🌱
- It drinks water from the ground through its roots. 💧
- It breathes in air (carbon dioxide) from the sky. 🌬️
Then, like magic, the plant mixes everything together and makes its own food (sugar) to grow big and strong! 🍃 And guess what? While doing this, the plant also gives out oxygen, which we need to breathe! 😃
So, thanks to plants, we have food and fresh air! Isn't that cool? 🌍💚
This difference illustrates how subtle changes in phrasing can shift the response to suit different audiences.
How Does Prompt Engineering Work?
Since generative AI models like GPT rely on patterns learned from vast amounts of data, prompt engineering taps into these internal mechanisms to guide the model in generating desired responses.
Let's break down the inner workings of large language models (LLMs), the powerful engines behind popular chatbots like Gemini and ChatGPT, into several steps to understand how they work with prompts.
1. Model Training
LLMs are trained on massive datasets containing diverse text types through a process called unsupervised learning. During this training, the model learns to predict the next word in a sentence based on the surrounding context, refining its ability to understand language patterns and relationships.
2. Tokenization and Embeddings
Unlike humans, the model doesn't inherently "understand" language. Instead, it breaks down natural language into smaller components called tokens. For example, the phrase "I love AI" is split into three tokens: "I", "love", and "AI".
[I love AI] -> ["I", "love", "AI"]
These tokens are then converted into numerical representations, known as embeddings. Embeddings help the model identify statistical relationships in the text, learning how words, phrases, and concepts connect. With these representations, the model is prepared to generate responses that align with learned language patterns.
3. Input Prompt Processing
When you provide an input prompt, the model applies the same tokenization process. The prompt is split into tokens, which are then encoded as embeddings. This allows the model to process the input in a way that reflects the underlying relationships between words and concepts.
4. Self-Attention
One of the key features of LLMs is their self-attention mechanism. This allows the model to determine which tokens are most relevant to each other. For example, in the sentence "The cat sat on the mat," the model pays close attention to the relationship between "cat" and "sat," even though other words, like "on" and "the," separate them.
5. Generating Responses with Probability-Based Predictions
After the model has processed the input prompt through tokenization, embeddings, and self-attention, it is ready to generate a response. In this step, the model predicts the next word (or token) based on the context derived from all prior processes.
-
Tokenization: First, the prompt is broken down into tokens (e.g., "The capital of France is" becomes ["The", "capital", "of", "France", "is"]). This allows the model to analyze the structure of the sentence.
-
Embeddings: These tokens are converted into numerical embeddings, which help the model understand the relationships between words in terms of their semantic meaning. These embeddings reflect how "capital", "France", and "is" are related to one another and provide the model with a foundational understanding of what is being asked.
-
Self-Attention: Using the self-attention mechanism, the model weighs the importance of each token relative to the others. For example, it will focus on the relationship between "capital" and "France" more heavily than "capital" and "is," because these are more contextually related in answering the prompt. This helps the model prioritize relevant tokens when making predictions.
-
Probability-Based Prediction: With this information in hand, the model now predicts the most likely next token by calculating a probability distribution. It does this by evaluating the context of the input prompt (from tokenized inputs, their embeddings, and the attention scores). For example, after processing the phrase "The capital of France is," the model assigns high probability to the token "Paris" as the next word because it has learned from previous data that "Paris" is strongly associated with this phrase.
This process continues iteratively, with the model predicting and appending one token at a time. In a more complex prompt like "Tell me about the capital of France," the model understands that more context is requested.
By understanding these steps, we see why even small tweaks to a prompt can have a significant impact on the generated output.
Why Do Scientists Study Prompts?
Prompting acts as a dynamic interface between the user and the model's vast, pre-trained knowledge. In other words, it allows for contextual fine-tuning, adjusting a model's output to meet specific needs by modifying the input rather than altering the underlying model. This approach is cost-effective, efficient, and offers flexibility in adapting the model to various applications.
As a result, prompt engineering has become a field of scientific study to find the most effective way to elicit the desired response from the model. It has also opened up new avenues for interdisciplinary research, intersecting fields such as linguistics, psychology, computer science, and cognitive science.
By combining theoretical insights with empirical testing, scientists have developed several key techniques to optimize prompt design. Some of the most notable methods include:
- Few-Shot Prompting: This technique provides a small number of examples within the prompt, guiding the model to learn the pattern and apply it to generate responses.
- Chain-of-Thought Prompting: This approach encourages the model to engage in multi-step reasoning by explicitly asking it to think through the problem before arriving at a conclusion.
- Self-Consistency Prompting: This method aims to enhance the reliability of the model's output by generating multiple responses to the same prompt and then selecting the most consistent one.
- Plan-and-Solve Prompting: In this technique, the task is broken down into smaller, manageable steps, guiding the model through each phase. This promotes more structured reasoning and ensures that the model doesn't miss key elements when tackling a complex problem.
- Role Prompting: This technique assigns the model a specific persona or perspective to tailor its responses. By specifying a role, the model's output can be aligned with a particular tone, style, or content.
These methods show that prompt engineering is much more than just a trick for making models perform better, it's a scientific discipline that explores the nuances of AI-human interaction.
No One-Size-Fits-All Approach in Prompting
There is no universal formula for crafting the perfect prompt. The changing nature of language models and their training data means a prompt effective in one scenario may fail in another. Successful prompting requires continuous adaptation and customization.
Several technical factors illustrate why a single approach cannot address every use case:
Model Architecture and Sensitivity
Minimal changes in phrasing can alter text tokenization and embedding, shifting the model's internal representation and probability distribution over the next token. Thus, a prompt that excels in one scenario may fall short in another.
Diverse Training Data and Learned Patterns
Models trained on varied datasets absorb a broad spectrum of linguistic patterns and associations. Each task—whether creative storytelling or technical problem solving—demands its own tailored prompt to activate the appropriate facets of the model's knowledge.
Task-Specific Context and Requirements
Every task, from summarizing research to generating marketing content or solving mathematical problems, comes with unique contextual demands, tones, and structural requirements. For instance, a chain-of-thought prompt can boost multi-step reasoning, whereas few-shot prompting may better guide the model in mimicking a specific style. A one-size-fits-all prompt cannot capture these nuanced differences.
Iterative Refinement
Optimizing prompts is an ongoing process. Users must refine their language and adjust context based on the model's output, emphasizing that effective prompt engineering is as much about iteration as initial design.
Dynamic and Evolving Techniques
As language models evolve, so do the techniques for prompting them. What works today might require rethinking tomorrow, making prompt engineering a dynamic and evolving discipline.
Beginner's Corner: Getting Started with Prompt Engineering
If you're new to prompt engineering, here are a few simple tips to help you begin:
-
Start simple: Begin with clear, straightforward prompts. For example, instead of asking "What can you tell me about the weather?" try "Describe today's weather in simple terms."
-
Experiment: Try writing different versions of your prompt. Compare the outputs and note which version gets closer to your desired answer.
-
Use examples: Look at sample prompts in our guide and other resources to see how small changes can affect the response.
-
Practice regularly: The more you experiment, the better you'll understand how your wording influences the output. Over time, you'll naturally develop your own style.
This beginner-friendly approach will help you build confidence as you learn how to craft effective prompts.
Conclusion
Prompt engineering is a powerful skill that allows users to achieve more accurate, context-aware, and meaningful outputs. While prompt engineering may seem like trial and error, it is actually a systematic technique to generate optimized output from generative AI models. Providing relevant information and adding specific and clear instructions are key ways to improve the quality of response from the model.
Resources
Refer to the following resources to learn more about prompt engineering:
- Prompt Engineering Basics Guide
- The Prompt Report: Insights from The Most Comprehensive Study of Prompting Ever Done
- Prompt Engineering vs. Fine-Tuning
Courses:
- Free ChatGPT for Everyone Course
- Introduction to Prompt Engineering Course
- 14 Prompt Engineering Courses
Getting started with AI tools:
- A Complete How-To Guide to ChatGPT
- A Complete How-To Guide to Google Gemini
- A Complete How-To Guide to Perplexity AI
- A Complete How-To Guide to Suno
- A Complete How-To Guide to NotebookLM
Valeriia Kuka
Valeriia Kuka, Head of Content at Learn Prompting, is passionate about making AI and ML accessible. Valeriia previously grew a 60K+ follower AI-focused social media account, earning reposts from Stanford NLP, Amazon Research, Hugging Face, and AI researchers. She has also worked with AI/ML newsletters and global communities with 100K+ members and authored clear and concise explainers and historical articles.
Bhuwan Bhatt
Bhuwan Bhatt, a Machine Learning Engineer with over 5 years of industry experience, is passionate about solving complex challenges at the intersection of machine learning and Python programming. Bhuwan has contributed his expertise to leading companies, driving innovation in AI/ML projects. Beyond his professional endeavors, Bhuwan is deeply committed to sharing his knowledge and experiences with others in the field. He firmly believes in continuous improvement, striving to grow by 1% each day in both his technical skills and personal development.