Q&A
What is prompt engineering?
Overview
LLMs have fixed weights after training, but their behaviour is controlled at inference time entirely through the input context. Prompt engineering is the discipline of crafting that input to steer the model toward the desired output. It's part art, part science: informed by knowledge of how models work (attention, training data, tokenization) but validated empirically.
graph TD
A[Task to accomplish] --> B[Design prompt]
B --> C["System message: role + rules"]
B --> D["User message: clear instructions"]
B --> E["Few-shot examples (optional)"]
B --> F["Chain-of-thought instruction"]
C & D & E & F --> G[LLM output]
G --> H{Good enough?}
H -->|no| B
H -->|yes| I[Ship it]
style B fill:#1e1b4b,color:#fff
Core Techniques
1. Clear, Specific Instructions
Vague prompts yield vague outputs. Be explicit about the task, scope, format, and constraints:
❌ "Summarize this text."
✓ "Summarize the following article in 3 bullet points, each under 20 words,
focusing on the key business implications."
2. Role / Persona Assignment
Setting a system message that gives the model a role improves domain-appropriate responses:
System: "You are an expert Python developer. When asked about code, provide
working, production-quality examples with error handling."
3. Few-Shot Prompting
Provide examples of the desired input-output format:
Q: Classify sentiment: "Great product!" → Positive
Q: Classify sentiment: "Terrible service." → Negative
Q: Classify sentiment: "It was okay." → ?
A few examples dramatically improve accuracy on classification, transformation, and formatting tasks.
4. Chain-of-Thought (CoT)
For reasoning or multi-step problems, instruct the model to show its work:
"Solve the following problem step by step. Show your reasoning before giving
the final answer."
Or implicitly: just add "Let's think step by step." at the end. CoT dramatically improves performance on maths, logic, and planning.
5. Output Format Specification
Specify the exact output format, especially for programmatic parsing:
"Return a JSON object with keys: 'summary' (string), 'confidence' (float 0-1),
'tags' (list of strings). Output JSON only, no markdown."
6. Delimiter and Structure
Use delimiters (XML tags, triple backticks, ---) to separate instructions from data, preventing prompt injection and confusion:
Summarize the text enclosed in <article> tags.
<article>
{user-supplied article text}
</article>
7. Zero-Shot, One-Shot, Few-Shot
- Zero-shot - describe the task, no examples.
- One-shot - one example.
- Few-shot - a handful of examples (typically 3–8). More shots generally improve accuracy until the context window fills.
Advanced Techniques
Chain-of-Thought + Self-Consistency
Generate multiple chain-of-thought reasoning paths and take the majority vote among answers - reduces variance on reasoning tasks.
ReAct (Reason + Act)
Interleave reasoning ("Thought") with tool calls ("Action") and observations for agentic tasks:
Thought: I need to find the current stock price.
Action: search("AAPL stock price today")
Observation: 92.
Thought: Now I can answer.
Answer: Apple's current stock price is approximately 92.
Meta-Prompting / Generated Knowledge
Ask the model to generate relevant background knowledge before answering, reducing hallucination on knowledge-intensive tasks.
Prompt Injection and Security
When user input is incorporated into prompts, prompt injection attacks can hijack the model's instructions. Mitigations: use structured delimiters (XML tags), validate/sanitize user inputs, prefer system-level instructions that the model is trained to respect.
Iterative Refinement Process
- Write a baseline prompt.
- Evaluate outputs on a representative set of examples.
- Identify failure modes (wrong format, hallucinations, off-topic).
- Add constraints, examples, or clarifications.
- Re-evaluate. Repeat.
Why This Matters in Interviews
Prompt engineering is a practical skill tested in AI/ML and product roles. Strong answers: name specific techniques (few-shot, CoT, role assignment, format specification), explain why they work (examples ground the output distribution, CoT externalizes reasoning), know when to use each, and raise prompt injection as a security concern. The empirical mindset - iterate and measure - is as important as knowing the techniques.
Common Pitfalls
- Vague instructions that leave too much ambiguity.
- Ignoring the system message - it's often more influential than the user turn.
- Conflating prompt engineering with fine-tuning (prompting changes inputs, not weights).
- Not specifying output format for programmatic uses.
- Forgetting that temperature and top-p settings also control output - prompts and sampling parameters work together.
References
- OpenAI prompt engineering guide: https://platform.openai.com/docs/guides/prompt-engineering
- Anthropic prompt engineering overview: https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/overview