Prompt Engineering
This Lesson Will Cover
- 🔍 What is Prompt Engineering
- 🎯 Few-Shot and Zero-Shot Learning
- 📋 Prompt Design Patterns
- 🧠 Latent Space Activation
- ⛓️ Chain-of-Thought Prompting
- 👥 System and User Role Prompting
- ⚠️ Common Pitfalls and Solutions
- 🔧 Model-Specific Prompting Strategies
What is Prompt Engineering?
The performance and effectiveness of Large Language Models (LLMs) depend on how they are prompted. The goal of prompt engineering is to write instructions for LLMs that produce good outputs.
What makes a good output? Well, that depends on what we’re looking for. In technical terms, good outputs are defined by our evaluation metrics. We’ll talk about those in the AI Deployment > Monitoring > LLM Evaluation Monitoring lesson.
Why do Prompt Engineering?
Prompt engineering helps us get what we want out of the model.
There are some basic, well-defined patterns that we need to understand, which we’ll explore below.
Few-Shot and Zero-Shot Learning
The concepts of few-shot and zero-shot learning emerged from the field of machine learning, particularly in computer vision[1], before being adapted for language models. These techniques address the challenge of making predictions with limited or no training examples—a fundamental problem in AI that mirrors the human ability to learn from minimal exposure.
-
Few-shot prompting: Provide a few examples to guide the model.
Convert these temperatures to Celsius: Fahrenheit: 32 → Celsius: 0 Fahrenheit: 100 → Celsius: 37.8 Fahrenheit: 212 → Celsius: -
Zero-shot prompting: Give no examples, relying solely on the instruction.
Convert 212 from Fahrenheit to Celsius.
Language models have advanced to the point where zero-shot prompting is often all that we need (at least, when using language models conversationally).
However, few-shot prompting remains valuable for specialized or complex tasks, and it’s an essential tool for doing prompt engineering with smaller models.
Prompt Design Patterns
Good prompts are clear, specific, and tailored to the task at hand.
Here are some general design patterns:
-
Open-Ended Prompts
Provide basic instructions and give the model more freedom to find the best answer.My friend has been really sad lately. Write them a letter to help cheer them up. -
Guided Prompts
Provide specific guidelines that restrict the model freedom but attempt to improve answer quality.My friend has been really sad lately. Write them a letter to help cheer them up. Use these best practices: - Express empathy and validate their feelings - Include words of encouragement and hope for the future - Keep the tone warm and genuine -
Role-Based Prompts
Instruct the model to adopt a specific role or persona to guide its responses.As a medical professional reviewing patient symptoms: - Persistent headache for 3 days - Fever of 101°F - Fatigue and muscle weakness What are the potential causes of these symptoms considering the patient's history? -
Examples-Based Prompts
Show the model examples of desired behavior, i.e. few-shot prompting.Translate the following text to French: English: Hello, how are you? French: Bonjour, comment ça va? English: Good morning, what's your name? French: -
Template-Based Prompts
Provide a structured template for the model to fill in. (This pattern is super neat, and usually works well.)Book Review Template: Title: [Book Name] Author: [Author Name] Genre: [Genre] Summary: [2-3 sentences about the plot] Strengths: [List 2-3 strong points] Weaknesses: [List 1-2 areas for improvement] Rating: [1-5 stars]
Latent Space Activation
The latent space of an LLM is a high-dimensional space where the model encodes its knowledge and relationships between concepts. When we prompt an LLM, we’re activating specific regions of this latent space, guiding the model to focus on particular concepts, knowledge domains, or patterns of reasoning.
For example:
-
Domain-Specific Activation
As a quantum physicist...This type of prompt activates regions related to physics concepts, scientific terminology, and academic writing patterns.
-
Style-Based Activation
In the style of Shakespeare...This activates regions encoding literary devices, archaic English, and poetic structures.
-
Contextual Activation
In the context of 1920s America...This triggers historical knowledge, cultural references, and period-specific language patterns.
Understanding latent space activation helps us see why certain prompts work better than others - they more precisely target the relevant regions of the model’s knowledge representation. This is why specific, well-crafted prompts often yield better results than vague or ambiguous ones.
Chain-of-Thought Prompting
In some cases, breaking down reasoning into intermediate steps, called chain-of-thought (CoT) prompting, can significantly improve performance.
This is especially true for complex tasks like math problems or logical reasoning.
It can also give models more opportunities to perform helpful tool calls[2].
Prompt
A customer just handed me $10 USD for a $7.38 CAD item.
How much change should I give them?
Tell me the amount in CAD dollars.
Prompt with CoT
A customer just handed me $10 USD for a $7.38 CAD item.
How much change should I give them?
Tell me the amount in CAD dollars.
Think step-by-step.
By encouraging the model to think step-by-step, CoT prompts enhance reasoning quality and reduce errors. Models like GPT-o1 and DeepSeek were designed around this insight.
System and User Role Prompting
Some LLMs allow you to define roles explicitly to guide interactions. This approach, often used with models like OpenAI’s ChatGPT, separates system instructions (e.g., persona or behavior) from user prompts.
Example:
System: "You are an expert travel planner. Respond concisely and provide practical recommendations."
User: "What are the best places to visit in Japan during spring?"
The system prompt establishes tone and expertise, while the user prompt focuses on the specific query.
- System prompts act as global context, influencing the overall tone, style, and behavioral constraints of the model’s response.
- User prompts direct the model to generate task-oriented outputs.
This behaviour is possible because of the way these models were trained to interpret structured inputs. During training, models learn to recognize and respond differently to prompts based on their position in the input sequence and their formatting patterns:
- System prompts typically appear at the beginning of conversations and use specific markers or prefixes (like “System:” or special tokens).
- User prompts follow later in the sequence with their own distinct markers.
This positioning and formatting create implicit patterns that the model learns to associate with different types of responses. The distinction isn’t hardcoded into the model architecture but rather emerges from how the training data is structured and how the model learns to process different input patterns.
Common Pitfalls and How to Avoid Them
-
Ambiguous Prompts
- Bad: “Tell me about dogs”
- Better: “What are the key responsibilities and considerations when adopting a dog as a first-time pet owner?”
-
Overloading the Model
- Avoid overly complex instructions in a single prompt. Break tasks into smaller, manageable chunks.
-
Lacking Context and Examples
- Provide relevant context and examples, especially for specialized or domain-specific tasks.
Prompting Different Models
Different LLM providers require distinct prompting approaches to achieve optimal results.
Provider-Specific Patterns
Each of the top vendor models has its own vibe of prompting.
For example, OpenAI models respond well to detailed system prompts and role-based instructions, whereas Anthropic’s Claude might instead perform better with natural, conversational prompts, e.g.
OpenAI: "You are an expert physicist. Explain quantum entanglement."
Anthropic: "I'd like to understand quantum entanglement. Could you explain it?"
This advice isn’t meant to be a hard-and-fast rule, but just an example to convey the idea of prompt vibes (I’m just making this term up, by the way).
Small-Model Considerations
The flagship models from OpenAI and Anthropic will typically perform well, even with minimal prompt tuning.
However, smaller models (e.g. llama3.2:1b, as a relatively extreme example), benefit more dramatically from more explicit and structured prompting.
Large Model: "Summarize this article"
Smaller Model: "Write a 3-sentence summary of this article, focusing on the main argument and two supporting points"
Vendor Lock-in
Prompt engineering is an iterative process of trial and error. We find out what works based on testing and user feedback, optimizing over time.
For example, through testing and evaluating against evals, we might find that adding a sentence like “check your work” or “show me your sources” helps reduce hallucinations.
However, it’s important to understand that over-engineering a prompt can lead to vendor lock-in. Each optimization might improve the quality of the prompt for one model while reducing (or failing to improve) the response quality of another. This means we could become dependent on a single model to maintain a certain level of performance.
With this in mind, it might be beneficial to avoid overly complex prompts that restrict a model’s ability to generate its own natural responses.
Conclusion
This was an awesome lesson, right? I hope you took away some new ideas for getting better results out of LLMs.
Prompt engineering is the first and most straightforward dials that we can use to try and get a model to generate what we’re looking for.
There’s a great review from Feb 2024 from Sahoo et al.[3] on prompt engineering techniques, which is definitely worth checking out. It’s a quick read. Very dense. Pretty pictures.
Key Takeaways
📝 Prompt engineering is about crafting effective instructions for LLMs using natural language.
🎯 Few-shot and zero-shot learning techniques help models perform with limited or no examples.
🤖 Different models may require different prompting strategies.
⚠️ Avoid common pitfalls like ambiguous instructions and overloading the model.
Next: Fine-Tuning
Next, we’ll explore the next dial you might think about adjusting: fine-tuning.
Footnotes
See Zero-shot learning wiki article for historical context. ↩︎
OpenAI’s GPT o3 was the first model I used that has highly effective tool calling abilities in it’s step-by-step thinking process. Note that this goes beyond prompt engineering: both tool calling and step-by-step thinking like this have been trained right into the model. ↩︎
A Systematic Survey of Prompt Engineering in Large Language Models: Techniques and Applications paper arXiv link ↩︎
Comments (0)
No comments yet.