Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

These exercises practice retrieving what the Module 4 reading explained, after you finish Lab 4. They aren’t graded. Work each one on paper first, then check your answer, and confirm any escaping or parsing answer with a few lines of Python if you like. The hints and answers are in the collapsed blocks; open the first hint only when you are stuck, and an answer only after you have one of your own.

Worked example: laying out a prompt

A clerk wants to know whether the customer in ticket T03, “Rear tire goes flat after each ride,” can borrow a bicycle while theirs is repaired. The answer depends on the handbook’s section H04, “Loan bicycles.” Lay the prompt out in three steps:

  1. Put the data first: the handbook section in <documents>, with its <source>, and the ticket in its own <ticket> tag.

  2. Put the instructions next, referring to the data by name: “Answer from the handbook section only.”

  3. Put the question last.

<documents>
  <document index="1">
    <source>Handbook H04: Loan bicycles</source>
    <document_content>The shop has two loan bicycles. ...</document_content>
  </document>
</documents>
<ticket>Rear tire goes flat after each ride</ticket>
<instructions>Answer from the handbook section only, and quote the sentence you use.</instructions>
<question>Can this customer borrow a bicycle during the repair?</question>

The ... stands for the rest of section H04, which the real prompt includes in full.

Exercise P4.1 (LO-4.1, ★, about 10 minutes)

You have two manual sections and a question about a tire. Lay out your tags.

Hint 1

Which part should the model read last?

Hint 2

Group sources before instructions.

Answer

Put both sections in documents, followed by instructions and the question:

<documents>
  <document index="1">
    <source>Manual section 1: its title</source>
    <document_content>The first section's text</document_content>
  </document>
  <document index="2">
    <source>Manual section 2: its title</source>
    <document_content>The second section's text</document_content>
  </document>
</documents>
<instructions>Answer from these sections only, and say which section you used.</instructions>
<question>The question about the tire</question>

The sources come first and the question last, the order Anthropic’s best-practices page recommends for long prompts. Each section gets its own <document>, so the instructions can ask which one the answer used, and its <source> gives the answer something to cite.

Common mistakes: putting the question first, as you would in an email; merging both sections into one <document>, so no answer can say which section it relied on; and putting instructions inside a document’s tags, where they read as part of the manual.

Worked example: escaping a value by hand

The phrase Chain & brakes < $30 becomes Chain &amp; brakes &lt; $30 inside a text node. The parser reads it back as Chain & brakes < $30.

Escape & before < and >. If you replaced < with &lt; first, replacing & afterward would turn that into &amp;lt;, which parses back as &lt; instead of <. The $ stays as it is: it means nothing in XML, and Template.substitute doesn’t rescan the values it inserts.

Exercise P4.2 (LO-4.2, ★★, about 15 minutes)

Finish <ticket>TODO</ticket> for the value Cables < $12 & grips.

Hint 1

Which two characters does XML forbid as themselves in text?

Hint 2

Preserve the literal dollar sign.

Answer

<ticket>Cables &lt; $12 &amp; grips</ticket>.

The < would start a tag and the & would start an entity reference, so both are escaped. The dollar sign and the digits are ordinary characters in XML. Parsing the element and reading its text gives back Cables < $12 & grips exactly, which is the check.

Common mistakes: writing $$12, which is how a template’s own text escapes a dollar sign, not a value, so the model would see two dollar signs; escaping in the wrong order and getting &amp;lt;; and escaping the tags too, which gives &lt;ticket&gt;.

Worked example: turning a diff into a test

A diff between two versions of the work-order prompt shows one changed line:

-Parts: <the parts it needs>
+Parts: <at most three parts, separated by commas>

The new rule can be checked, so write the cases that check it, on both sides of each limit:

Response lineExpectedWhy
Parts: tube, tire leverpassTwo parts
Parts: tube, tire, rim tapepassThree parts, the limit
Parts: tube, tire, rim tape, valvefailFour parts
Parts: tube; tirefailThe wrong separator

The rationale goes in the version note: “Version 2 caps the parts list at three, because mechanics asked for short orders; the four cases above check the cap and the separator.”

Exercise P4.3 (LO-4.3, ★★, about 15 minutes)

A diff deletes “briefly” and adds “one line, at most 12 words.” Write a test and a rationale.

Hint 1

The new rule has two parts. Can a response break one and keep the other?

Hint 2

Count words and lines independently.

Answer

Reject a 13-word line and two 5-word lines. The rule trades open-ended brevity for an observable limit.

Add a passing case at the limit, a single line of exactly 12 words, so the test shows where the limit sits. The 13-word line breaks only the word limit. The two 5-word lines break only the line limit: they have 10 words in all, so a check that counts words across the whole response passes them. Decide, and write down, whether a trailing newline makes a second line.

The rationale: “briefly” means different lengths to different readers, and no code can check it. “One line, at most 12 words” can be counted in every response. The trade-off is that some answers may need more than 12 words, which the next evaluation should show.

Common mistakes: testing only the 13-word case, which leaves the line limit untested, and counting words over the whole response instead of per line.

Exercise P4.4 (LO-4.2, LO-4.3, ★★★, about 25 minutes)

Should a blank ticket count as a missing variable? State a policy and design cases for it.

Hint 1

Who has to fix each problem: the programmer who called the renderer, or the customer who filled in the form?

Hint 2

The distinction can belong in different layers.

Answer

Require the key in the renderer. Validate nonempty tickets before rendering, so no field and blank field get distinct messages.

ValuesCaught byMessage
No ticket keyThe rendererThe template needs a ticket field
ticket is ""The validatorThe ticket is blank
ticket is only spaces and a newlineThe validatorThe ticket is blank
ticket is "Pedal creaks when I stand up"NeitherThe prompt renders

A missing key is a bug in the calling code, often a caller written for another version of the template, so the programmer fixes it. A blank ticket is a data problem, so the form or the customer fixes it. Separate messages send each problem to the person who can fix it.

Common mistakes: having the renderer treat "" as missing, which hides which problem occurred; and skipping the blank check, so the model receives an empty <ticket></ticket> and has nothing to route.