05 Formatting Output And Speaking For Claude
Chapter 5: Formatting Output and Speaking for Claude
Setup
Run the following setup cell to load your API key and establish the get_completion helper function.
Lesson
Claude can format its output in a wide variety of ways. You just need to ask for it to do so!
One of these ways is by using XML tags to separate out the response from any other superfluous text. You've already learned that you can use XML tags to make your prompt clearer and more parseable to Claude. It turns out, you can also ask Claude to use XML tags to make its output clearer and more easily understandable to humans.
Examples
Remember the 'poem preamble problem' we solved in Chapter 2 by asking Claude to skip the preamble entirely? It turns out we can also achieve a similar outcome by telling Claude to put the poem in XML tags.
Why is this something we'd want to do? Well, having the output in XML tags allows the end user to reliably get the poem and only the poem by writing a short program to extract the content between XML tags.
An extension of this technique is to **put the first XML tag in the assistant turn. When you put text in the assistant turn, you're basically telling Claude that Claude has already said something, and that it should continue from that point onward. This technique is called "speaking for Claude" or "prefilling Claude's response."
Below, we've done this with the first <haiku> XML tag. Notice how Claude continues directly from where we left off.
Claude also excels at using other output formatting styles, notably JSON. If you want to enforce JSON output (not deterministically, but close to it), you can also prefill Claude's response with the opening bracket, {}.
Below is an example of multiple input variables in the same prompt AND output formatting specification, all done using XML tags.
Bonus lesson
If you are calling Claude through the API, you can pass the closing XML tag to the stop_sequences parameter to get Claude to stop sampling once it emits your desired tag. This can save money and time-to-last-token by eliminating Claude's concluding remarks after it's already given you the answer you care about.
If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the Example Playground.
Exercise 5.1 - Steph Curry GOAT
Forced to make a choice, Claude designates Michael Jordan as the best basketball player of all time. Can we get Claude to pick someone else?
Change the PREFILL variable to compell Claude to make a detailed argument that the best basketball player of all time is Stephen Curry. Try not to change anything except PREFILL as that is the focus of this exercise.
❓ If you want a hint, run the cell below!
Exercise 5.2 - Two Haikus
Modify the PROMPT below using XML tags so that Claude writes two haikus about the animal instead of just one. It should be clear where one poem ends and the other begins.
❓ If you want a hint, run the cell below!
Exercise 5.3 - Two Haikus, Two Animals
Modify the PROMPT below so that Claude produces two haikus about two different animals. Use {ANIMAL1} as a stand-in for the first substitution, and {ANIMAL2} as a stand-in for the second substitution.
❓ If you want a hint, run the cell below!
Congrats!
If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!
Example Playground
This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses.