Prefill Claude's response

When using Claude, you have the unique ability to guide its responses by prefilling the Assistant message. This powerful technique allows you to direct Claude's actions, control the output format, and even help Claude stay in character during role-play scenarios. In some cases where Claude is not performing as expected, a few prefilled sentences can vastly improve Claude's performance.

Check out our blog post Long context prompting for Claude 2.1 to see an example of highly effective prefilling.


Why prefill Claude's response?

Prefilling Claude's response offers several key benefits:

  1. Increased steerability: By providing some initial text for Claude to continue from, you can steer Claude's response in a desired direction. This is particularly useful when you want Claude to focus on a specific topic, generate a particular type of content, or act a certain way.

  2. Control output format: Prefilling allows you to specify the exact format you want Claude to use for its output. This is especially handy when working with structured data formats like JSON or XML. For more details on this, see our guide on controlling output format.

  3. Maintain character consistency: In role-play scenarios, prefilling Claude's response can help Claude stay in character throughout a long conversation. By consistently reminding Claude of its role in the Assistant message, you can better ensure that Claude maintains the desired persona. Check out keep Claude stay in character for more details.


How to prefill Claude's response

To prefill Claude's response, simply include the desired initial text in the Assistant message when making an API request. Here's an example prompt:

RoleGood Prompt
UserPlease extract the name, size, price, and color from this product description and output it within a JSON object.

<description>The SmartHome Mini is a compact smart home assistant available in black or white for only $49.99. At just 5 inches wide, it lets you control lights, thermostats, and other connected devices via voice or app—no matter where you place it in your home. This affordable little hub brings convenient hands-free control to your smart devices.</description>
Assistant (prefill){

In this example, by starting the Assistant message with {, we constrain Claude's output to be the rest of the requested JSON schema.

RoleResponse
Assistant (Claude's response) "name": "SmartHome Mini",
"size": "5 inches wide",
"price": "$49.99",
"colors": [
"black",
"white"
]
}

Here is how the above prompt would be written in code in the Messages API format:

import anthropic

client = anthropic.Anthropic(
    # defaults to os.environ.get("ANTHROPIC_API_KEY")
    api_key="my_api_key",
)
message = client.messages.create(
    model="claude-2.1",
    max_tokens=1000,
    temperature=0,
    messages=[
        {
            "role": "user",
            "content": "Please extract the name, size, price, and color from this product description and output it within a JSON object.\n\n<description>The SmartHome Mini is a compact smart home assistant available in black or white for only $49.99. At just 5 inches wide, it lets you control lights, thermostats, and other connected devices via voice or app—no matter where you place it in your home. This affordable little hub brings convenient hands-free control to your smart devices.\n</description>"
        }
        {
            "role": "assistant",
            "content": "{"
        }
    ]
)
print(message.content)

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.