Pre-fill Responses
What is Prefill?
Prefill (or "putting words in Claude's mouth") is a technique where you start the assistant's response with specific text. The model then continues from where you left off.
As Anthropic explains:
> "Parsing XML tags is nice, but maybe you want a structured JSON output to make sure it's JSON serializable. You could just add that Claude needs to begin his output with a certain format."
How Prefill Works
API Structure
const response = await anthropic.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [
{
role: "user",
content: "Analyze this product review..."
},
{
role: "assistant",
content: "{" // Prefill starts here
}
]
});
// Response continues: "sentiment": "positive", ...}
The Model Continues
| Prefill | Model Continues With |
|---|---|
{ | JSON object |
[ | JSON array |
| XML content |
Here is my analysis: | Structured prose |
def | Python function |
SELECT | SQL query |
Common Prefill Patterns
JSON Object
messages: [
{ role: "user", content: prompt },
{ role: "assistant", content: "{" }
]
// Model completes: {"field": "value", ...}
JSON Array
messages: [
{ role: "user", content: "List the top 5 issues..." },
{ role: "assistant", content: "[" }
]
// Model completes: ["issue1", "issue2", ...]
XML Tags
messages: [
{ role: "user", content: prompt },
{ role: "assistant", content: "<final_verdict>" }
]
// Model completes: <fault>Vehicle B</fault>...</final_verdict>
Code
messages: [
{ role: "user", content: "Write a function to calculate..." },
{ role: "assistant", content: "python\ndef calculate(" }
]
// Model completes: total):\n return sum(items)...\n
Why Prefill is Powerful
1. Format Guarantee
Without prefill, the model might add preamble:
Here's my analysis of the document:
{
"sentiment": "positive",
...
}
With prefill ({), you get clean JSON:
{
"sentiment": "positive",
...
}
2. Reduced Tokens
No need for "Output only JSON..." instructions when you prefill with {.
3. Role Guidance
Prefill can set the tone:
{ role: "assistant", content: "As a senior engineer reviewing this code, " }
Combining with Output Format
Full Example
const systemPrompt = `
You analyze product reviews and return structured data.
Output format:
{
"sentiment": "positive" | "negative" | "mixed",
"confidence": 0.0-1.0,
"key_points": ["point1", "point2"]
}
`;
const userPrompt = `
Review to analyze:
"This laptop is amazing! Fast, lightweight, but the battery
could be better. Still highly recommend."
`;
const response = await anthropic.messages.create({
model: "claude-sonnet-4-20250514",
system: systemPrompt,
messages: [
{ role: "user", content: userPrompt },
{ role: "assistant", content: "{" }
]
});
// Clean JSON output guaranteed
const result = JSON.parse("{" + response.content[0].text);
Prefill for Different Formats
Structured Text
{ role: "assistant", content: "## Analysis\n\n" }
// Model continues with markdown content
Code with Language
{ role: "assistant", content: "typescript\n" }
// Model writes TypeScript code
### Specific Structurejavascript
{
role: "assistant",
content: '{"status": "complete", "analysis": {'
}
// Forces nested JSON structure
## When to Use Prefill
| Situation | Use Prefill? |
|-----------|--------------|
| Need clean JSON | Yes, start with `{` |
| Need XML output | Yes, start with `<tag>` |
| Conversational response | No |
| Format already specified | Optional |
| Code generation | Yes, for language hint |
## Prefill Gotchas
### 1. Don't Over-Prefilljavascript
// Too much - constrains the model
{ content: '{"sentiment": "positive"' }
// Just right - guides format
{ content: "{" }
### 2. Match Your Parserjavascript
// Prefill
{ content: "{" }
// Your parser must prepend the prefill
const json = JSON.parse("{" + response.content[0].text);
### 3. Handle Incomplete Responsesjavascript
// If max_tokens is hit, JSON may be incomplete
try {
const data = JSON.parse("{" + response);
} catch {
// Handle truncation
}
## Real-World Application
For the car accident analysis:javascript
const systemPrompt = You analyze Swedish car accident forms...;
const userPrompt =
Analyze these documents and determine fault.
;const response = await anthropic.messages.create({
model: "claude-sonnet-4-20250514",
system: systemPrompt,
messages: [
{ role: "user", content: userPrompt },
{ role: "assistant", content: "
]
});
// Parse the verdict
const verdictContent = "
```
Key Takeaways
- Prefill guides format - Start the response to control structure
- Use for JSON/XML - Guarantees clean, parseable output
- Reduces instructions - Less "output only..." text needed
- Combine with format spec - Prefill + examples = reliable output
- Handle in parser - Prepend the prefill to complete the response
Next Lesson
In our final lesson, we'll explore Extended Thinking - how to use Claude's reasoning capabilities to improve and debug prompts.