PROMPT ENGINEERING
A Comprehensive Guide to Modern Prompting Techniques
1. Introduction
Prompt Engineering is the practice of designing,
structuring, testing, and optimizing instructions given to an AI model to
produce accurate, relevant, consistent, safe, and useful outputs.
A strong prompt commonly combines:
·
Role and perspective
·
Context and background
·
Task
·
Input
·
Constraints
·
Output format
·
Quality criteria
ROLE + CONTEXT + TASK + INPUT +
CONSTRAINTS + OUTPUT FORMAT + QUALITY CRITERIA
2. Zero-Shot Prompting
Zero-shot prompting asks the model to perform a task without
providing examples. The model relies on its existing knowledge and the clarity
of the instruction.
Classify the following review as
Positive, Negative, or Neutral:
"The laptop has excellent performance but poor battery life."
3. One-Shot Prompting
One-shot prompting provides one example before the actual
task. The example establishes the expected behavior, terminology, style, or
format.
Classify the sentiment.
Example:
Review: "The product is excellent and arrived early."
Classification: Positive
Now classify:
Review: "The product stopped working after two days."
Classification:
4. Few-Shot Prompting
Few-shot prompting provides multiple examples. It is useful
when the expected behavior is specialized or difficult to describe with
instructions alone.
Example 1:
Input: Python Developer
Output: Software Development
Example 2:
Input: AWS Solutions Architect
Output: Cloud Architecture
Example 3:
Input: Data Scientist
Output: Data Science
Now classify:
Input: Kubernetes Engineer
Output:
5. Structured Output Prompting
Structured output makes model responses predictable and
machine-readable. It is useful for APIs, databases, Python applications,
FastAPI, RAG pipelines, and agents.
Return the candidate information using
this JSON structure:
{
"name": "",
"skills": [],
"experience_years": 0,
"current_role": "",
"location": ""
}
6. Chain-of-Thought Prompting
Chain-of-Thought (CoT) prompting encourages multi-step
problem solving. It can help with mathematics, logic, planning, and complex
classification. In production, concise rationale or verification is often
preferable to requesting private internal reasoning.
A product costs $100.
It receives a 20% discount and then an additional 10% discount.
Calculate the final price and explain the key calculation steps.
7. Tree of Thoughts (ToT)
Tree of Thoughts conceptually explores multiple candidate
reasoning paths and selects a promising solution. It can be useful for complex
planning, puzzles, and search problems, with additional computation and
latency.
Problem
|
+-- Approach A -- A1 / A2
+-- Approach B -- B1 / B2
+-- Approach C -- C1 / C2
|
Best solution
8. RAG-Based Prompting
Retrieval-Augmented Generation supplies external or private
knowledge to the model. A retriever finds relevant documents, which are
inserted into the prompt as context.
You are an enterprise support assistant.
Answer the user's question using ONLY the information
provided in the Context.
If the answer cannot be found in the Context,
say: "I don't have enough information to answer this."
Context:
{retrieved_documents}
Question:
{user_question}
9. Guardrail Prompting
Guardrails constrain model behavior around safety, privacy,
hallucination, confidential information, topic restrictions, and output
requirements. Prompt rules should be supplemented with application-level
controls.
Rules:
1. Do not invent product information.
2. Do not disclose confidential information.
3. If information is unavailable, say so.
4. Keep responses professional.
10. Temperature and Generation Controls
Temperature is a generation parameter rather than a
prompting technique. Lower values generally favor consistency, while higher
values generally allow more variation. Availability depends on the model/API.
Typical tendency:
Data extraction -> Low
Classification -> Low
Summarization -> Low
Support -> Low-Moderate
Brainstorming -> Moderate-High
Creative writing -> Higher
11. Role / Role-Play Prompting
Role prompting gives the model a professional perspective,
communication style, or audience level. It helps frame the response but does
not guarantee expertise or correctness.
You are a senior technical recruiter
specializing in US technology recruitment.
Analyze the candidate resume against the job description.
Identify matching skills, missing skills, experience, and overall fit.
12. Constraint-Based Prompting
Constraint prompting explicitly defines what the model must
and must not do. It is valuable when predictable output is required.
Write a technical explanation of RAG.
Constraints:
- Maximum 500 words
- Use simple English
- Include one architecture example
- Do not use marketing language
- Do not make unsupported claims
13. Prompt Chaining
Break a complex workflow into smaller prompts and stages.
Resume → Extract Information → Extract
Skills → Analyze JD → Match → Assess Fit → Generate Summary
14. Self-Consistency Prompting
Generate multiple candidate solutions and compare them to
identify the most consistent result.
Problem → Solution 1 / Solution 2 /
Solution 3 → Compare → Final Answer
15. ReAct Prompting
ReAct means Reason + Act. The model can decide when to use
search, APIs, databases, Python, or other tools and then continue using the
observations.
Question → Decide → Tool → Observe →
Decide → Tool → Final Answer
16. Least-to-Most Prompting
Solve complex tasks progressively by starting with simpler
subproblems and using their results to solve subsequent problems.
Identify subproblems → Solve simple
parts → Build on results → Final solution
17. Task Decomposition
Break a large task into requirements, components, data
flows, security, scalability, and deployment steps.
Large task → Requirements → Components →
Data Flow → Security → Scalability → Deployment
18. Iterative Refinement
Create a draft, review it, identify weaknesses, and produce
an improved version.
Draft → Review → Identify Problems →
Improve → Final
19. Critique and Revision
Use a review pass to assess accuracy, completeness,
security, performance, and maintainability before revision.
Generate → Critique → Revise → Validate
20. Meta-Prompting
Ask the model to design, improve, or evaluate a prompt
rather than directly performing the target task.
Goal → Prompt requirements → Generated
prompt → Test → Improve
21. Delimiter-Based Prompting
Use clear boundaries to separate instructions from
documents, code, emails, or retrieved content.
--- DOCUMENT START ---
{document}
--- DOCUMENT END ---
22. Contextual Prompting
Provide relevant background, audience, environment, and
success criteria so the model understands the situation.
Context + Task + Audience + Success
Criteria → Answer
23. Negative Prompting
Specify unwanted behaviors or content. Positive
specifications are often preferable to excessively long negative lists.
Do not invent facts; do not use
marketing language; do not include unrelated content.
24. Prompt Injection Awareness
Treat external content such as retrieved documents and web
pages as untrusted data, not instructions. Prompts should be supplemented by
technical security controls.
Instructions = trusted; Retrieved
content = untrusted data
25. Prompt Evaluation
Evaluate prompts against representative datasets for
accuracy, relevance, factuality, format compliance, cost, latency, and safety.
Prompt A/B/C → Evaluation Dataset →
Metrics → Best Version
26. Prompt Versioning
Treat prompts as application logic: version, test, document,
review, and measure changes.
prompt_v1 → prompt_v2 → prompt_v3 →
regression evaluation
27. Prompt Engineering for RAG Applications
A production RAG prompt commonly combines role, task,
retrieved context, grounding rules, delimiters, and structured output.
SYSTEM ROLE
You are an enterprise knowledge assistant.
CONTEXT
--- START CONTEXT ---
{context}
--- END CONTEXT ---
QUESTION
{question}
RULES
1. Use only information supported by the context.
2. Do not invent facts.
3. If the context does not contain the answer, say so.
OUTPUT
{
"answer": "...",
"sources": []
}
28. Prompt Engineering for AI Agents
Agents may use search, databases, APIs, Python, browsers,
and other tools. Prompting becomes part of agent orchestration.
ROLE: You are a research assistant.
OBJECTIVE: Find accurate information.
TOOLS: Web Search, Python.
RULES:
1. Use search for current information.
2. Use Python for calculations.
3. Do not fabricate results.
4. Verify important information.
STOP CONDITION: Return the answer when sufficient evidence is collected.
29. Context Engineering
Prompt engineering asks how to instruct the model. Context
engineering asks what information should be supplied to the model at the right
time.
System Instructions + Relevant Context +
History + Retrieved Knowledge + Tool Results + Application State → LLM
30. Best Practices
·
Be specific about the role, task, audience, and
expected result.
·
Provide relevant context rather than maximum
context.
·
Use examples when the desired behavior is
difficult to describe.
·
Define output structure, especially for
programmatic applications.
·
Separate instructions from untrusted data using
delimiters.
·
Use validation and evaluation instead of blindly
trusting model output.
·
Keep prompts as simple as the task allows.
·
Version and test important production prompts.
31. Common Mistakes
Vague instructions
Replace 'Make it better' with explicit style, audience,
length, and content requirements.
Conflicting instructions
Avoid contradictory requirements such as 'be extremely
detailed' and 'stay under 50 words'.
No output format
Define a schema or use structured-output capabilities when
applications require predictable data.
Blind trust
LLMs can hallucinate; use retrieval, validation,
verification, evaluation, and human review where appropriate.
Prompts as security controls
Prompts should not replace authorization, access control,
data isolation, or other security mechanisms.
Too much context
Irrelevant context can increase cost, latency, and
confusion.
32. Prompting Techniques at a Glance
|
Technique |
Main Purpose |
Typical Use |
|
Zero-shot |
Direct task |
Simple
classification |
|
One-shot |
Teach one
example |
Formatting |
|
Few-shot |
Demonstrate
patterns |
Specialized
classification |
|
Structured
output |
Predictable
response |
APIs / JSON |
|
Chain-of-Thought |
Multi-step
reasoning |
Complex
problems |
|
Tree of
Thoughts |
Explore
alternatives |
Planning /
search |
|
RAG |
Ground
answers |
Enterprise
knowledge |
|
Guardrails |
Control
behavior |
Safety /
compliance |
|
Temperature |
Control
variability |
Creativity /
consistency |
|
Role
prompting |
Establish
perspective |
Domain tasks |
|
Constraints |
Control
behavior/output |
Production
workflows |
|
Prompt
chaining |
Break complex
tasks |
AI pipelines |
|
Self-consistency |
Compare
solutions |
Reasoning |
|
ReAct |
Reason +
tools |
AI agents |
|
Decomposition |
Break tasks
down |
Large
workflows |
|
Iterative
refinement |
Improve
output |
Writing /
code |
|
Critique
& revision |
Find
weaknesses |
Quality
improvement |
|
Meta-prompting |
Generate
prompts |
Prompt
optimization |
|
Delimiters |
Separate
data/instructions |
RAG /
documents |
|
Contextual
prompting |
Provide
background |
Domain tasks |
|
Evaluation |
Measure
quality |
Production
systems |
|
Versioning |
Manage
changes |
Enterprise AI |
33. Practical Prompt Framework
A useful framework is R + C + T + C + O:
R — Role: You are
a senior Python developer.
C — Context: The
application is a FastAPI service running on AWS.
T — Task: Analyze
the API error.
C — Constraints: Do
not assume missing information.
O — Output: Return
JSON containing root_cause and recommended_fix.
34. End-to-End Example: Technical Recruitment
This example combines role prompting, context, constraints,
structured output, and grounding.
You are a senior technical recruiter
specializing in US technology recruitment.
Analyze the candidate resume against the supplied job description.
Identify:
1. Required skills that match
2. Required skills that are missing
3. Years of relevant experience
4. Relevant domain experience
5. Potential concerns
Rules:
- Do not invent information.
- Consider only information present in the supplied documents.
- Distinguish between explicit and inferred skills.
Return JSON:
{
"match_score": 0,
"matching_skills": [],
"missing_skills": [],
"experience_summary":
"",
"concerns": [],
"recommendation":
""
}
35. Evolution of Prompt Engineering
·
Stage 1 — Simple Prompting: What is RAG?
·
Stage 2 — Instruction Prompting: Explain RAG to
a beginner using an example.
·
Stage 3 — Few-Shot Prompting: Provide examples
and ask the model to perform the same task.
·
Stage 4 — Structured Prompting: Return the
result in a defined structure.
·
Stage 5 — RAG: Answer using retrieved context.
·
Stage 6 — Tool-Augmented Prompting: Use tools
when current information is required.
·
Stage 7 — Agentic Systems: Plan → Act → Observe
→ Decide → Respond.
·
Stage 8 — Context Engineering: Combine
instructions, relevant context, memory, tools, retrieved knowledge, and
application state.
36. Conclusion
Prompt engineering is a foundational skill for Generative
AI. The most important techniques include zero-shot, one-shot, few-shot,
structured output, Chain-of-Thought, Tree of Thoughts, RAG prompting,
guardrails, role prompting, constraints, prompt chaining, self-consistency,
ReAct, decomposition, iterative refinement, critique and revision,
meta-prompting, delimiters, contextual prompting, evaluation, and versioning.
The goal is not to
create increasingly complicated prompts. The real skill is knowing which
technique to use, when to use it, and when not to use it.
Prompt engineering is
the art of communicating intent to an AI model; AI engineering is the
discipline of building a reliable system around that model.
No comments:
Post a Comment