DevFlow
AI Engineering Foundations/Prompt Engineering
Lesson

Prompt Engineering Techniques

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

25 min read
Layered prompt cards flowing through a structured orchestration system into high-quality outputs and measurable results.
Layered prompt cards flowing through a structured orchestration system into high-quality outputs and measurable results.

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.

typescript
16 lines
1// Bad prompt — ambiguous, under-specified
2const prompt = "Write code for a function"
3
4// Good prompt — precise role, task, constraints, output format
5const prompt = `
6You are a senior TypeScript engineer.
7Write a pure function that takes an array of numbers and returns
8the top-K values sorted descending.
9
10Requirements:
11- Use TypeScript generics
12- Handle edge cases (empty array, K > array length)
13- Include JSDoc comments
14
15Return 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.

text
3 lines
1You are a senior distributed systems engineer with 10 years of
2experience at companies like Google and Amazon. Review the
3following 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.

text
7 lines
1Classify the following error messages as either "network", "auth", or "data".
2
3Error: "ECONNREFUSED 127.0.0.1:5432" network
4Error: "JWT signature verification failed" auth
5Error: "Unexpected token at position 42" data
6
7Error: "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.

text
10 lines
1Analyze this codebase change and determine if it introduces a
2security vulnerability.
3
4Think 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?
9
10Then 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.

text
12 lines
1Extract the following entities from the text below.
2Return a JSON object matching this exact schema:
3
4{
5 "people": string[],
6 "organizations": string[],
7 "locations": string[],
8 "dates": string[]
9}
10
11Text: "In March 2024, Satya Nadella announced that Microsoft
12would invest $10B in OpenAI's San Francisco headquarters."
json
6 lines
1{
2 "people": ["Satya Nadella"],
3 "organizations": ["Microsoft", "OpenAI"],
4 "locations": ["San Francisco"],
5 "dates": ["March 2024"]
6}

The Prompt Engineering Toolkit

TechniqueBest ForComplexity
Role PromptingTone & expertise controlLow
Few-ShotFormat consistencyLow–Medium
Chain-of-ThoughtReasoning tasksMedium
Structured OutputParseable data extractionMedium
Self-ConsistencyHigh-stakes decisionsHigh
Constitutional AISafety & alignmentHigh

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.

typescript
14 lines
1// Underutilized system prompt
2const systemPrompt = "You are a helpful assistant."
3
4// Effective system prompt
5const systemPrompt = `
6You are a code review assistant specialized in TypeScript and React.
7
8Rules:
9- Focus on correctness, performance, and security
10- Suggest specific, actionable improvements
11- Always show the improved code, not just describe it
12- Do not comment on style unless it causes bugs
13- 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.