AI Personal Learning
and practical guidance

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:

Input:
Prompt template
User: Write a haiku about {{ANIMAL}}. Put it in the  tag.
+
Input {{ANIMAL}}
haiku
=
Prompt after replacement
User: Write a haiku about rabbits. Put it in the  tag.

Output:
Claude's reply
.
Fluffy and fluffy, with his nose in motion
Hopping across the verdant fields.
The gentle grace of a rabbit.

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  XML tags. Notice how Claude continues directly from where we stopped.

Example:

Input:
Prompt template
User: Write a haiku about {{ANIMAL}}. Put it in the  tag.
Helper: 
+
Type {{ANIMAL}}
Cat
=
Prompt after substitution
User: Write a haiku about Cat. Put it in the  tag.
Helper: 

Output:
Claude's response
The grace and poise of a cat that
Purring softly by the fireplace.
The mysterious cat.
</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:

Input:
Prompt template
User: Write a haiku about {{ANIMAL}}. Use JSON format with keys "first_line", "second_line" and "third_line".
Helper: {
+
Type {{ANIMAL}}
Cat
=
Prompt after substitution
User: Write a haiku about Cat. Use JSON format with keys "first_line", "second_line" and "third_line".
Helper: {

Output:
Claude's response
"first_line": "The grace and poise of a cat,",
"second_line": "Purring softly beside me,",
"third_line": "Charming cat."
}

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

Example:

Input:
Prompt Template
User: Hey Claude. this is an email:{{EMAIL}}. Please make this email more {{ADJECTIVE}} and write the new version in the  XML tag.
Helper: 
+
Enter {{EMAIL}}
Hi Zack, just pinging you for a quick update on that prompt you were supposed to write.
+
Enter {{ADJECTIVE}}
olde english
+ Type {{ADJECTIVE}} olde english
Replace the prompt
User: Hey Claude. this is an email: Hi Zack, just pinging you for a quick update on that prompt you were supposed to write.. Please make this email more olde english. and write the new version in the  XML tag.
Helpers: 

Output:
Claude's response
Dear Zack.

I respectfully request that you quickly update me on the prompt you were commissioned to write. Please let me know the progress of this matter and I am awaiting your response with bated breath.

Sincerely.
Your humble servant
</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:

Input:
Prompt Template
User: Who is the best basketball player of all time? Please pick a specific player.
ASSISTANT: The greatest basketball player of all time is Stephen Curry because:

OUTPUT:
Claude's response
- Widely regarded as the best shooter in the history of the NBA, he revolutionized the game with his amazing three-point range and accuracy. His shooting ability changed the way the game was played.

- He led the Golden State Warriors to 3 NBA Championships and was league MVP twice.He has been performing at an elite level for over a decade.

- His impact on the game goes beyond individual statistics. He changed the way teams and players approached the game, forcing defenses to adapt to his shooting ability.

- While players like Michael Jordan, LeBron James, and Kareem Abdul-Jabbar make a strong case, Curry's unprecedented shooting ability and the way he reshaped the game make him the strongest candidate for greatest player of all time.

❓ 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:

Input:
Prompt template
User: Write two haiku about {{ANIMAL}}. Place each poem in a  tag.
Helper: 
+
Enter {{ANIMAL}}
cats
=
Prompt after replacement
User: Write two haiku about cats. Place each poem in the  tag.
Helper: 

Output:
Claude's response
The grace and poise of a cat that
Purring softly in the sunlight.
Cats rule the realm.
</haiku


Whiskers twitching, eyes
alert to unseen prey.
cat, the hidden master.

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:

Input:
Prompt template
User: Write two haiku, one about {{ANIMAL1}} and the other about {{ANIMAL2}}. Place each haiku in a  tag.
+
Enter {{ANIMAL1}}
cat
+
Enter {{ANIMAL2}}
dog
=
Prompt after substitution
User: Write two haiku, one about cats and the other about dogs. Place each haiku in a  tag.

Output:
Claude's response
Here are two haiku, one about cats and the other about dogs, both wrapped in  tags:


The grace and poise of a cat.
Softly purring by the fire.
The mysterious cat.



Faithful companion.
Wagging tail. Endless energy.
Loyal and loving dog.
AI Easy Learning

The layman's guide to getting started with AI

Help you learn how to utilize AI tools at a low cost and from a zero base.AI, like office software, is an essential skill for everyone. Mastering AI will give you an edge in your job search and half the effort in your future work and studies.

View Details>
May not be reproduced without permission:Chief AI Sharing Circle " Anthropic Hints Engineering Course - Chapter 5: Formatted Output & Claude Expressions

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish