While these tips apply broadly to all Claude models, you can find prompting tips specific to extended thinking models here.
Prefilling is only available for non-extended thinking modes. It's not currently supported with extended thinking.
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, skip preambles, enforce specific formats like JSON or XML, and even help Claude maintain character consistency in role-play scenarios.
In some cases where Claude is not performing as expected, a few prefilled sentences can vastly improve Claude's performance. A little prefilling goes a long way!
To prefill, include the desired initial text in the Assistant message (Claude's response will continue from where the Assistant message leaves off):
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "What is your favorite color?"},
{"role": "assistant", "content": "As an AI assistant, I don't have a favorite color, But if I had to pick, it would be green because"} # Prefill here
]
)The prefill content cannot end with trailing whitespace. A prefill like "As an AI assistant, I " (with a space at the end) will result in an error.
{ forces Claude to skip the preamble and directly output the JSON object. This is cleaner, more concise, and easier for programs to parse without additional processing.
For guaranteed JSON output that conforms to a specific schema, consider using Structured Outputs instead of prefilling. Structured outputs ensure Claude's response always matches your defined JSON schema, making it ideal for production applications that require strict format compliance.[ROLE_NAME] can remind Claude stay in character, even for longer and more complex conversations. This is especially powerful when combined with role prompting in the system parameter.See more examples of prefill and other Messages API patterns.
Get inspired by a curated selection of prompts for various tasks and use cases.
An example-filled tutorial that covers the prompt engineering concepts found in our docs.
A lighter weight version of our prompt engineering tutorial via an interactive spreadsheet.