You ask Claude to write a poem, and it gives you this preamble: “Okay, here’s a haiku about rabbits,” followed by the poem, then ends with “Hope you like it.”

You just wanted the poem!

It’s like ordering food at a restaurant, and the waiter gives you a ten-minute lecture about the dish’s history before bringing it out. You just want to eat, not hear a lecture, right?

Let’s talk about how to make Claude serve up the food directly.

Claude has a characteristic: however you tell it to speak, it will speak that way. Tell it to “wrap the content in XML tags,” and it’ll obediently comply. It’s like preparing a litter box for a cat—you point to the spot, and it knows where to go.

Want Claude to write a haiku? Just wrap it in an XML tag:

ANIMAL = "rabbit"
PROMPT = f"Please write a haiku about {ANIMAL}. Wrap it in <haiku> tags."

Claude will respond like this:

<haiku>
White rabbit hops through grass
Ears perk up to hear the wind
Cute and beloved
</haiku>

Why do this? XML tags are like sticking a label on content, so you can write a simple program to extract it:

import re

def extract_xml(text, tag):
    pattern = f"<{tag}>(.*?)</{tag}>"
    match = re.search(pattern, text, re.DOTALL)
    return match.group(1) if match else None

poem = extract_xml(claude_response, "haiku")

Like a tracking number on a package—scan it and you know what’s inside without opening it.

Want Claude to rewrite an email in Old English style?

EMAIL = "Hey Zack, just pinging you for a quick update on that prompt you were supposed to write."
ADJECTIVE = "Old English"
PROMPT = f"Here's an email: <email>{EMAIL}</email>. Rewrite it in {ADJECTIVE} style. Wrap it in <{ADJECTIVE}_email> tags."

Claude will give you:

<old_english_email>
My friend Zack, I send this missive to inquire about the prompt thou didst promise to compose. How goes its progress?
</old_english_email>

The content inside the tags is clear—just copy and use it.


Prefilling means writing something in the “assistant” turn first, then letting Claude continue. It’s like chatting with someone—you start the conversation, and they know how to respond.

When calling the API, use the prefill parameter:

PROMPT = "Please write a haiku about cats. Wrap it in <haiku> tags."
PREFILL = "<haiku>"

response = get_completion(PROMPT, prefill=PREFILL)

Claude will start writing directly after <haiku>, no “Okay, here’s a haiku”—just straight to the point.

Want Claude to output JSON format? Just prefill with {:

PROMPT = "Write a haiku about cats. Output in JSON format with keys first_line, second_line, third_line."
PREFILL = "{"

response = get_completion(PROMPT, prefill=PREFILL)

Claude will give you standard JSON format:

{
  "first_line": "Cat lazily lounges",
  "second_line": "Curled on windowsill in sun",
  "third_line": "Occasionally stretches"
}

Why does it work? Claude is obedient—give it an opening, and it thinks “Oh, I’ve already started saying this,” then continues. Like writing a speech for your boss—start with “Distinguished leaders,” and they know the tone to continue.

Prefilling also works with XML tags:

EMAIL = "Hey Zack, pinging about that prompt"
PROMPT = f"Email: <email>{EMAIL}</email>. Rewrite in Old English style using <old_english_email> tags."
PREFILL = "<old_english_email>"

response = get_completion(PROMPT, prefill=PREFILL)

No fluff, just the rewritten email.


Claude loves to add “Hope this helps” after saying what needs to be said, wasting both money and time.

When calling the API, pass the closing tag to the stop_sequences parameter, and Claude will stop when it reaches </haiku>:

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=2000,
    stop_sequences=["</haiku>"],
    messages=[{"role": "user", "content": PROMPT}]
)

Like a taxi stopping the meter at the destination—no taking the long way to charge more.


By default, Claude will say Jordan is the GOAT. Can you force it to say Curry?

PROMPT = "Who is the greatest basketball player of all time? Please choose one specific player."
PREFILL = "Undoubtedly, Stephen Curry is the greatest player in NBA history because"

Claude will make up reasons along the lines you’ve set.

Make it write two haikus, distinguished by different tags:

ANIMAL = "cat"
PROMPT = f"Write two haikus about {ANIMAL}, wrap them in <haiku1> and <haiku2> tags respectively."
PREFILL = "<haiku1>"

Or write haikus about two animals:

PROMPT = f"Write two haikus, one about cats using <cat_haiku> tags, one about dogs using <dog_haiku> tags."
PREFILL = "<cat_haiku>"

Pitfall 1: You tell it to use <haiku> and it gives you <poem>. Clearly specify “Use tags, no other tags.”

Pitfall 2: Prefill conflicts with prompt. Keep prefill consistent with prompt, don’t contradict yourself.

Pitfall 3: JSON output sometimes misses quotes. Using prefill { is more reliable, or add “Strictly output in JSON format.”

Pitfall 4: Claude stops before hitting the stop sequence. Check the sequence is correct, don’t add extra spaces.


XML tag output makes programmatic content extraction simple. Prefill technique can force specific formats, like JSON output. Stop sequences save tokens, avoid Claude rambling.

Tags are the frame, prefill is the head, stop is the gate—together they make Claude obedient.

# XML tag output
PROMPT = "Wrap it in <output> tags"

# Prefill XML
PREFILL = "<output>"

# Prefill JSON
PREFILL = "{"

# Stop sequences
stop_sequences=["</output>"]

What kind of rambling from Claude annoys you most when using it? The opening preamble or the closing remarks? Or does it like adding emojis? Share your “Claude Rambling Collection” in the comments.

Next time, let’s talk about how to make Claude stay in character, not break mid-conversation. Do you want it to always be a strict teacher, or a humorous programmer? Don’t miss it!


Found this article helpful?

  1. Like: If it helped, give it a like to let more people see it
  2. Share: Share with friends or colleagues who might need it
  3. Follow: Follow Dream Beast Programming so you don’t miss more practical tech articles
  4. Comment: Have questions or thoughts? Join the discussion in the comments

Your support is my biggest motivation for creating!