How to Use the Prefill Parameter to Guide Claude's Responses

The prefill parameter works by injecting a partial assistant message into the conversation history, causing Claude to continue generating text from that exact starting point rather than beginning from scratch.

The prefill parameter is a powerful technique in prompt engineering that lets you control how Claude structures its output. In the anthropics/prompt-eng-interactive-tutorial repository, this pattern is implemented in the get_completion helper function to demonstrate "speaking for Claude"—a method where you pre-populate part of Claude's response to enforce formats or skip preambles.

What Is the Prefill Parameter?

The prefill parameter is an optional argument passed to API wrapper functions that inserts a message with role: "assistant" into the messages array before Claude generates its response. Unlike system prompts that set behavioral context, prefilling provides literal text that Claude treats as already spoken.

In Anthropic 1P/05_Formatting_Output_and_Speaking_for_Claude.ipynb, the get_completion function implements this pattern at lines 36-46:

def get_completion(prompt: str, system_prompt="", prefill=""):
    message = client.messages.create(
        model=MODEL_NAME,
        max_tokens=2000,
        temperature=0.0,
        system=system_prompt,
        messages=[
            {"role": "user", "content": prompt},
            {"role": "assistant", "content": prefill}   # ← prefill injected here

        ]
    )
    return message.content[0].text

When prefill contains a non-empty string, Claude receives a conversation history where the assistant has already "said" that text, compelling the model to continue from that exact point.

How the Prefill Parameter Influences Claude's Behavior

Claude generates responses based on the full context of preceding messages. By using the prefill parameter to insert an assistant message, you effectively tell Claude: "You have already started speaking this way; now finish the thought."

According to Anthropic 1P/hints.py (lines 66-67), content placed in the prefilled assistant turn will not be echoed back in the output—Claude only returns the continuation. This means you must account for the missing prefix when processing the response, or simply prepend the prefill text to the returned content if you need the complete message.

Practical Examples of Using the Prefill Parameter

Enforcing XML Output Format

Prefilling with an opening XML tag forces Claude to generate content within that structure. This is demonstrated in the tutorial's haiku example:

PROMPT = "Please write a haiku about cats. Put it in <haiku> tags."
PREFILL = "<haiku>"

response = get_completion(PROMPT, prefill=PREFILL)
print(response)

Result: Claude continues immediately after <haiku>, generating the poem body and closing tag without introductory text like "Here is a haiku:".

Forcing JSON Structure

To ensure valid JSON output, prefill with the opening characters of the expected object:

PROMPT = "Summarize the user's preferences as JSON."
PREFILL = "{\n  \"preferences\": "

response = get_completion(PROMPT, prefill=PREFILL)
print(response)

Claude completes the JSON structure, typically closing the braces and brackets correctly because the prefill establishes the syntactic context.

Skipping Preamble with Direct Answers

When you want Claude to answer immediately without conversational filler, prefill the beginning of the sentence:

PROMPT = "What is the capital of France?"
PREFILL = "The capital is"

response = get_completion(PROMPT, prefill=PREFILL)
print(response)  # Output: "The capital is Paris."

This technique eliminates phrases like "The capital of France is" or "According to my knowledge," producing concise, extractable answers.

Key Files in the Tutorial Repository

The anthropics/prompt-eng-interactive-tutorial repository contains several files that demonstrate the prefill parameter implementation:

  • Anthropic 1P/05_Formatting_Output_and_Speaking_for_Claude.ipynb — Contains the primary get_completion function definition with the prefill parameter at lines 36-46.
  • Anthropic 1P/hints.py — Provides instructional context at lines 66-67 explaining that prefilled content is not echoed back in the response.
  • Anthropic 1P/09_Complex_Prompts_from_Scratch.ipynb — Demonstrates advanced usage patterns for the prefill parameter in complex prompt engineering scenarios.
  • AmazonBedrock/boto3/05_Formatting_Output_and_Speaking_for_Claude.ipynb — Implements the same get_completion helper for Amazon Bedrock's boto3 API, confirming the prefill pattern works across different Claude API implementations.

Summary

  • The prefill parameter injects a partial assistant message into the conversation history, causing Claude to continue generating from that specific starting point.
  • In the anthropics/prompt-eng-interactive-tutorial repository, the get_completion function in Anthropic 1P/05_Formatting_Output_and_Speaking_for_Claude.ipynb implements this by adding a message with role: "assistant" and the provided prefill content.
  • Prefilled content is not returned in the API response; Claude only generates the continuation, so you must account for the missing prefix when processing results.
  • Common use cases include enforcing XML/JSON formats, eliminating conversational preambles, and seeding specific sentence structures for more predictable, parseable outputs.

Frequently Asked Questions

Does the prefill parameter cause Claude to repeat the prefilled text?

No. According to the hints file in Anthropic 1P/hints.py (lines 66-67), Claude does not echo back the content you provide in the prefill parameter. The API returns only the continuation text that Claude generates after the prefilled content, meaning you must manually prepend the prefill string to the response if you need the complete message.

Can I use the prefill parameter with the Amazon Bedrock API?

Yes. The tutorial repository includes AmazonBedrock/boto3/05_Formatting_Output_and_Speaking_for_Claude.ipynb, which implements the same get_completion helper function with the prefill parameter for the Bedrock boto3 client. The pattern of injecting an assistant message works identically across both the direct Anthropic API and Amazon Bedrock implementations.

What happens if I pass an empty string to the prefill parameter?

When the prefill argument is an empty string (the default value in get_completion), the function sends only the user message without an additional assistant turn. Claude generates its response from scratch without any prefilled context, resulting in standard conversational behavior where the model chooses its own opening phrasing and structure.

Is the prefill parameter the same as system prompts?

No. System prompts set behavioral context and instructions via the system parameter in the API call, influencing Claude's tone, persona, and constraints across the entire conversation. The prefill parameter instead provides literal text content as part of the assistant's message history, mechanically forcing Claude to continue from that specific string rather than influencing behavior through instructions.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →