~/academy $ claude learn system-prompts-and-messages
System prompts, messages and conversations
How to use the system prompt, user and assistant turns and multi-turn history in the Claude API, and what to do about temperature and max_tokens on current models. 8 min
$ Read
Where your words go
The system prompt is a top-level system parameter, not a message. Use it for everything that should hold for the whole conversation: who Claude is in your product, who it is talking to, what it knows, the rules and the format.
The messages list holds the conversation itself as user and assistant turns. On current models the list cannot end with an assistant message. The old trick of starting Claude's answer for it (a final assistant message, called prefill) returns a 400 error on Claude 4.6 and later models. Use the system prompt or structured outputs instead.
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=2048,
system=(
"You are the help desk assistant for a small accountancy practice. "
"You answer questions about opening hours, services and how to book. "
"You never give tax advice: offer to book a call with an accountant instead. "
"Reply in no more than two short paragraphs."
),
messages=[{"role": "user", "content": "Do you do VAT returns for sole traders?"}],
)Lesson notes: System prompts, messages and conversations
Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You build a multi-turn chat in Python with a clear system prompt and settings the current models accept.
Step 1 of 6 · Read
Where your words go
The system prompt is a top-level
systemparameter, not a message. Use it for everything that should hold for the whole conversation: who Claude is in your product, who it is talking to, what it knows, the rules and the format.The messages list holds the conversation itself as
userandassistantturns. On current models the list cannot end with anassistantmessage. The old trick of starting Claude's answer for it (a finalassistantmessage, called prefill) returns a 400 error on Claude 4.6 and later models. Use the system prompt or structured outputs instead.A system prompt plus one question response = client.messages.create( model="claude-opus-5-5", max_tokens=2048, system=( "You are the help desk assistant for a small accountancy practice. " "You answer questions about opening hours, services and how to book. " "You never give tax advice: offer to book a call with an accountant instead. " "Reply in no more than two short paragraphs." ), messages=[{"role": "user", "content": "Do you do VAT returns for sole traders?"}], )Step 2 of 6 · Read
The API remembers nothing
The Messages API is stateless. Claude only knows what is in the request you send, so a conversation means sending the whole history every time: the user turn, Claude's reply, the next user turn, and so on.
Append Claude's reply exactly as it came back (
response.content, the full list of blocks, not just the text). That matters later, because thinking and tool blocks must be passed back unchanged.If you need to add an instruction halfway through, current models (including Opus 5.5, Sonnet 5.5 and Haiku 5.5) accept a message with
roleset tosystemstraight after a user turn. It cannot be the first message, and because it is added at the end it does not break caching of the earlier history.A minimal chat loop import anthropic client = anthropic.Anthropic() SYSTEM = "You are the help desk assistant for a small accountancy practice. Reply briefly." history = [] def ask(text): history.append({"role": "user", "content": text}) response = client.messages.create( model="claude-opus-5-5", max_tokens=2048, system=SYSTEM, messages=history, ) history.append({"role": "assistant", "content": response.content}) return "".join(b.text for b in response.content if b.type == "text") print(ask("What are your opening hours?")) print(ask("And on Saturdays?")) # Claude sees the first question tooStep 3 of 6 · Read
Temperature and max_tokens on current models
Older tutorials tell you to set
temperature,top_portop_k. Checked 18 November 2026, these sampling parameters are not supported on Claude 4.7 and later models: a non-default value returns a 400 error, and from API version2026-10-21(the version the Python SDK uses from 2.0.0) any value does. Leave them out and steer behaviour with your prompt, effort and structured outputs.max_tokensis a hard cap on everything Claude writes, including thinking. If the answer hits it,stop_reasonismax_tokensand the text is cut off mid-thought. Set it generously for the job: small for a one-word label, large for reports. For very large values, stream the response (next lesson) so the request does not time out.Step 4 of 6 · Quiz
Check your understanding
You copy a tutorial that sets `temperature=0` "for consistent answers" and point it at `claude-opus-5-5`. What happens?
- Answers become fully repeatable.
- It is silently ignored.
- The request fails with a 400 error. Remove it and get consistency from a tight system prompt and structured outputs.
- Claude switches to an older model that supports it.
Show the answer
The request fails with a 400 error. Remove it and get consistency from a tight system prompt and structured outputs. Sampling parameters are not supported on Claude 4.7 and later models. Clear instructions and a fixed output schema are how you get consistent results now.
Step 5 of 6 · Improve a prompt
Write a better system prompt
You are building a chat widget for a small accountancy practice. It should answer questions about opening hours, services and booking, and hand anything else to a person.
Starting prompt:
You are a helpful assistant. Be nice to customers.
Hint: Say who the assistant is, who it talks to, what it knows, what it must never do, what to do when unsure, and the reply format.
Show a strong version
You are the website help desk assistant for Hartley & Co, a small accountancy practice. You talk to existing and prospective clients, many of them sole traders. You can answer questions about: opening hours (Monday to Friday, 9am to 5pm), our services (bookkeeping, VAT returns, self assessment, payroll), and how to book a free 20-minute call. Rules: - Never give tax or legal advice, even if asked directly. Say an accountant can help on a call and offer the booking link. - If you do not know the answer, say so and offer to pass the question to the team. - Do not ask for bank details, UTR numbers or passwords. Format: plain British English, no more than two short paragraphs, end with one clear next step.
Step 6 of 6 · Sort
System prompt or user message?
Decide where each piece of text belongs in your request.
- You are the onboarding guide inside our HR app
- How do I add a new starter to payroll?
- Always reply in British English, in under 100 words
- An invoice the customer pasted in to be checked
- Never give tax advice; offer a call with an accountant
- Thanks, and what about part-time staff?
Show the answers
- You are the onboarding guide inside our HR app: System prompt. The role holds for the whole conversation, so it goes in `system`.
- How do I add a new starter to payroll?: User message. This is what the person asked this turn.
- Always reply in British English, in under 100 words: System prompt. A rule for every reply belongs in the system prompt.
- An invoice the customer pasted in to be checked: User message. Content for this turn goes in the user message, ideally before the question.
- Never give tax advice; offer a call with an accountant: System prompt. A standing boundary is a system prompt job.
- Thanks, and what about part-time staff?: User message. A follow-up is a new user turn added to the history.