What you will be able to do
- Write a role-setting system prompt and pass it through the system parameter of the Messages API.
- Phrase instructions and constraints so Claude understands both what to do and why.
- Build a reusable prompt template that separates instructions, data and examples with XML tags.
- Pin an output format in a way that still works on current Claude models.
Key concept
Role-first system prompt — The system prompt tells Claude who it is for your application. Instructions, context and format rules are then built on top of that role. The role focuses behaviour and tone, and the rest of the prompt supplies the context a capable newcomer would otherwise be missing.
1.Start with a role in the system prompt
In the Messages API, standing instructions go in the top-level system parameter, separate from the conversation in messages. Anthropic's best-practices guide says the first thing to put there is a role. Setting a role focuses Claude's behaviour and tone for your use case, and the guide adds that even a single sentence makes a difference. The guide on reducing prompt leaks says the same thing from another angle. After showing a system prompt full of extra rules, it notes that the prompt is still mostly a role prompt, which it calls the most effective way to use system prompts.
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
system="You are a helpful coding assistant specializing in Python.",
messages=[
{"role": "user", "content": "How do I sort a list of dictionaries by key?"}
],
)
print(message.content)The role sets the direction. It does not tell Claude how your team works, what counts as a good answer, or what it must avoid. That comes next.
2.Write instructions a newcomer could follow
The guide's model of Claude is a brilliant new employee who lacks context on your norms and workflows. Its practical test is the golden rule: show your prompt to a colleague with little context on the task. If they would be confused, Claude will be too. A few habits follow from this:
- Be specific about the output format and the constraints. - Use numbered or bulleted steps when order or completeness matters. - Ask for 'above and beyond' behaviour explicitly instead of hoping Claude infers it. For example, 'Create an analytics dashboard' does less than a request that also asks for as many relevant features and interactions as possible.
The guide rewrites exactly this rule. The better version gives the reason: the text-to-speech engine will not know how to pronounce ellipses. Its conclusion is that Claude is smart enough to generalise from the explanation. Once Claude knows the output is spoken, it can avoid other things a speech engine would stumble on, not just the one forbidden character. So guardrail wording works best as a rule plus its reason, not as a bare prohibition.
Newer models also follow restrictive wording more literally. The Sonnet 5 prompting guide warns that review prompts which say 'only report high-severity issues' or 'don't nitpick' may be followed more faithfully than earlier models followed them. The model can still find the lower-severity bugs and then decide not to report them. The result is higher precision and lower measured recall. When you write a constraint into a system prompt, make sure it describes what you actually want. If a later step will filter the results, say that the current step's job is coverage.
A team is building a customer support assistant and wants the system prompt to reliably shape Claude's tone and behavior across thousands of daily conversations. The current system prompt is a single sentence: "You are a helpful assistant." Support agents report inconsistent formality and occasional off-topic tangents. What is the most effective first change to the system prompt design?
Correct answer: A — Give Claude a specific role describing the product, audience, and tone, since even a one-sentence role framing measurably focuses behavior and register
- A. Correct. Anthropic's guidance is that setting a role in the system prompt focuses Claude's behavior and tone for the use case, and even a single sentence makes a measurable difference.
- B. Incorrect. The system prompt is the intended place to set persistent role and tone; shifting this to the user turn discards the mechanism designed for exactly this purpose.
- C. Incorrect. Telling Claude what to do (a role and desired tone) is more effective than only telling it what not to do; a banned-phrase list alone doesn't establish consistent register.
- D. Incorrect. max_tokens controls output length, not tone consistency, and does not address the root cause of inconsistent framing.
3.Turn the prompt into a template with XML tags and examples
Once a prompt works, you reuse it with different inputs. That makes it a template: fixed instructions plus placeholder variables such as {{DATA}} that your code fills in for each request. The risk is that Claude confuses the pieces, for example by reading part of the data as an instruction. The best-practices guide recommends putting each type of content in its own XML tag. It says this helps Claude parse complex prompts unambiguously, especially when instructions, context, examples and variable inputs are mixed together.
| Prompt content | Tagging practice from the guide |
|---|---|
| Instructions, background, variable input | Give each its own tag, for example <instructions>, <context>, <input> |
| Few-shot examples | Wrap each in <example>, and a set of them in <examples> |
| Multiple documents | Nest them: each <document index="n"> inside <documents> |
| Tag names in general | Keep them consistent and descriptive across your prompts |
Examples belong in the template too. The guide calls few-shot (multishot) examples one of the most reliable ways to steer format, tone and structure. Good examples are relevant, meaning they mirror your real use case. They are diverse enough to cover edge cases and to stop Claude copying an unintended pattern. And they are structured, meaning tagged so Claude can tell them apart from the instructions. The template below from Anthropic's consistency guide combines these ideas. The variable data sits in its own tag, followed by one worked example of the exact shape each answer should take.
As a Market Intelligence AI, your task is to analyze data about our competitors. Here is our competitor data:
<data>
{{DATA}}
</data>
Output following this example format:
<competitor>
<name>Rival Inc</name>
<overview>A 50-word summary.</overview>
<swot>
<strengths>- Bullet points</strengths>
<weaknesses>- Bullet points</weaknesses>
<opportunities>- Bullet points</opportunities>
<threats>- Bullet points</threats>
</swot>
<strategy>A 30-word strategic response.</strategy>
</competitor>Sources1
4.Pin the output format, and keep the system prompt stable
For output that other code will parse, the consistency guide says to define the format precisely using JSON, XML or a custom template. Its customer-feedback example names every JSON key and the allowed values: "sentiment" (positive/negative/neutral), "key_issues" (a list) and "action_items" (a list of dicts with "team" and "task"). Claude then returns exactly those keys.
Not on current models. The prompt-leak guide notes that prefilling is not supported on Claude 4.6 and later models, or on Claude Mythos Preview. On those models, get format consistency from a precise format specification and tagged examples instead.
Where the system prompt sits also affects reuse. The top-level system field comes before every message, so it is part of the stable prefix that later turns read from the prompt cache. Editing it partway through a session changes the very start of the prompt and invalidates the cache for everything after it. If you discover mid-conversation that you need a new instruction, you can append a mid-conversation system message at that point instead, on supported models. The cached prefix stays the same, and the instruction still counts as a system instruction rather than ordinary user text.
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.A bare all-caps rule such as 'NEVER use ellipses' is the most reliable way to enforce a constraint.Why is that wrong?
The guide's better version explains the reason for the rule (a text-to-speech engine cannot pronounce ellipses). Claude generalises from that reason to similar cases the bare rule never mentioned.
Covered in Write instructions a newcomer could follow
2.Prefilling the Assistant turn is a universal way to force an output format.Why is that wrong?
Prefilling is not supported on Claude 4.6 and later models or Claude Mythos Preview. Define the format precisely and give tagged examples instead.
Covered in Pin the output format, and keep the system prompt stable
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practicesOfficial docs
“Setting a role in the system prompt focuses Claude's behavior and tone for your use case.”
↩︎ Start with a role in the system prompt“Think of Claude as a brilliant but new employee who lacks context on your norms and workflows.”
↩︎ Write instructions a newcomer could follow“Claude is smart enough to generalize from the explanation.”
↩︎ Write instructions a newcomer could follow“XML tags help Claude parse complex prompts unambiguously, especially when your prompt mixes instructions, context, examples, and variable inputs.”
↩︎ Turn the prompt into a template with XML tags and examples“Examples are one of the most reliable ways to steer Claude's output format, tone, and structure.”
↩︎ Turn the prompt into a template with XML tags and examples“Setting a role in the system prompt focuses Claude's behavior and tone for your use case.”
↩︎ Key concept“Claude is smart enough to generalize from the explanation.”
↩︎ Exam trap 1 - 2.https://platform.claude.com/docs/en/test-and-evaluate/strengthen-guardrails/reduce-prompt-leakOfficial docs
“Notice that this system prompt is still predominantly a role prompt, which is the most effective way to use system prompts.”
↩︎ Start with a role in the system prompt“prefilling is not supported on Claude 4.6 and later models and Claude Mythos Preview.”
↩︎ Exam trap 2 - 3.https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/prompting-claude-sonnet-5Official docs
“Claude Sonnet 5 may follow that instruction more faithfully than earlier models did”
↩︎ Write instructions a newcomer could follow - 4.https://platform.claude.com/docs/en/test-and-evaluate/strengthen-guardrails/increase-consistencyOfficial docs
“Precisely define your desired output format using JSON, XML, or custom templates so that Claude follows every output formatting element you require.”
↩︎ Pin the output format, and keep the system prompt stable - 5.
“editing the top-level system field changes the very beginning of the prompt and invalidates the cache for everything that follows.”
↩︎ Pin the output format, and keep the system prompt stable