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

6.5 · Prefill Constraints & Gotchas

The rules you must respect

CONCEPT Four rules that break prefills

Prefilling is powerful (6.1–6.4) but hemmed in by hard constraints. Miss one and you get a 400 error, an unparseable response, or silently wrong output. These four are the ones the exam leans on.

RuleConsequence if violated
Prefill text must not end with trailing whitespaceRequest rejected
Prefill is not available with extended thinkingCannot combine the two
Final assistant text = prefill + generationParse fails if you forget to prepend
Unsupported on Claude 4.6+ / Fable 5 / Mythos (last turn)400 error

CONCEPT Rule by rule

1 · No trailing whitespace. A prefill of "The answer is " (note the trailing space) is invalid. End on the last non-space character: "The answer is". Same for tabs and newlines — "{\n" is out; use "{".
2 · Not with extended thinking. When extended/adaptive thinking is enabled, you cannot prefill the assistant turn. The model needs to produce its own thinking block first, so there's no partial assistant message for you to seed. Choose one or the other.
3 · Final text = prefill + generation. The API returns only the continuation in content. The complete assistant message is your prefill concatenated with the generated text. Whenever you parse or store the reply, prepend the prefill — and if a stop sequence stripped the closing delimiter (6.4), add that back too.
4 · Rejected on new models. On Claude 4.6-and-newer models, plus Fable 5 and Mythos, a last-turn prefill returns a 400. Adding assistant messages elsewhere in the conversation (few-shot examples, prior turns) is unaffected — only the final, to-be-continued assistant turn is blocked. The recommended replacement is Structured Outputs for format, and a clear system/user instruction for preamble-skipping and persona.

DIAGRAM Decision: can I prefill this request?

flowchart TD A["I want to prefill the assistant turn"] --> M{"Model is 4.6+ / Fable / Mythos?"} M -->|"yes"| SO["🔴 400 — use Structured Outputs
or an instruction instead"] M -->|"no (earlier model)"| T{"Extended thinking on?"} T -->|"yes"| NO["🔴 Incompatible — turn off thinking
or drop the prefill"] T -->|"no"| W{"Prefill ends in whitespace?"} W -->|"yes"| FIX["🟠 Trim trailing space/newline"] W -->|"no"| OK["🟢 Valid — remember:
final = prefill + generation"] FIX --> OK 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 OK ok; class SO bad; class NO bad; class FIX gate;

CODE BAD prefills → GOOD prefills

# ❌ trailing space → rejected
{"role": "assistant", "content": "The answer is "}
# ✅ trim it
{"role": "assistant", "content": "The answer is"}

# ❌ prefill + extended thinking in the same request → not allowed
resp = client.messages.create(
    model="claude-opus-4-5", max_tokens=1024,
    thinking={"type": "enabled", "budget_tokens": 2000},
    messages=[{"role": "user", "content": "..."},
              {"role": "assistant", "content": "{"}],   # incompatible
)

# ❌ parsing without prepending the prefill
json.loads(resp.content[0].text)          # starts at '"name"...' → error
# ✅ reconstruct: prefill + generation (+ stopped delimiter)
json.loads("{" + resp.content[0].text + "}")

# ❌ last-turn prefill on a new model → 400
#    model="claude-opus-4-8", messages=[..., {"role":"assistant","content":"{"}]
# ✅ replacement on new models: Structured Outputs
resp = client.messages.create(
    model="claude-opus-4-8", max_tokens=300,
    output_config={"format": {"type": "json_schema", "schema": schema}},
    messages=[{"role": "user", "content": "Return {name, id} for Ada."}],
)

TRADE-OFFS Keep prefill (legacy) vs. move to the modern path

Prefill (earlier models)Modern replacement
Format controlPrefill { / a tagStructured Outputs (output_config.format)
Skip preamblePrefill the first wordSystem instruction: "no preamble"
PersonaPrefill [Name]:System-prompt persona + instruction
Works with thinking?NoYes
Works on 4.6+ / Fable?No (400)Yes
If you're on a current model or want extended thinking, don't reach for prefill — the features it provided now live in Structured Outputs and clearer prompting.

EXAM TRAPS

Trap: "A trailing space in the prefill is harmless." → It's a rejected request. Prefill content must not end in whitespace — trim spaces, tabs, and newlines.
Trap: Combining prefill with extended thinking. → Not supported. The model emits its own thinking block first, leaving no partial assistant message to continue from.
Trap: "Just parse content and you're done." → The final message is prefill + generation. Prepend the prefill (and re-add any stop-sequence delimiter) before parsing.
Trap: Using last-turn prefill on Claude 4.6+/Fable/Mythos. → Returns a 400. Only earlier models allow it; on new models use Structured Outputs or an instruction. (Assistant messages earlier in the conversation are still fine.)

PRACTICE

Q1 · Why is {"role":"assistant","content":"Answer: "} rejected?

The prefill ends in trailing whitespace (the space after the colon), which is not allowed. End on the last non-space character: "Answer:".

Q2 · Can you prefill and use extended thinking together?

No — prefilling the assistant turn is not available with extended thinking enabled. The model produces its own thinking block first, so there's no partial assistant message for you to seed. Pick one.

Q3 · Your prefill request 400s on claude-opus-4-8. What's wrong and what do you do?

Last-turn prefill is unsupported on Claude 4.6-and-newer models (and Fable 5 / Mythos), so it returns a 400. Switch to the modern equivalent: Structured Outputs (output_config.format) for JSON shape, or a clear system/user instruction for preamble-skipping and persona. Assistant messages earlier in the conversation are still allowed.