Skip to content
SDB
Generative AI

Chapter 05 · beginner · 25 min

Prompt Engineering

Systematic techniques to guide LLMs toward better outputs

Subhendu Datta BhowmikAI Tutorials

Why Prompt Engineering Matters

The same model can produce dramatically different outputs depending on how you ask. Prompt engineering is the practice of systematically designing inputs to guide model behavior without modifying weights.

Good prompting can:

  • Improve accuracy by 10–40% on reasoning tasks
  • Eliminate unwanted output formats
  • Reduce hallucinations through structured verification
  • Enable complex multi-step reasoning

It's also the cheapest intervention — zero training cost, instant iteration.

The System Prompt

The system prompt is the most powerful lever for shaping model behavior. It runs before every conversation turn.

Anatomy of an Effective System Prompt

1. ROLE & PERSONA     — Who is the model?
2. CONTEXT            — What domain/product is this?
3. TASK               — What does it do?
4. CONSTRAINTS        — What should it never do?
5. OUTPUT FORMAT      — How should responses be structured?
6. EXAMPLES (optional) — What does good look like?

Example: Customer Support Bot

You are a support agent for Acme Cloud Storage.
Your job is to help users troubleshoot storage, billing, and account issues.

Guidelines:
- Be concise and friendly. Use plain language, no jargon.
- Only answer questions related to Acme products.
- Never make up product features — say "I'll check on that" if unsure.
- If the issue needs engineering, respond: "I've escalated ticket #[auto-generated]."

Format: Use numbered steps for procedures. Lead with the solution, not the explanation.
System Prompt Best Practicespython
import anthropic

client = anthropic.Anthropic()

SYSTEM_PROMPT = """You are a Python code reviewer specializing in security and performance.

When reviewing code:
1. Identify security vulnerabilities (OWASP Top 10)
2. Note performance bottlenecks
3. Suggest specific, actionable fixes with code examples

Output format:
## Security Issues
- [SEVERITY: HIGH/MEDIUM/LOW] Issue description
  Fix: `corrected code snippet`

## Performance Issues
- Issue description
  Fix: `optimized code snippet`

## Summary
Overall assessment in 1–2 sentences.

If the code has no issues, say "No issues found" and briefly explain what it does correctly."""

code_to_review = """
def get_user(username):
    query = f"SELECT * FROM users WHERE username = '{username}'"
    return db.execute(query)
"""

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system=SYSTEM_PROMPT,
    messages=[
        {"role": "user", "content": f"Review this code:\n\n```python\n{code_to_review}\n```"}
    ],
)
print(response.content[0].text)

Few-Shot Prompting

Provide 2–5 examples of the task in the prompt to demonstrate the expected pattern. The model generalizes from your examples.

Zero-Shot vs Few-Shot

Zero-shot: describe the task only

Classify this review as positive, negative, or neutral:
Review: "The shipping was fast but the product broke after a week."
Classification:

Few-shot: show examples first

Classify reviews as positive, negative, or neutral:

Review: "Absolutely love this! Best purchase of the year."
Classification: positive

Review: "Arrived damaged and customer support never responded."
Classification: negative

Review: "It works as described. Delivery took a week."
Classification: neutral

Review: "The shipping was fast but the product broke after a week."
Classification:

Few-shot is especially powerful for:

  • Unusual output formats
  • Domain-specific classification schemes
  • Tasks where tone matters

Chain-of-Thought (CoT) Prompting

On reasoning tasks, prompting the model to think step-by-step dramatically improves accuracy.

Zero-Shot CoT

Simply add "Let's think step by step" or "Think through this carefully before answering."

Few-Shot CoT

Provide worked examples with reasoning:

Q: If a train travels at 60 mph and needs to cover 150 miles, how long will it take?
A: Let me work through this step by step.
   - Speed = 60 mph
   - Distance = 150 miles
   - Time = Distance / Speed = 150 / 60 = 2.5 hours
   The answer is 2.5 hours.

Q: A store has 48 apples. They sell 1/3 in the morning and 1/4 of the remainder in the afternoon. How many are left?
A: Let me work through this step by step.

