Formatting Output & Speaking for Claude
Claude will choose its own format if you don't specify one. Learn to take control: JSON schemas, Markdown structure, table generation, and the powerful prefill technique.
Why Output Format Matters for Downstream Use
Claude's response doesn't end when it reaches you — it feeds into something downstream. Maybe it's parsed by code, inserted into a database, displayed in a UI, reviewed by a human, or piped into another prompt. If the format doesn't match what's expected downstream, the whole pipeline breaks.
Format specification is not about aesthetics — it's about making Claude's output machine-readable, predictable, and processable. A JSON extractor that returns prose once will break your entire data pipeline.
Format Options: When to Use Each
JSON Output: Specifying Schema Explicitly
For any task that requires JSON output, always provide the exact schema. Don't just say "return JSON" — show Claude the exact structure, field names, and types you expect.
Extract structured information from the following job posting.
Return a valid JSON object — no markdown, no explanation, just JSON.
Schema:
{
"job_title": string,
"company": string,
"location": string | null,
"remote": "yes" | "no" | "hybrid",
"salary_range": {
"min": number | null,
"max": number | null,
"currency": string
},
"required_skills": string[],
"experience_years_min": number | null,
"seniority": "junior" | "mid" | "senior" | "lead" | "unknown"
}
<job_posting>
{job_posting_text}
</job_posting>
string | null, number | null, and enum values like "yes" | "no" | "hybrid" to your schema dramatically improves Claude's JSON output consistency. It signals the valid type domain for each field, reducing type mismatches.Markdown Formatting: Helpful vs. Cluttering
Markdown formatting is additive when it renders — and visual noise when it doesn't. Know your rendering context:
# Requesting rich Markdown (rendered UI, documentation)
Write a technical README for this Python library.
Use: H2 headers for sections, code blocks for examples,
bold for key terms, bullet lists for features.
Format: standard GitHub README style.
# Requesting no Markdown (plain text contexts, voice, SMS)
Write a short welcome message for our SMS onboarding flow.
Plain text only — no asterisks, no hashes, no bullet characters.
Just clean sentences.
# Controlling Markdown granularly
Write a product comparison.
Use a Markdown table for the feature comparison.
Use plain prose for the recommendation paragraph.
No headers — this will be embedded in a larger document.
The Prefill Technique: Speaking for Claude
The Claude API lets you pre-populate the start of Claude's response. This is called prefilling the assistant turn. It's one of the most powerful format control techniques in the API.
By adding a partial message in the assistant role, you force Claude to continue from that point — anchoring the format, tone, and structure from the very first character.
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Extract the key financial metrics from this earnings call transcript.\n\n<transcript>{transcript}</transcript>"
},
{
"role": "assistant",
"content": "{" # <-- Prefill forces JSON output from character 1
}
]
)
# Claude continues from the "{" — response is guaranteed to start as JSON
raw = "{" + message.content[0].text
import json
data = json.loads(raw)
Anti-Preamble: Eliminating "Certainly!"
Without guidance, Claude often starts responses with filler phrases like "Certainly! I'd be happy to help...", "Great question!", or "Of course! Here's what I found...". These phrases add zero value and waste tokens. Prefill is the most reliable way to eliminate them:
Format Specification Formula
A complete format specification answers five questions about the output: