- Blog
- prefill Claude's response
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:
- 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.
- 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.
- 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:
In this example, by starting the Assistant
message with {
, we constrain Claude's output to be the rest of the requested JSON schema.
Here is how the above prompt would be written in code in the Messages API format:
python
`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
},
{
"role": "assistant",
"content": "{"
}
]
)
print(message.content)`