The Core Idea: A model only knows what is in your prompt. Prompt engineering means giving a clear brief: role, task, context, format, constraints and examples, then testing and improving it like code.
- Beginner: What Is Prompt Engineering?
A prompt is the text you send to a model. Prompt engineering is the skill of writing prompts so that the model gives the answer you actually want. The model cannot read your mind. It only sees the words you give it, so missing details are filled in with guesses.
Think of a prompt as instructions for a very capable new colleague who knows a lot in general but knows nothing about your situation. The clearer your brief, the better the result.
- Beginner: The Parts of a Good Prompt
| Part | Question it answers | Example |
|---|---|---|
| Role | Who should the model act as? | You are a customer-support analyst. |
| Task | What exactly should it do? | Classify the review below. |
| Context | What material should it use? | Review: "The battery died after two days." |
| Format | How should the answer look? | Reply with JSON only. |
| Constraints | What must or must not happen? | At most 12 words. Do not add other text. |
| Examples | What does a good answer look like? | Two or three sample inputs and outputs. |
Remember: You do not need all six parts every time. Add the ones that remove guesswork for your task.
- Medium: Few-Shot Examples and Delimiters
Zero-shot means asking with no examples. Few-shot means showing the model a few input and output pairs first. Examples are often the strongest way to communicate a style or format, because the model copies the pattern.
Use delimiters, clear separators such as triple quotes or tags, to keep your instructions apart from the data. This also makes the prompt easier to read and safer against text in the data that looks like instructions.
| Technique | When to use it |
|---|---|
| Zero-shot | Simple, common tasks |
| Few-shot (2 to 5 examples) | You need a specific format, tone or label set |
| Delimiters around data | Always, when you paste documents or user text |
| Ask for a format | Whenever another program will read the answer |
Tip: Make examples diverse and correct. A wrong example teaches the wrong pattern.
- Medium: Reasoning, Step by Step
For multi-step problems such as math, logic or planning, asking the model to show its reasoning before the final answer often improves accuracy. This is called chain-of-thought prompting. A simple version is to add a line such as Think step by step, then give the final answer on its own line.
Newer reasoning models: Some models already reason internally before answering. For them, a simple clear question often works best and extra instructions to think step by step may not be needed. Test both ways on your task.
- Advanced: Testing, Iterating and Staying Safe
Prompts are small programs, so treat them like code: test them on a set of real examples, change one thing at a time, and keep a record of what worked.
| Habit | Why it matters |
|---|---|
| Keep a test set of 20+ real inputs | A prompt that works on one example may fail on others |
| Change one thing at a time | So you know what caused the improvement |
| Version your prompts | You can go back when a change makes things worse |
| Watch for prompt injection | Text in user data may say ignore previous instructions. Keep untrusted text clearly separated and never give the model power you would not give a stranger |
| Ask for uncertainty | Allow answers like I do not know to reduce invented facts |
Security note: Never put secrets, passwords or API keys inside a prompt. Assume anything in the prompt could be revealed.
- Visual Explanation: From Vague to Precise
The figure contrasts a weak prompt with a strong one and shows the loop used to improve prompts.
Click to see the six-part checklistCompare ▼
Part 1 compares what each prompt contains and what you get back. Part 2 shows the write, test and improve cycle. Open the bar to see the checklist the code uses.
- Python Implementation: A Prompt Linter and a Few-Shot Builder
This script does two practical things. It scores a prompt on the six parts of a good prompt (a simple linter), and it builds a few-shot prompt from a list of examples so you never have to type it by hand.
# A small "prompt linter": checks whether a prompt contains the parts of a good promptCHECKS = { "role": ["you are", "act as"], "task": ["summarize", "classify", "explain", "write", "extract", "translate"], "context": ["context:", "text:", "document:", "input:"], "format": ["format", "json", "bullet", "table", "words", "sentences"], "constraints": ["do not", "only", "must", "at most", "never"], "examples": ["example", "for instance"],} def lint(prompt): text = prompt.lower() found = [name for name, words in CHECKS.items() if any(w in text for w in words)] return found, [name for name in CHECKS if name not in found] weak = "Tell me about this review."strong = ( "You are a customer-support analyst.\n" "Task: classify the review below as positive, neutral or negative.\n" "Input: 'The battery died after two days.'\n" "Format: answer with JSON only, like {\"label\": \"...\", \"reason\": \"...\"}.\n" "Constraints: the reason must be at most 12 words. Do not add any other text.\n" "Example: Input: 'Great screen!' -> {\"label\": \"positive\", \"reason\": \"praises the screen\"}")for name, prompt in [("weak prompt", weak), ("strong prompt", strong)]: found, missing = lint(prompt) print(name, "-> score", len(found), "/", len(CHECKS), "| missing:", missing) # Build a few-shot prompt from data instead of typing it by handexamples = [("I love it", "positive"), ("It broke at once", "negative"), ("It is okay", "neutral")]def few_shot(examples, new_text): lines = ["Classify the sentiment as positive, neutral or negative."] for text, label in examples: lines.append("Text: " + text + "\nSentiment: " + label) lines.append("Text: " + new_text + "\nSentiment:") return "\n\n".join(lines) prompt = few_shot(examples, "Delivery was slow but the product is great")print("Few-shot prompt has", len(prompt.split()), "words and ends with:", repr(prompt.split("\n")[-1]))The weak prompt scores 0 out of 6 and the strong one scores 6. The few-shot builder always ends with Sentiment: so the model knows exactly what to complete.
Interactive Knowledge Check
Key Points
The Big Picture
Clear role + clear task + the right context + a format + an example → predictable answers
Better prompts are the cheapest improvement you can make. Before fine-tuning or switching models, improve the prompt.
Up Next: You now know how to write the prompt. In LLM APIs and Open-Source Models you will learn how to send it from your own code and how to choose between hosted and local models.