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

6.2 · Prefill to Control Format

Skip preamble · force JSON with {

CONCEPT The opening character decides the format

An LLM continues the most probable path from where the text stops. If the transcript already ends in {, the only sensible continuation is more JSON — Claude will not backtrack to write "Here is the JSON you asked for:". That is the whole trick behind format-control prefills: seed the first character(s) of the shape you want, and the rest follows.

WantPrefillWhy it works
One JSON object, no prose{Continuation must complete a JSON object
A JSON array[Continuation must complete a JSON array
Content inside a tag<summary>Response opens inside your XML structure
A code block only```pythonResponse starts as fenced code, no explanation
The classic move: prefill { to force straight-into-JSON. It kills the preamble ("Sure! Here's…") that otherwise breaks a downstream json.loads().

CONCEPT Prefill a tag to enforce structure

Prefilling an opening XML tag such as <analysis> forces the response to begin inside a named section. Combined with a stop sequence on the closing tag (6.4), you extract exactly one clean block — no wrapper text, no trailing commentary. This is a reliable way to carve a specific field out of a longer reasoning turn.

🔑 Prefill sets where generation starts; a stop sequence sets where it ends. Together they bound the output on both sides — 6.4 completes the pair.

DIAGRAM BAD (no prefill) vs. GOOD (prefill {)

flowchart TD Q["user: 'Return {name, id} for Ada as JSON'"] --> B{"Prefill the assistant turn?"} B -->|"no prefill"| PRE["🔴 'Sure! Here is the JSON:
{"name": "Ada", "id": 7}'"] PRE --> STRIP["Must regex/strip the preamble
before json.loads()"] B -->|"prefill '{'"| CLEAN["🟢 continues: '"name": "Ada", "id": 7}'"] CLEAN --> PARSE["Prepend '{' → parse directly"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef bad fill:#f8d7da,stroke:#dc3545,color:#721c24; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; class CLEAN ok; class PARSE ok; class PRE bad; class STRIP gate;

CODE Force JSON with {, then reconstruct

import anthropic, json
client = anthropic.Anthropic()

resp = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=300,
    messages=[
        {"role": "user",
         "content": "Return {name, id} for user Ada as a single JSON object."},
        {"role": "assistant", "content": "{"},   # ← force straight into JSON
    ],
)

# content holds ONLY the continuation: '"name": "Ada", "id": 7}'
data = json.loads("{" + resp.content[0].text)   # prepend prefill, then parse
print(data["name"])                             # Ada
⚠️ On Claude 4.6-and-newer models this exact request returns a 400 — prefill the last turn is unsupported there. Use Structured Outputs (output_config.format with a JSON schema) for the same guarantee. The prefill technique itself is examinable, and still valid on earlier models. See 6.5.

TRADE-OFFS Prefill { vs. Structured Outputs

Prefill {Structured Outputs
GuaranteeOpening is {; body still free-formWhole response validates against a schema
Model supportEarlier models only (400 on 4.6+)Current models
ParsingYou prepend the prefill by handSDK can parse into a typed object
Best forQuick format nudge, skip preambleStrict, schema-validated JSON
Same intent, two eras: on legacy models prefill {; on current models reach for Structured Outputs. Both answer "give me clean JSON, no chatter."

EXAM TRAPS

Trap: "Prefill { guarantees valid JSON." → It guarantees the response starts as JSON and skips preamble. The body can still be malformed or run past max_tokens — for a hard schema guarantee use Structured Outputs.
Trap: Forgetting the prefill when parsing. → content[0].text begins after your {. Prepend it, or json.loads() raises.
Trap: "Prefill only does JSON." → Any opening works — [, an XML tag, a code fence, a first word. Prefill controls whatever shape you seed.
Trap: Prefilling a tag but never closing it. → The response opens inside <summary> but keeps going; pair the tag prefill with a stop sequence on the closing tag (6.4) to get exactly one block.

PRACTICE

Q1 · Why does prefilling { remove the "Here is the JSON:" preamble?

The transcript already ends in {, so the most probable continuation is more JSON, not a fresh sentence of prose. Claude can only continue forward from the seeded character — it cannot prepend a preamble before text that already exists.

Q2 · What is the modern replacement for a "force JSON" prefill on new models?

Structured Outputs — output_config.format with a JSON schema. It constrains the entire response to the schema (not just the opening character) and is supported on current models where last-turn prefill returns a 400.

Q3 · How do you extract exactly one <analysis>…</analysis> block?

Prefill the opening tag <analysis> so the response starts inside it, and set a stop sequence of </analysis> so generation ends at the close (6.4). Prefill bounds the start; the stop sequence bounds the end.