Self-Consistency

Sample multiple CoT reasoning paths (temperature > 0) and take the majority answer. Improves accuracy by ~10% on math benchmarks.

Chain-of-Thought with Structured Reasoningpython
import anthropic
import json

client = anthropic.Anthropic()

def reason_and_answer(question: str) -> dict:
    """Use CoT to get a structured reasoning + answer."""
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        system="""You are a precise reasoning assistant. For every question:
1. Work through the problem step by step in <thinking> tags
2. Give your final answer in <answer> tags
3. Rate your confidence 0-100 in <confidence> tags

Example:
<thinking>
Step 1: Identify what's being asked...
Step 2: ...
</thinking>
<answer>The final answer</answer>
<confidence>85</confidence>""",
        messages=[{"role": "user", "content": question}],
    )

    text = response.content[0].text
    import re

    thinking = re.search(r'<thinking>(.*?)</thinking>', text, re.DOTALL)
    answer = re.search(r'<answer>(.*?)</answer>', text, re.DOTALL)
    confidence = re.search(r'<confidence>(.*?)</confidence>', text, re.DOTALL)

    return {
        "reasoning": thinking.group(1).strip() if thinking else "",
        "answer": answer.group(1).strip() if answer else text,
        "confidence": int(confidence.group(1).strip()) if confidence else None,
    }

result = reason_and_answer(
    "A snail climbs 3 feet during the day and slides back 2 feet at night. "
    "How many days does it take to climb a 10-foot wall?"
)
print(f"Answer: {result['answer']}")
print(f"Confidence: {result['confidence']}%")
print(f"Reasoning: {result['reasoning'][:200]}...")

Structured Output Prompting

For applications that parse model responses, force structured formats.

JSON Mode / Tool Use

Many APIs support JSON mode or function calling to guarantee valid JSON:

# Using tool use for guaranteed structured output
tools = [{
    "name": "extract_entities",
    "description": "Extract named entities from text",
    "input_schema": {
        "type": "object",
        "properties": {
            "people": {"type": "array", "items": {"type": "string"}},
            "organizations": {"type": "array", "items": {"type": "string"}},
            "locations": {"type": "array", "items": {"type": "string"}},
            "dates": {"type": "array", "items": {"type": "string"}}
        },
        "required": ["people", "organizations", "locations", "dates"]
    }
}]

XML Tags for Reliable Parsing

Claude reliably produces XML-tagged outputs that are easy to parse:

Extract the key information and wrap it:
<summary>...</summary>
<sentiment>positive|negative|neutral</sentiment>
<action_items><item>...</item></action_items>

Advanced Techniques

Role Prompting

Assigning a specific role measurably improves domain performance:

  • "You are a senior security engineer at a Fortune 500 company..."
  • "You are a Socratic tutor who never gives direct answers..."

Negative Constraints

Explicitly stating what NOT to do is often more effective than positive instructions:

  • "Do not use bullet points"
  • "Never make up citations. If you don't know, say so."
  • "Do not include disclaimers or caveats"

Temperature and Top-P

  • Factual tasks (extraction, classification, math): temperature=0
  • Creative tasks (brainstorming, writing): temperature=0.7–1.0
  • Code generation: temperature=0–0.3

Prompt Chaining

Break complex tasks into sequential prompts: summarize → analyze → generate. Each step's output feeds the next, reducing errors in single-step approaches.

Knowledge check

What does adding "Let's think step by step" to a prompt typically accomplish?

Summary

  • System prompts are the most powerful tool: define role, context, constraints, and output format
  • Few-shot prompting provides examples that teach the model the expected pattern
  • Chain-of-thought significantly improves reasoning by making the model work through problems step by step
  • Structured output (JSON, XML, tool use) ensures parseable responses for applications
  • Prompt injection is a real security risk when processing untrusted user content
  • Temperature controls randomness: low for factual tasks, high for creative ones

Next chapter covers Retrieval-Augmented Generation (RAG) — connecting LLMs to external knowledge for grounded, up-to-date responses.

Generative AI