D4 · Prompt Engineering › Cluster 6 · Prefill & Output Control

6.1 · Prefilling the Assistant Turn

Put words in Claude's mouth

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.

🔑 The prefill is text you author. Claude appends to it. So the beginning of the response is deterministic — you control the opening token(s), which is enough to control the whole shape of what follows.

This gives you three levers that a user-turn instruction can only request:

GoalPrefillEffect
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…" preamblethe first real wordClaude continues the sentence you started, not a new one
Hold a persona[Sherlock]:Response stays in the voice you framed (6.3)
⚠️ Prefilling the last assistant turn is a feature of earlier Claude models. On Claude 4.6-and-newer models (and Fable 5 / Mythos) a last-turn prefill returns a 400 error — the recommended replacement is Structured Outputs. The full rule set is in 6.5; this cluster teaches the mechanic because the exam tests both the technique and its constraints.

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.

Rule of thumb: instruction describes what you want; prefill guarantees the opening. They compose — a clear instruction plus a prefill is stronger than either alone.

DIAGRAM Where the prefill sits and what Claude does with it

flowchart TD U["user: 'Give me the user record as JSON'"] --> A["assistant (prefill): '{'"] A --> Q{"Last message role?"} Q -->|"assistant"| CONT["🟢 Claude CONTINUES from the prefill
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)
Do NOT read 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

Trap: "Prefill goes in the system prompt." → No. A prefill is an assistant-role message placed last in messages. The system prompt is a separate channel and cannot make Claude continue from partial text.
Trap: "The response includes the prefill." → It does not. content holds only the generated continuation; the final assistant text is prefill + generation, and you reconstruct it yourself.
Trap: "Prefill is just an instruction phrased differently." → An instruction requests a shape; a prefill fixes the opening tokens, so it constrains the output far more strongly.
Trap: Assuming prefill works on every model. → Last-turn prefill is rejected (400) on Claude 4.6-and-newer and Fable 5 / Mythos; only earlier models allow it.

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.)