Output formatting and structure

When an LLM talks to a human, free-form text is fine. But when LLM output feeds into code (a database, an API, or another prompt) you need predictable, parseable structure. This lesson covers three techniques for controlling output format: XML tags, prefilling, and JSON mode.

Output control techniques

Three techniques for controlling LLM output format

Start with XML tags since they are the easiest to use and work with every model. Then learn prefilling for strict format control. JSON mode is the most advanced but also the most reliable for machine-readable output. We will cover all three in order.

XML tags are the simplest way to get structured output. Instead of hoping the model formats correctly, you tell it exactly which tags to use: "Write a haiku about coding. Wrap your response in <haiku> tags." The model responds: <haiku> Silent keys clicking Logic flows through nested loops Bugs hide in shadows </haiku> Now your code can extract the text between <haiku> tags reliably.

Prefilling is a more powerful technique. You start the assistant response with text, and the model continues from there. For example, if you prefill with "{" the model is forced to return JSON because it must continue a valid JSON object. This works because LLMs generate the next token based on what came before. By seeding the response, you lock in the format before the model even starts thinking about content.

Use XML tags when you need multiple labeled sections (like <thinking> and <answer>). Use prefilling when you need strict format compliance (like forcing JSON output). "Just asking" works for simple cases but is less reliable in production. The more critical the format, the more you should use prefilling or XML tags instead of hoping.

AI prompt: Try it with AI

Loading practice…

Matching exercise: Format technique match

Loading practice…

Fill in the blanks: Complete the prefill pattern

Loading practice…

Ordering exercise: Structured output pipeline

Loading practice…

Validation checklist: Output formatting checklist

Loading practice…