Advanced Prompting
Chain-of-thought, few-shot examples, system prompts, XML structure, and prompt chaining.
Introduction
Lesson 03 established the RTCF framework — the foundation for clear, specific prompts. This lesson builds on that foundation with five advanced techniques that unlock Claude's more sophisticated capabilities: chain-of-thought reasoning, few-shot learning, system prompt design, XML-structured prompts, and prompt chaining.
Each technique addresses a different class of problem. Understanding when to reach for each one — and how to combine them — is the mark of a production-grade prompt engineer.
Chain-of-Thought Prompting
Chain-of-thought (CoT) prompting instructs the model to produce explicit intermediate reasoning steps before arriving at a final answer. The canonical trigger phrase is "Let's think step by step.", though many variations work.
Why it improves reasoning
LLMs generate text token by token. When forced to write out intermediate steps, each token in the reasoning chain becomes evidence the model can condition on for subsequent tokens. This is functionally similar to a human using scratch paper — it reduces the cognitive load on any single "decision" in the reasoning chain.
Wei et al. (2022) demonstrated that CoT prompting on the GSM8K benchmark (elementary math word problems) improved accuracy from roughly 18% (direct answer) to 57% with standard CoT. The gains are largest for models at or above 100B parameters.
When to use CoT
- Multi-step arithmetic or algebra
- Logic puzzles with multiple constraints
- Code debugging (trace what each line does)
- Medical or legal reasoning where steps must be auditable
- Any task where intermediate verification improves final accuracy
CoT is generally not needed for simple factual recall, clear-cut classification, or latency-sensitive tasks — CoT produces more tokens, increasing cost and response time.
Zero-shot vs few-shot CoT
Zero-shot CoT: Append "Let's think step by step." to the prompt. Requires no examples.
Few-shot CoT: Provide 2–4 examples where the full reasoning chain is written out. More reliable for specialized domains where step format matters.
Few-Shot Prompting
Few-shot prompting provides worked examples in the prompt itself, allowing the model to infer the task pattern without fine-tuning. The model generalizes from examples in-context.
Example selection criteria
- Representativeness: Cover the output space. For a 4-class classifier, include at least one example per class.
- Diversity: Avoid near-duplicate examples. If all your POSITIVE examples are about food, the model may over-fit to food sentiment.
- Correctness: Wrong labels produce wrong outputs. Verify every example.
- Format consistency: Every example must use exactly the same input-output format. One deviation confuses the pattern.
- Difficulty calibration: Include easy, medium, and hard examples.
How many examples?
The 3–8 rule of thumb is a reasonable starting point, but the optimal number depends on task complexity. Simple binary classification may work with 2 examples; a 10-class taxonomy may need 3 examples per class (30 total). Beyond 8–10 examples, context window cost increases while marginal accuracy gains diminish rapidly.
System Prompts
The system prompt is a special message sent before the conversation begins. It establishes the model's persona, defines hard behavioral constraints, and sets persistent formatting rules that apply to every turn in the conversation.
Structure of a good system prompt
- Persona: Who Claude is. "You are an expert Python developer working on a financial data platform."
- Behavioral constraints: What Claude must always or never do.
- Response format: How every answer should be structured. "Answer in markdown. Keep responses under 400 words unless the user explicitly asks for more."
- Scope limits: What topics are in and out of bounds.
XML Tags (Claude-Specific)
Claude handles XML tags exceptionally well for separating different parts of a complex prompt, because it was pre-trained on large volumes of XML-formatted technical documentation.
When to use XML tags
- When the prompt contains both instructions and content — wrap the document in
<document>tags to prevent the model from treating it as instructions - When providing few-shot examples — wrap each in
<example>tags - When specifying output schemas — use
<output_format>tags - For long prompts with multiple distinct sections
Recommended tag vocabulary
| Tag | Purpose |
|---|---|
<document> | Input text to be processed (not instructions) |
<instructions> | Task instructions |
<examples> / <example> | Few-shot examples |
<output_format> | Schema or format specification |
<context> | Background information |
Prompt Chaining
Prompt chaining decomposes a complex task into sequential sub-tasks, where the output of each step becomes the input to the next.
When to chain
- When a single prompt consistently produces errors a human would catch mid-task
- When you need intermediate outputs for logging, human review, or branching logic
- When steps require different expertise or personas
- When a task is too long to fit in a single context window
# Extract → Verify → Format chain
facts = call_claude(f"Extract all action items from: {meeting_notes}")
checked = call_claude(f"Remove duplicates and sort by priority: {facts}")
formatted = call_claude(f"Format as a JIRA-compatible task list: {checked}")
Meta-Prompting
Meta-prompting means asking Claude to help write a better prompt. This is underused and genuinely effective. Claude can identify missing RTCF components, suggest few-shot examples, and generate XML-structured templates.
"I need a prompt that extracts structured data from legal contracts.
The output should be JSON. What information should I provide to get
reliable results? Draft a production-ready prompt."
Treat meta-prompting output as a starting point — always test and iterate on the generated prompt.
Further Reading
- Wei et al. (2022) — "Chain-of-Thought Prompting Elicits Reasoning in Large Language Models"
- Brown et al. (2020) — "Language Models are Few-Shot Learners" (GPT-3 paper, Section 3)
- Anthropic: Using XML Tags
- Wang et al. (2022) — "Self-Consistency Improves Chain of Thought Reasoning in Language Models"