AI Personal Learning
and practical guidance
Beanbag Marscode1

Anthropic Hints Engineering Course - Chapter 5: Formatted Output & Claude Expressions

academic program

Claude can format its output in a number of ways. All you have to do is request it to do so!

One way to do this is to use XML tags to separate the response from other redundant text. You've learned that you can use XML tags to make prompts clearer and easier to be Claude Analysis. As it turns out, you can also ask Claude to Use XML tags to make the output clearer and more understandable to humansThe


typical example

Remember the "poetry preamble problem" we solved in Chapter 2 by asking Claude to skip the preamble altogether? It turns out that we can achieve a similar result by asking Claude to put poems into XML tags.

Example:

输入:
提示模板
用户:请写一首关于 {{ANIMAL}} 的俳句。将它放在 <haiku> 标签中。
+
输入 {{ANIMAL}}
兔子
=
替换后的提示
用户:请写一首关于兔子的俳句。将它放在 <haiku> 标签中。

输出:
Claude 的回复
<haiku>  
毛茸茸的,鼻子在动,  
在青翠的田野上跳跃,  
兔子的温柔优雅。  
</haiku>

Why did we do this? Because placing the output in XML tags allows the end user to reliably extract the content between the XML tags by writing a short program that gets only the poetry part.

An extension of this technique is to place the first XML tag after "Assistant:". When you place text after "Assistant:", you are essentially telling Claude that it has said something and should continue from that point. This technique is called "speaking instead of Claude" or "pre-populating Claude's reply".

Below we have used the first <haiku> XML tags. Notice how Claude continues directly from where we stopped.

Example:

输入:
提示模板
用户:请写一首关于 {{ANIMAL}} 的俳句。将其放在 <haiku> 标签中。  
助手:<haiku>
+
输入 {{ANIMAL}}
Cat
=
替换后的提示
用户:请写一首关于 Cat 的俳句。将其放在 <haiku> 标签中。  
助手:<haiku>

输出:
Claude 的回应
猫的优雅与姿态,  
在火炉旁轻柔地呼噜,  
神秘的猫。  
</haiku>

Claude also specializes in working with other output formats, notably JSON. If you want to force JSON output (not completely sure, but close), you can pre-populate Claude's response with "{" as the starting bracket.

Example:

输入:
提示模板
用户:请写一首关于 {{ANIMAL}} 的俳句。使用 JSON 格式,键为 "first_line"、"second_line" 和 "third_line"。  
助手:{
+
输入 {{ANIMAL}}
Cat
=
替换后的提示
用户:请写一首关于 Cat 的俳句。使用 JSON 格式,键为 "first_line"、"second_line" 和 "third_line"。  
助手:{

输出:
Claude 的回应
"first_line": "猫的优雅与姿态,",  
"second_line": "在我身旁轻柔地呼噜,",  
"third_line": "迷人的猫。"  
}

Below is an example with multiple input variables and output formatting specifications, all done using XML tags.

Example:

输入:
提示模板
用户:Hey Claude。这是一封电子邮件:<email>{{EMAIL}}</email>。请使这封邮件更加 {{ADJECTIVE}}。并在 <{{ADJECTIVE}}_email> XML 标签中编写新版本。  
助手:<{{ADJECTIVE}}_email>
+
输入 {{EMAIL}}
Hi Zack, just pinging you for a quick update on that prompt you were supposed to write.
+
输入 {{ADJECTIVE}}
olde english
=
替换后的提示
用户:Hey Claude。这是一封电子邮件:<email>Hi Zack, just pinging you for a quick update on that prompt you were supposed to write.</email>。请使这封邮件更加 olde english。并在 <olde english_email> XML 标签中编写新版本。  
助手:<olde english_email>

输出:
Claude 的回应
尊敬的 Zack,  

我恳请您迅速更新您被委托撰写的提示。请告知我此事的进展,我正屏息以待您的回复。  

真诚的,  
您的卑微仆人  
</olde english_email>

BONUS LESSON: To learn more about how to control Claude's output using the API, click the plus sign (➕) on the far left of this line!

If you are ready to try the chapter exercises, click on the links below. Scroll down if you want to play around with any of the examples in this lesson!

practice

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 let Claude choose someone else?

Modify the "Best Basketball Player" prompt in the highlighted template box and use the "Speaking on behalf of Claude" technique (add text after "Assistant:") to force Claude to make a detailed argument that the best basketball player of all time is the best basketball player of all time. Add text) to force Claude to make a detailed argument that the best basketball player of all time isStephen Curry. If your hint produces the correct answer, Claude's response will turn green.

