Lesson 04 Advanced Prompting
Theme:

Lab Guide

Three exercises: chain-of-thought, few-shot classification, and XML extraction.

Setup: Open lab.pdf. Make sure your .env has ANTHROPIC_API_KEY. Run the setup cell before starting.

📥 Lab Notebooks

⬇ lab.pdf ⬇ lab_answer.pdf

Exercise 1 — Chain-of-Thought

EX 1Compare CoT vs direct answer on a multi-step word problem

Use the word problem below (provided in the notebook). Implement two functions: one that asks Claude for a direct answer, and one that uses chain-of-thought reasoning. Run each 3 times and compare accuracy and consistency.

The word problem

A train leaves City A at 9:00 AM traveling at 75 mph toward City B.
A second train leaves City B at 10:30 AM traveling toward City A at 55 mph.
The distance between City A and City B is 280 miles.
At what time do the two trains meet, and how far from City A is the meeting point?

Expected answer: They meet at 12:15 PM, approximately 243.75 miles from City A.

What to implement

Self-check: If CoT produces a wrong answer, read the reasoning trace. Where does the error occur? Can you fix it with a better trigger phrase?

Exercise 2 — Few-Shot Classifier

EX 2Classify customer support tickets by urgency

Build a few-shot classifier for ticket urgency. The 4 urgency levels are:

Step 1: Build FEW_SHOT_EXAMPLES

Write a string with 4 labeled examples — one per urgency level. Each example should follow this format:

Ticket: [ticket text]
Urgency: [CRITICAL|HIGH|MEDIUM|LOW]

Step 2: Implement classify_ticket()

The function must:

Step 3: Test on provided tickets

The notebook provides 6 test tickets with expected labels. Run your classifier and check accuracy. A good few-shot prompt should achieve at least 5/6 correct.

Tip: If accuracy is low, review your few-shot examples. Are they clearly representative of each level? Is the format perfectly consistent?

Exercise 3 — XML Extraction

EX 3Extract structured data from meeting notes using XML tags

Use the meeting notes document provided in the notebook. Implement extract_with_xml(document_text) which uses XML tags to structure the prompt and returns a JSON string.

Required XML structure

<instructions>
  Extract structured information from the document below.
  Return JSON matching the schema in <output_format>.
  Output only valid JSON — no markdown fences, no explanation.
</instructions>

<document>
  [meeting notes here]
</document>

<output_format>
  {
    "meeting_date": "string",
    "attendees": ["string"],
    "action_items": [{"owner": "string", "task": "string", "deadline": "string or null"}],
    "decisions": ["string"]
  }
</output_format>

Validation

The notebook validates your output with json.loads() and checks for:

Self-check: Count the action items in the raw meeting notes. Did your extraction find all of them? If not, what's in the notes that your prompt might have missed?