Specifying the desired format

One of the simplest ways to control Claude’s output is to simply state the format you want. Claude can understand and follow instructions related to formatting, and format outputs such as:

  • JSON
  • XML
  • HTML
  • Markdown
  • CSV
  • Custom formats

For example, if you want Claude to generate a haiku in JSON format, you can use a prompt like this:

RoleContent
UserPlease write a haiku about a cat. Use JSON format with the keys “first_line”, “second_line”, and “third_line”.
Assistant (Claude’s response){
“first_line”: “Soft furry feline”,
“second_line”: “Napping in the warm sunlight”,
“third_line”: “Purring contentedly”
}

Prefilling Claude’s response

Another effective way to control Claude’s output format is to prefill the Assistant field with the beginning of the desired format. This technique is particularly useful when working with structured formats like JSON or HTML.

For example, to generate a recipe in JSON format:

RoleContent
UserPlease generate a recipe for chocolate chip cookies in JSON format.
Assistant (prefill){
Assistant (Claude’s response)“title”: “Chocolate Chip Cookies”,
“ingredients”: [
“2 1/4 cups all-purpose flour”,
“1 tsp baking soda”,
“1 tsp salt”,
“1 cup unsalted butter, softened”,
“3/4 cup granulated sugar”,
“3/4 cup brown sugar”,
“1 tsp vanilla extract”,
“2 large eggs”,
“2 cups semisweet chocolate chips”
],
“instructions”: [
“Preheat the oven to 375°F (190°C) and line baking sheets with parchment paper.“,
“In a medium bowl, whisk together the flour, baking soda, and salt. Set aside.“,
“In a large bowl, beat the butter and sugars with an electric mixer until light and fluffy, about 2-3 minutes.“,
“Beat in the vanilla and eggs one at a time until well combined.“,
“Gradually stir the flour mixture into the butter mixture until just combined.“,
“Fold in the chocolate chips.“,
“Drop rounded tablespoons of the dough onto the prepared baking sheets, about 2 inches apart.“,
“Bake for 9-11 minutes, until the edges are golden brown.“,
“Allow the cookies to cool on the baking sheets for 5 minutes before transferring to a wire rack to cool completely.”
],
“yield”: “About 3 dozen cookies”
}

By prefilling the response with the opening JSON curly brace, you give Claude a clear starting point for generating the recipe in the desired format. This also is another technique you can use to have Claude skip its friendly preamble text.


Providing examples

In addition to explicit instructions, providing examples of the desired output format can help Claude better understand your requirements. When including examples, make it clear that Claude should follow the formatting of the examples provided (otherwise Claude may pick up other details from the provided examples, such as content or writing style).

Here is an example prompt showcasing this technique:

RoleContent
UserYour task is to write a poem. Here are some examples of ideal formatting for the poem:

<poem>
Title: “Autumn Leaves”
Verse 1:
Crisp autumn leaves dance
In the gentle, chilly breeze
A colorful sight

Verse 2:
Red, orange, and gold
Painting the world with beauty
Before winter comes
</poem>

<poem>
Title: “Moonlit Night”
Verse 1:
Moonlight casts shadows
Across the tranquil garden
A peaceful retreat

Verse 2:Stars twinkle above
As crickets sing their nightsong
Nature’s lullaby
</poem>

Now, please write a poem about a sunset, following the formatting of the examples above.
Assistant (prefill)<poem>
Assistant (Claude’s response)Title: “Sunset Symphony”
Verse 1:
Fiery sun descends
Painting the sky with wonder
A masterpiece forms

Verse 2:
Pinks, purples, and reds
Blending in pure harmony
Day bids night hello
</poem>

Tips for better output control

  • Be as specific as possible in your instructions
  • Use clear and consistent formatting in your prompts
  • Provide multiple examples when possible to reinforce the desired format
  • Experiment with different combinations of techniques to find what works best for your use case

Additional resources

  • Prompt engineering techniques: Explore other strategies for optimizing your prompts and enhancing Claude’s performance.
  • Anthropic cookbook: Browse a collection of Jupyter notebooks featuring copy-able code snippets that demonstrate highly effective and advanced techniques, integrations, and implementations using Claude.
  • Prompt library: Get inspired by a curated selection of prompts for various tasks and use cases.