Claude Tips mascot
Claude Tips & Tricks
API Tips advanced

Prefill Claude's Response for Control

Use the assistant prefill technique to steer Claude's output format, language, or starting point.

In the API, you can start Claude’s response by including a partial assistant message:

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "List the top 5 Python web frameworks"},
        {"role": "assistant", "content": "```json\n["}
    ]
)

Claude will continue from where you left off, ensuring JSON output. This works for:

  • Forcing output format: Start with { or [ for JSON
  • Setting tone: Prefill with “Certainly! Here’s a concise…”
  • Language control: Start the response in the desired language
  • Structured output: Begin with a template Claude should fill in