Prompt Engineering Masterclass
Table of Contents
Introduction
Prompt engineering is the discipline of crafting effective inputs to large language models (LLMs) to elicit accurate, relevant, and useful responses. As LLMs become integral to production systems, mastering prompt engineering is no longer optional -- it is a core skill for AI engineers, data scientists, and software developers alike.
This tutorial provides a comprehensive, hands-on guide to advanced prompting techniques. You will learn how to apply zero-shot, few-shot, chain-of-thought, self-consistency, and tree-of-thought prompting strategies. You will also learn how to enforce structured outputs, design reusable prompt templates, evaluate prompt quality, and avoid common mistakes.
All examples use Python with the OpenAI API, but the principles apply to any LLM provider.
Prerequisites
- Python 3.9 or higher
- An OpenAI API key (or compatible LLM API)
- Basic understanding of LLMs and API calls
- Installed packages:
pip install openai langchain pydantic
Set your API key as an environment variable:
export OPENAIAPIKEY="sk-your-key-here"
Understanding Prompt Engineering Fundamentals
A prompt is the textual input provided to an LLM. The quality of the output is directly proportional to the clarity, specificity, and structure of the prompt. There are several dimensions to consider:
- Instruction clarity: Be explicit about what you want.
- Context: Provide relevant background information.
- Input/output format: Specify the desired format.
- Constraints: Define boundaries (length, style, language).
- Role assignment: Tell the model who it should act as.
import openai
client = openai.OpenAI()
def callllm(prompt: str, system: str = "", model: str = "gpt-4o", temperature: float = 0.7) -> str:
"""Utility function to call the LLM with a given prompt."""
messages = []
if system:
messages.append({"role": "system", "content": system})
messages.append({"role": "user", "content": prompt})
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
)
return response.choices[0].message.content
Zero-Shot Prompting
Zero-shot prompting means asking the model to perform a task without providing any examples. The model relies entirely on its pre-trained knowledge.
When to Use
- The task is straightforward and well-known (e.g., summarization, translation).
- You want to minimize prompt length and cost.
- The model has strong prior knowledge of the domain.
Example: Sentiment Analysis
prompt = """Classify the sentiment of the following review as 'positive', 'negative', or 'neutral'.
Review: "The product arrived on time and works exactly as described. Very satisfied with my purchase."
Sentiment:"""
result = callllm(prompt, temperature=0.0)
print(result) # Expected: positive
Example: Text Summarization
prompt = """Summarize the following paragraph in exactly two sentences.
Paragraph: "Machine learning is a subset of artificial intelligence that focuses on building