Lesson 6: Prompt Strategies for Different Tasks
Learning goals:
- Master the prompt best practices for code-generation tasks
- Learn effective strategies for writing and summarizing documents
- Understand prompt techniques for data analysis and extraction
Prerequisites: << 05 Debugging and Improving Prompts
Different tasks need different prompt strategies
You've learned the basic structure of a prompt, few-shot, CoT, and how to debug — those are general skills. But different kinds of tasks have their own quirks and traps, and they call for targeted strategies.
Code generation needs clear inputs, outputs, and edge cases; document writing needs a defined audience and tone; data extraction needs to handle missing values and keep the format consistent. This lesson walks through concrete techniques for three common task types.1
Task type 1: Code generation
Code generation is one of the most common ways people use AI. The key is simple: spell out the requirements clearly, and the AI can write code you can actually use.1
The six elements of a code-generation prompt
A good code-generation prompt should include:1
- Language and version: Python 3.10, TypeScript 5.0
- Function signature: input parameter types, return type
- Core logic: what the function should do
- Edge cases: how to handle empty input and error conditions — this is defensive programming: assume the input might be invalid and decide up front how to respond
- Code style: comments, type hints, error handling
- Dependency limits: standard library only, or which third-party libraries are allowed
Comparison: vague vs clear code prompts
❌ Fuzzy prompt:
The AI can only guess: what data? Process it how?
✅ Clear prompt:
This prompt nails down the input, output, edge cases, and code style, so the AI can write usable code on the first try.
There's research behind this: "prompts with explicit specifications reduced the need for back-and-forth refinements by 68%"1 — spell out the details and you're far more likely to get code you can use straight away.
Code-generation best practices
Practice 1: Spell out the input and output data structures
Don't say "process the data." Say "input is List[Dict[str, Any]], output is Dict[str, int]."
Practice 2: Use examples to clarify edge cases
Practice 3: Specify the code style
Practice 4: Say what you don't want
Code-review prompts
When you ask the AI to review code, name the review dimensions explicitly:
Name the review dimensions, and the AI won't waste time on details that don't matter.
Task type 2: Writing and summarizing documents
Document work covers a lot: technical docs, meeting notes, article summaries, report generation.
The key elements of document writing
- Target audience: technical vs non-technical readers
- Purpose: explaining how to use something vs persuading a decision-maker
- Tone and style: formal vs casual, detailed vs concise
- Structure template: which sections organize the content
Comparison: document-summary prompts
❌ Fuzzy prompt:
✅ Clear prompt:
This prompt nails the audience (frontend engineers), the purpose (quick start), the structure (four parts), and the tone (practical).
Document-writing best practices
Practice 1: Spell out the audience's background
Practice 2: Provide a structure template
Practice 3: Control the level of detail
Meeting-notes prompts
Task type 3: Data analysis and extraction
Data analysis covers pulling structured information out of text, classifying, filtering, and counting.
A reliable data-extraction prompt spells out four things up front:
- Field definitions: what each field means and its allowed range of values
- Missing-value handling: what to do when a field can't be found
- Output format: JSON, CSV, table
- Data validation: whether the extracted data needs checking
❌ Fuzzy prompt:
✅ Clear prompt:
The examples cover both the complete case and the missing case, so the AI knows to output null when it can't find something rather than making it up — that habit of confidently fabricating facts is called hallucination.
Practice 1: Spell out the allowed values for each field
Practice 2: Use few-shot to standardize the format
When extracting structured data, 2-3 examples work better than a written description (the few-shot technique from Lesson 3).
Practice 3: Say how to handle edge cases
Practice 4: Add data validation
Applying the general principles
Whatever the task, the general principles from earlier lessons still hold:
- The four elements (Lesson 2): role, task, format, constraints
- Few-shot (Lesson 3): give 2-3 examples for complex tasks
- CoT (Lesson 4): add "let's think step by step" for tasks that need reasoning
- Debugging (Lesson 5): change one thing at a time, verify with test cases
The task-specific strategies in this lesson are targeted optimizations layered on top of those general principles:
- Code generation → emphasize input/output types, edge cases, what not to do
- Document writing → emphasize the audience's background, structure templates, tone
- Data extraction → emphasize field definitions, missing-value handling, few-shot examples
Recap
Different tasks need different prompt strategies:
- Code generation: nail down input/output types, edge cases, code style, dependency limits; use examples to clarify special cases
- Document writing: define the audience's background and technical level, provide a structure template, control the level of detail and the tone
- Data extraction: define the allowed values for each field, say how to handle missing values, use few-shot to standardize the format, add data validation
These strategies build on the general skills from earlier lessons (the four elements, few-shot, CoT, debugging) — they're optimizations for specific task types. In practice, pick the combination that fits the task in front of you.
Course wrap-up
You've finished all six lessons of Prompt Engineering Basics:
- Understood what a prompt really is and why it matters
- Mastered the four core elements of a prompt
- Learned to guide the AI with examples using few-shot
- Applied chain-of-thought to make the AI show its reasoning
- Built a systematic routine for debugging and improving prompts
- Learned the specific strategies for different task types
Now you have the full toolbox. The next step is practice: pick a real task from your own work, apply these techniques, watch what happens, and keep refining. Prompt engineering is a skill you sharpen by doing — the coursework ends here, but the real learning starts when you put it to use.