Prompt Engineering

Advanced Prompting Techniques

Master advanced prompting strategies — chain-of-thought, few-shot, role prompting, self-consistency, and more.

Advanced Prompting Techniques

Beyond basic prompting, several advanced techniques can dramatically improve LLM output quality, accuracy, and reliability.

Zero-Shot Prompting

Asking the model to perform a task without any examples:

Classify the sentiment of this review as Positive, Negative, or Neutral:

"The food was decent but the service was incredibly slow. Won't be returning."

Sentiment:

Zero-shot works well for straightforward tasks where the model has seen similar patterns in training.

Few-Shot Prompting

Providing examples of the desired input-output pattern before asking the actual question:

Classify the sentiment:

Review: "Absolutely loved it! Best purchase ever."
Sentiment: Positive

Review: "It's okay, nothing special."
Sentiment: Neutral

Review: "Terrible quality, broke after one day."
Sentiment: Negative

Review: "The food was decent but the service was incredibly slow."
Sentiment:

How Many Examples?

ExamplesNameBest For
0Zero-shotSimple, well-defined tasks
1-3Few-shotFormat and pattern matching
4-8Many-shotComplex classification, nuanced tasks

Tips for few-shot examples:

  • Use diverse, representative examples
  • Cover edge cases
  • Keep examples consistent in format
  • Order can matter — put the most relevant examples last

Chain-of-Thought (CoT) Prompting

Ask the model to show its reasoning step by step. This dramatically improves accuracy on reasoning tasks.

Without CoT:

Q: A store has 15 apples. 8 are sold in the morning and 3 more arrive
   in the afternoon. How many apples are there at the end of the day?
A: 10

With CoT:

Q: A store has 15 apples. 8 are sold in the morning and 3 more arrive
   in the afternoon. How many apples are there at the end of the day?

Think step by step:

A: Let me work through this:
   1. Starting apples: 15
   2. After morning sales: 15 - 8 = 7
   3. After afternoon delivery: 7 + 3 = 10

   The store has 10 apples at the end of the day.

Simple CoT Trigger Phrases

  • "Think step by step"
  • "Let's work through this carefully"
  • "Show your reasoning"
  • "Break this down into steps"

Zero-Shot CoT

Simply adding "Let's think step by step" to any prompt:

"What is 17 × 23? Let's think step by step."

→ 17 × 23
  = 17 × 20 + 17 × 3
  = 340 + 51
  = 391

This single phrase improves math, logic, and reasoning tasks significantly.

Role Prompting

Assigning a specific persona or expertise to the model:

You are a senior security engineer at a FAANG company with 15 years
of experience in application security. You specialize in finding
vulnerabilities in web applications.

Review this authentication code and identify all security issues:

[code]

Effective Role Prompts

RoleBest For
"Expert in X"Deep technical analysis
"Teacher explaining to beginners"Simplified explanations
"Devil's advocate"Finding counterarguments
"Code reviewer at Google"High-quality code review
"Technical writer for API docs"Documentation

Self-Consistency

Generate multiple responses to the same prompt and take the majority answer. Useful for reasoning tasks where the model might err.

# Run the same prompt 5 times:
Prompt: "Is 17 a prime number? Think step by step."

Response 1: Yes (correct reasoning)
Response 2: Yes (correct reasoning)
Response 3: No (flawed reasoning)    ← outlier
Response 4: Yes (correct reasoning)
Response 5: Yes (correct reasoning)

Majority vote: Yes ✓

In practice, you can do this programmatically by setting temperature > 0 and calling the API multiple times.

Generated Knowledge Prompting

Ask the model to generate relevant knowledge before answering:

Step 1: Generate relevant facts about electric vehicles and
        environmental impact.

Step 2: Using those facts, answer: "Are electric vehicles truly
        better for the environment than gas cars?"

This two-step approach produces more informed, nuanced responses.

Prompt Chaining

Breaking a complex task into a sequence of simpler prompts, where each output feeds into the next:

Prompt 1: "Extract all the key claims from this article"
           → [List of claims]

Prompt 2: "For each claim, identify what evidence would be needed
            to verify it"
           → [Evidence requirements]

Prompt 3: "Rate each claim's credibility based on the evidence
            requirements. Use: Verified / Likely / Uncertain / Unlikely"
           → [Final analysis]

When to Chain vs Single Prompt

Use ChainingUse Single Prompt
Complex multi-step tasksSimple, focused tasks
Need intermediate validationOne-shot generation
Each step requires different expertiseConsistent format needed
Output quality is criticalSpeed matters more

Negative Prompting

Telling the model what NOT to do:

Explain quantum computing to a high school student.

DO NOT:
- Use jargon without defining it
- Include mathematical formulas
- Make it longer than 200 words
- Use condescending language like "simply put" or "it's easy"

ReAct (Reasoning + Acting)

Combining reasoning with actions in a structured format:

Question: What is the population of the capital of Australia?

Thought 1: I need to find the capital of Australia first.
Action 1: The capital of Australia is Canberra.
Thought 2: Now I need to find the population of Canberra.
Action 2: Canberra has a population of approximately 467,000.
Answer: The population of Canberra, the capital of Australia,
        is approximately 467,000.

This pattern is foundational for AI agents (covered in Module 7).

Comparison Table

TechniqueBest ForComplexity
Zero-shotSimple tasksLow
Few-shotPattern matching, classificationLow
Chain-of-thoughtMath, logic, reasoningMedium
Role promptingDomain expertiseLow
Self-consistencyHigh-accuracy answersMedium
Prompt chainingComplex multi-step tasksHigh
ReActTasks requiring tools/actionsHigh

Practice Exercises

  1. Use chain-of-thought to solve: "If a train travels at 60 mph for 2.5 hours, then 80 mph for 1.5 hours, what's the total distance?"
  2. Create a few-shot prompt to classify customer support tickets into categories
  3. Use role prompting to get a code review from different perspectives (security, performance, readability)

These techniques form the foundation for building reliable AI applications. The next topic covers reusable prompt patterns and frameworks.