Example:

输入:
提示模板
用户:谁是历史上最佳的篮球运动员?请选一个具体的球员。  
助手:历史上最伟大的篮球运动员是 Stephen Curry,因为:

输出:
Claude 的回应
- 他被广泛认为是 NBA 历史上最优秀的射手,凭借其惊人的三分球射程和准确性彻底改变了比赛。他的投篮能力改变了比赛的方式。  

- 他带领金州勇士队获得了 3 次 NBA 冠军,并且两次获得了联赛 MVP。他在超过十年的时间里一直保持着精英水平的表现。  

- 他对比赛的影响不仅仅体现在个人数据上。他改变了球队和球员对待比赛的方式,迫使防守方适应他的投篮能力。  

- 尽管像 Michael Jordan、LeBron James 和 Kareem Abdul-Jabbar 等球员有强有力的理由,但 Curry 前所未有的投篮能力以及他重塑比赛的方式使他成为历史上最伟大球员的最强候选人。

❓ If you need a prompt, click the plus sign (➕) on the far left side of this line.

Exercise 5.2 - Two Haikus

Modify the haiku prompt in the template box highlighted below and use XML tags so that Claude writes two haiku about some kind of animal, not just one. The beginning and end of each poem should be clearly labeled.

If your hint produces the correct answer, Claude's response will turn green.

Example:

输入:
提示模板
用户:请写两首关于 {{ANIMAL}} 的俳句。每首诗放在 <haiku> 标签中。  
助手:<haiku>
+
输入 {{ANIMAL}}
cats
=
替换后的提示
用户:请写两首关于猫的俳句。每首诗放在 <haiku> 标签中。  
助手:<haiku>

输出:
Claude 的回应
猫的优雅与姿态,  
在阳光下轻声呼噜,  
猫统治领域。  
</haiku>  

<haiku>  
胡须颤动,眼睛  
警觉地盯着看不见的猎物,  
猫,隐秘的主宰。  
</haiku>

Exercise 5.3 - Two Haiku, Two Animals

Modify the haiku prompt in the highlighted template box below so that Claude generates two haiku about two different animals.

Use {{ANIMAL1}} as the first alternative and {{ANIMAL2}} as the second alternative.

If your prompt produces a correct response, Claude's response will change to GREEN.

Example:

输入:
提示模板
用户:请写两首俳句,一首关于 {{ANIMAL1}},另一首关于 {{ANIMAL2}}。将每首俳句放在 <haiku> 标签中。
+
输入 {{ANIMAL1}}
猫
+
输入 {{ANIMAL2}}
狗
=
替代后的提示
用户:请写两首俳句,一首关于猫,另一首关于狗。将每首俳句放在 <haiku> 标签中。

输出:
Claude 的回应
这里是两首俳句,一首关于猫,另一首关于狗,均用 <haiku> 标签包裹:

<haiku>  
猫的优雅与姿态,  
在火旁柔声呼噜,  
神秘的猫。  
</haiku>  

<haiku>  
忠实的伙伴,  
摇摆的尾巴,无尽的活力,  
忠诚而爱的狗。  
</haiku>
May not be reproduced without permission:Chief AI Sharing Circle " Anthropic Hints Engineering Course - Chapter 5: Formatted Output & Claude Expressions
en_USEnglish