Prompt Engineering Techniques
Part of Prompt Engineering in the AI Engineering Foundations learning path.

What is Prompt Engineering?
Prompt engineering is the discipline of crafting inputs to language models to reliably produce desired outputs. It sits at the intersection of linguistics, cognitive science, and software engineering.
A well-engineered prompt is not just "asking nicely." It is a precise specification — analogous to writing a clean API contract.
1// Bad prompt — ambiguous, under-specified2const prompt = "Write code for a function"34// Good prompt — precise role, task, constraints, output format5const prompt = `6You are a senior TypeScript engineer.7Write a pure function that takes an array of numbers and returns8the top-K values sorted descending.910Requirements:11- Use TypeScript generics12- Handle edge cases (empty array, K > array length)13- Include JSDoc comments1415Return ONLY the function code, no explanation.16`Core Techniques
1. Role Prompting
Assign the model a specific role or persona. This activates relevant knowledge domains and constrains tone.
1You are a senior distributed systems engineer with 10 years of2experience at companies like Google and Amazon. Review the3following architecture diagram and identify single points of failure.Role prompting works because LLMs are trained on text written by specific types of people. Assigning a role shifts the probability distribution toward that person's writing style and knowledge.
2. Few-Shot Prompting
Provide input/output examples to demonstrate the exact format and reasoning style you want.
1Classify the following error messages as either "network", "auth", or "data".23Error: "ECONNREFUSED 127.0.0.1:5432" → network4Error: "JWT signature verification failed" → auth5Error: "Unexpected token at position 42" → data67Error: "SSL handshake timeout after 30s" →When to use it: When zero-shot prompting produces inconsistent formatting or the task requires a specific reasoning pattern.
3. Chain-of-Thought (CoT) Prompting
Force the model to reason step-by-step before producing the final answer. This dramatically improves accuracy on multi-step problems.
1Analyze this codebase change and determine if it introduces a2security vulnerability.34Think through it step by step:51. What data is being received?62. Is it sanitized before use?73. What operations are performed on it?84. Could an attacker control any of these inputs?910Then give a final verdict: SAFE or VULNERABLE with a reason.CoT prompting can increase accuracy on reasoning tasks by 30–50% compared to direct prompting. The model "shows its work," and the intermediate steps constrain subsequent tokens toward more logical conclusions.
4. Structured Output Prompting
When you need parseable output, specify the exact schema.
1Extract the following entities from the text below.2Return a JSON object matching this exact schema:34{5 "people": string[],6 "organizations": string[],7 "locations": string[],8 "dates": string[]9}1011Text: "In March 2024, Satya Nadella announced that Microsoft12would invest $10B in OpenAI's San Francisco headquarters."1{2 "people": ["Satya Nadella"],3 "organizations": ["Microsoft", "OpenAI"],4 "locations": ["San Francisco"],5 "dates": ["March 2024"]6}The Prompt Engineering Toolkit
| Technique | Best For | Complexity |
|---|---|---|
| Role Prompting | Tone & expertise control | Low |
| Few-Shot | Format consistency | Low–Medium |
| Chain-of-Thought | Reasoning tasks | Medium |
| Structured Output | Parseable data extraction | Medium |
| Self-Consistency | High-stakes decisions | High |
| Constitutional AI | Safety & alignment | High |
Common Anti-Patterns
Vague instructions: "Make it better" tells the model nothing. Always specify *what* better means — shorter, more formal, less jargon.
Over-constraining: Too many constraints create conflicting objectives. The model satisfies them in unpredictable order.
Ignoring the system prompt: The system prompt sets the frame for the entire conversation. Most developers underutilize it.
1// Underutilized system prompt2const systemPrompt = "You are a helpful assistant."34// Effective system prompt5const systemPrompt = `6You are a code review assistant specialized in TypeScript and React.78Rules:9- Focus on correctness, performance, and security10- Suggest specific, actionable improvements11- Always show the improved code, not just describe it12- Do not comment on style unless it causes bugs13- Be direct — no filler phrases like "Great question!"14`Key Takeaways
- Prompts are specifications. Treat them with the same rigor as code.
- Few-shot examples are the highest-leverage technique for format consistency.
- Chain-of-thought prompting is essential for any multi-step reasoning task.
- System prompts are underused — they are your most powerful lever.
- Iterate on prompts the same way you iterate on code: measure, change one thing, remeasure.