new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Prompt Engineering · all subjects

output formatting

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

XML tags for structured output

Use XML tags to format Claude's output in a structured way. When you ask Claude to put its response in XML tags (e.g., <haiku>content</haiku>), you make the output clearer and more easily parseable by downstream programs or users. This allows the end user to reliably extract specific content by writing a program to extract text between XML tags.

Prefilling Claude's response

You can prefill Claude's response by placing text in the assistant message turn before Claude continues. This technique, called 'speaking for Claude' or 'prefilling Claude's response', tells Claude that it has already said something and should continue from that point. For example, if you prefill with "<haiku>", Claude will continue directly after that opening tag.

Prefilling to enforce JSON output

To strongly encourage JSON output format (though not deterministically), prefill Claude's response with the opening bracket "{" in the assistant turn. Claude will then continue with valid JSON structure.

Using stop_sequences with XML tags in API

When calling Claude through the API, you can pass the closing XML tag to the stop_sequences parameter to make Claude stop sampling once it emits your desired tag. This eliminates Claude's concluding remarks after the answer and saves money and time-to-last-token.

Example: Haiku with XML tags

Example showing how to request and prefill XML-tagged output: ```python ANIMAL = "Cat" PROMPT = f"Please write a haiku about {ANIMAL}. Put it in <haiku> tags." PREFILL = "<haiku>" print(get_completion(PROMPT, prefill=PREFILL)) ``` This causes Claude to produce a haiku wrapped in <haiku> tags without the preamble.

Example: JSON output with prefill

Example showing how to get JSON-formatted output by prefilling with an opening bracket: ```python ANIMAL = "Cat" PROMPT = f"Please write a haiku about {ANIMAL}. Use JSON format with the keys as \"first_line\", \"second_line\", and \"third_line\"." PREFILL = "{" print(get_completion(PROMPT, prefill=PREFILL)) ``` Claude continues from the opening bracket and produces valid JSON.

Example: Multiple variables with XML tags for output formatting

Example combining multiple input variables with output formatting using XML tags: ```python EMAIL = "Hi Zack, just pinging you for a quick update on that prompt you were supposed to write." ADJECTIVE = "olde english" PROMPT = f"Hey Claude. Here is an email: <email>{EMAIL}</email>. Make this email more {ADJECTIVE}. Write the new version in <{ADJECTIVE}_email> XML tags." PREFILL = f"<{ADJECTIVE}_email>" print(get_completion(PROMPT, prefill=PREFILL)) ``` This shows using XML tags both to wrap input data and to format output, with dynamic tag names based on variables.

Output formatting positioning in complex prompts

If there is a specific way Claude's response should be formatted, clearly tell Claude what that format is. Putting output formatting instructions toward the end of the prompt is better than at the beginning. This element is optional depending on the task.

Example citation format in legal research response

When Claude cites legal research sources, use the format of brackets containing the search index ID followed by a period at the end of the sentence doing the citing. For example: 'The statute of limitations expires after 10 years for crimes like this. [3].' or 'However, the protection does not apply when it has been specifically waived by both parties. [5].'

Give your agent this brain