CONCEPT An assistant message you write, that Claude continues
Normally you send a request whose last message has role: "user", and Claude produces the
whole assistant turn from scratch. Prefilling means adding a partial
assistant message as the last item in messages. Claude doesn't
start a fresh turn — it continues generating from exactly where your text ends. You have,
literally, put the first words in its mouth.
This gives you three levers that a user-turn instruction can only request:
| Goal | Prefill | Effect |
|---|---|---|
| Force straight-into-JSON | { | No prose preamble; the response is a JSON object (6.2) |
| Enforce a structure | <analysis> | Response opens inside the tag you chose (6.2) |
| Skip "Here is…" preamble | the first real word | Claude continues the sentence you started, not a new one |
| Hold a persona | [Sherlock]: | Response stays in the voice you framed (6.3) |
CONCEPT Prefill vs. a user-turn instruction
Both aim at the same outcome — a specific output shape — but they differ in enforcement strength.
An instruction like "reply only with JSON" is a request Claude usually honors. A prefill of
{ makes the first character of the response a literal {, because it is text that
already exists in the transcript. Prefill constrains; instruction persuades.
DIAGRAM Where the prefill sits and what Claude does with it
generates: "name": "Ada" ...}"] Q -->|"user"| FRESH["Claude writes a fresh turn
(may add 'Here is the JSON:' preamble)"] CONT --> FINAL["Final assistant text =
prefill + generation"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; classDef bad fill:#f8d7da,stroke:#dc3545,color:#721c24; class CONT ok; class FINAL ok; class FRESH gate;
CODE A request with a prefilled assistant message
The prefill is just another entry in messages — with role: "assistant", placed
last. Claude's returned content holds only what it generated; the prefill is not
echoed back, so you prepend it yourself when parsing (see 6.5).
import anthropic client = anthropic.Anthropic() resp = client.messages.create( model="claude-opus-4-5", # a model that still allows prefill (see 6.5) max_tokens=300, messages=[ {"role": "user", "content": "Give me the user record for Ada as JSON."}, {"role": "assistant", "content": "{"}, # ← PREFILL: last turn is assistant ], ) generated = resp.content[0].text # e.g. "name": "Ada", "id": 7} full_json = "{" + generated # prepend the prefill → valid JSON print(full_json)
resp.content[0].text and try to json.loads() it directly — it
starts at "name", not {. The opening brace lives in your prefill, not the response.TRADE-OFFS Prefill vs. leaving the last turn to the user
✅ Prefill the assistant turn
- Deterministic opening — no preamble to strip
- Forces format/structure/persona from token one
- Cheap: a few tokens of your own text
⚠️ Costs & limits
- Unsupported on Claude 4.6+/Fable 5 → 400 (6.5)
- Incompatible with extended thinking (6.5)
- You must prepend the prefill when parsing
EXAM TRAPS
assistant-role message placed last in messages. The system prompt is a
separate channel and cannot make Claude continue from partial text.content holds
only the generated continuation; the final assistant text is prefill + generation, and you
reconstruct it yourself.PRACTICE
Q1 · What exactly makes a message a "prefill"?
It is a message with role: "assistant" placed as the last entry in
the messages array. Because the conversation ends on a partial assistant turn, Claude continues
generating from where that text stops instead of starting a new turn.
Q2 · You prefill { and get back "name": "Ada"}. How do you get valid JSON?
Prepend the prefill: "{" + response_text. The API returns only the generated
continuation, so the opening brace you supplied must be added back before parsing.
Q3 · Why prefer a prefill over "please respond only in JSON"?
The instruction is a request Claude usually — but not always — honors and may still emit a
preamble. A prefill of { makes the first character literally {, so there is no
preamble to strip and the shape is enforced from token one. (On new models, use Structured Outputs for the
same guarantee — 6.5.)