How to Write Good Prompts: A Practical Guide (2026)
Learn the three key elements of a good prompt, see good vs bad examples, master advanced techniques like few-shot and chain-of-thought, and use ready-made prompt templates for writing, coding, analysis, and translation.
The Three Elements of a Good Prompt: Context, Role, Task
Every effective prompt contains three core elements: role, task, and context. Mastering these three is the difference between getting mediocre answers and getting exactly what you need.
Role tells the AI who to act as. This sets the perspective, expertise level, and tone. For example, "You are a senior Python developer" produces very different output than "You are a beginner learning Python." The role primes the model to draw on relevant patterns from its training data.
Task describes what you want the AI to do. Be specific about the output format, length, and scope. "Write a function" is vague; "Write a Python function that takes a list of integers and returns the median value, with error handling for empty lists" is specific and actionable.
Context provides the background the AI needs. This includes your audience, constraints, prior work, and any reference material. Without context, the AI guesses — and it often guesses wrong. The more context you provide, the more accurate and useful the response.
Good Prompt vs Bad Prompt: Side-by-Side Examples
The best way to understand prompt quality is to see good and bad versions side by side. Here are three common scenarios:
--- BAD ---
Write about AI.
--- GOOD ---
Role: You are a technology journalist writing for a general audience.
Task: Write a 300-word article explaining how AI chatbots work, using a simple analogy.
Context: The readers have no technical background. Avoid jargon. Use a restaurant analogy to explain how language models predict the next word.
--- BAD ---
Fix my code.
--- GOOD ---
Role: You are a Python debugging expert.
Task: Find and fix the bug in this function, then explain what was wrong.
Context: This function should return the factorial of n, but it returns incorrect results for n > 5.
```python
def factorial(n):
result = 1
for i in range(1, n):
result *= i
return result
```
--- BAD ---
Translate this.
--- GOOD ---
Role: You are a professional translator specializing in tech documentation.
Task: Translate the following English text to Chinese, keeping technical terms in English where appropriate.
Context: This is for a developer-facing API documentation page. The tone should be professional and concise.
Text: "The API key must be included in the Authorization header as a Bearer token."Three pairs of bad vs good prompts showing how adding role, task, and context improves results
Advanced Techniques: Few-Shot, Chain-of-Thought, Role-Playing
Once you've mastered the basics, these advanced techniques can dramatically improve results for complex tasks.
Few-shot prompting means giving the AI examples of the input-output pattern you want before asking it to perform the task. This is especially powerful for classification, formatting, or style-matching tasks. Instead of describing the format, you show it.
Chain-of-thought (CoT) prompting asks the AI to reason step by step before giving the final answer. This significantly improves performance on math, logic, and multi-step reasoning problems. Simply adding "Let's think step by step" can boost accuracy, but explicitly asking the AI to show its reasoning process works even better.
Role-playing extends the role element by giving the AI a persona with specific expertise, constraints, and even a conversational style. For example, "You are a strict code reviewer who always points out potential security issues" produces more thorough reviews than a generic prompt.
--- FEW-SHOT EXAMPLE ---
Classify the sentiment of each review:
Review: "This product is amazing!" → Sentiment: Positive
Review: "Worst purchase ever." → Sentiment: Negative
Review: "It's okay, nothing special." → Sentiment: Neutral
Review: "Battery life is terrible but the screen is great." → Sentiment:
--- CHAIN-OF-THOUGHT EXAMPLE ---
Question: A store sells apples at $2 each. If you buy 5, you get 10% off your total. How much does buying 5 apples cost?
Let's think step by step:
1. Regular price for 5 apples: 5 × $2 = $10
2. Discount: 10% of $10 = $1
3. Final price: $10 - $1 = $9
Answer: $9Examples of few-shot prompting and chain-of-thought reasoning
Prompt Templates for Common Scenarios
Here are ready-to-use templates for four common tasks. Replace the bracketed placeholders with your specific content.
- Writing: Role (professional writer) + Task (write [type] for [audience]) + Context (tone, length, key points to cover)
- Coding: Role (expert [language] developer) + Task (write/debug/refactor [function description]) + Context (existing code, error messages, constraints)
- Analysis: Role (data analyst) + Task (analyze [data/text] and provide insights) + Context (what you want to find, output format, audience)
- Translation: Role (professional translator) + Task (translate [text] from [source] to [target]) + Context (domain, tone, terms to keep in original language)
Common Prompt Mistakes to Avoid
Even with the right framework, certain mistakes consistently produce poor results. Here are the most common ones:
Being too vague. "Write something about AI" gives the AI no direction. Always specify the format, length, audience, and angle. Vague prompts produce vague answers.
Overloading the prompt. Cramming multiple unrelated tasks into one prompt confuses the AI. If you need several things done, break them into separate prompts or use a multi-step approach where each step builds on the previous output.
Not iterating. Your first prompt rarely produces the perfect result. Treat prompt writing as a conversation — if the output isn't right, refine your prompt based on what went wrong and try again. The best prompt engineers iterate 3-5 times before settling on a final version.
Ignoring model differences. A prompt that works great with GPT-4o might perform poorly with a smaller model. Different models have different strengths — adjust your prompt complexity based on the model you're using.
FAQ
What makes a good AI prompt?
A good prompt has three elements: a clear role (who the AI should act as), a specific task (what to do, including format and length), and relevant context (audience, constraints, background). Being specific and providing examples always improves results.
How long should my prompt be?
There's no fixed length — it depends on the task complexity. A simple question might need one sentence. A complex coding task might need several paragraphs of context. The key is including all necessary information without adding irrelevant details. Start concise and add detail if the output isn't what you want.
What is chain-of-thought prompting?
Chain-of-thought (CoT) prompting asks the AI to reason through a problem step by step before giving the final answer. Instead of jumping to a conclusion, the AI shows its work. This significantly improves accuracy on math, logic, and multi-step reasoning tasks. You can trigger it by adding "Let's think step by step" or explicitly asking the AI to show its reasoning.
Should I use the same prompt for different AI models?
Not always. While the core principles (role, task, context) apply to all models, different models respond differently to prompt styles. More capable models like GPT-4o and Claude can handle complex, multi-instruction prompts. Smaller models may need simpler, more focused prompts. Test your prompt on the specific model you plan to use and adjust as needed.
Can I save and reuse prompts?
Yes, and you should. Keeping a library of tested prompts for recurring tasks saves time and ensures consistent results. Many teams maintain shared prompt libraries. When reusing, adjust the context section for each specific use case while keeping the role and task structure intact.
Related in This Series
Related Providers
Sources
- OpenAI Prompt Engineering GuideOpenAI · Checked 2026-07-29
- Anthropic Prompt Engineering OverviewAnthropic · Checked 2026-07-29
- Google Gemini Prompting StrategiesGoogle · Checked 2026-07-29
- Chain-of-Thought Prompting PaperarXiv · Checked 2026-07-29