Skip to content
bits
Prompting that works · Lesson 6 of 10

Ask for the format

Tutor:

If you don't say what shape you want, Claude picks one. Sometimes that's headings and bullet points when you wanted a sentence. Sometimes it's a friendly intro line before the data your code was about to parse.

So say the shape. Anthropic's formatting advice comes down to three habits.

Tutor:

Say what to do, not what not to do. “Don't use markdown” tells Claude what to avoid but not what to write instead. “Write it as two short paragraphs of plain prose” gives it a target.

Match your prompt to the output. The style of your prompt rubs off on the reply. A prompt full of bullet points and bold tends to get bullet points and bold back. If you want plain prose, write your prompt in plain prose.

Name a container. Ask for the answer inside tags, like <summary>, or as JSON with named keys. Then your code knows exactly where the answer starts and stops.

No format asked for vs Format asked forillustrative: made-up numbers that show the idea, not output from a real modelcompare

promptGet the booking details from: Table for 4 at Luigi's, Friday 7:30pm, under the name Okafor.

No format asked for

Sure! Here are the booking details: The reservation is for **4 people** at **Luigi's** on **Friday at 7:30pm**, booked under the name **Okafor**. Let me know if you need anything else!

Format asked for

{"name": "Okafor", "party_size": 4, "day": "Friday", "time": "19:30"}

Tutor:

The right column's prompt added: “Reply with only JSON, with the keys name, party_size, day and time.”

Tutor:

When code depends on it, don't just ask. Constrain it. A prompt makes the right format very likely; it doesn't make it certain. For JSON your program will parse, Claude's API has structured outputs: you pass a schema, and the reply is guaranteed to match it. Here's the TypeScript version, with a Zod schema.

import Anthropic from "@anthropic-ai/sdk";
import { zodOutputFormat } from "@anthropic-ai/sdk/helpers/zod";
import { z } from "zod";
const client = new Anthropic();
const Booking = z.object({
name: z.string(),
party_size: z.number(),
day: z.string(),
time: z.string(),
});
const response = await client.messages.parse({
model: "claude-haiku-5-5",
max_tokens: 1024,
output_config: { format: zodOutputFormat(Booking) },
messages: [{
role: "user",
content: "Get the booking details from: Table for 4 at Luigi's, Friday 7:30pm, under the name Okafor.",
}],
});
console.log(response.parsed_output); // typed as { name, party_size, day, time }
TypeScriptUTF-8● lines 13–16: highlighted
Tutor:

An old trick you'll still see: prefill. Older tutorials start Claude's reply for it, by sending an assistant message that begins with {, to force JSON. Current Claude models (4.6 and later) reject a prefilled last assistant turn with a 400 error. Use a clear instruction, output tags or structured outputs instead.

quick check+20 neurons
Tutor:

A tutorial ends its messages with an assistant turn containing just “{” to force JSON. You run it on Claude Haiku 5.5. What happens?

    Type your answer or tap an option · Enter checks

    Tutor:

    Your turn. No API here, so do it with the prompt alone. Ask for the booking as JSON, name the keys you want (“with the keys name, party_size and time”), and say it should be only the JSON.

    Your turn

    Run your prompt and the replies land here.

      • Asks for JSON, not yet
      • Names the keys, not yet
      • Says only the JSON, not yet
      • Includes the booking, not yet
      • Ran at 1.0, not yet
      run at
      ~0 tokens

      Turn on JavaScript to run prompts.

      Tutor:

      Run it three times. If the time comes back as “19:30” once and “7:30pm” the next, that's a hint your prompt should say which format you want. That sort of drift is what lesson 9's tests are for.