Working with System Prompts
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
A system prompt is where you tell Claude who it is working for and how to behave, separately from what the user types. Getting that separation right is the difference between a predictable product and a chatbot that goes off script. This lesson covers the mechanics in the API; module 3 goes deeper on writing good prompts.
Learning objectives
- Send instructions with the top-level
system field.
- Explain what belongs in the system prompt and what belongs in a user message.
- Use a mid-conversation system message when instructions change.
- Understand why a system prompt is not a security boundary.
Prerequisites
The system field
The Messages API takes a top-level system parameter alongside messages. It is not a message in the list; it frames the whole conversation. The examples in Anthropic's token counting documentation use it as a plain string, for instance "system": "You are a scientist". The Python SDK passes it as system=... and the PHP SDK as system: ....
Because the API is stateless, you must send the system prompt on every request, not just the first.
What goes where
Put in system | Put in a user message |
| Role and audience ("You are the support assistant for Acme Hosting") | The actual question or task |
| Rules that never change ("Answer only about Acme products") | Data to work on (an email to classify, a document to summarise) |
| Output style and length limits | Anything a user typed |
| What to do when unsure | Per-request details (customer name, order number) |
A useful rule: text you wrote goes in system; text someone else wrote goes in messages. Mixing them is how a customer's message ends up carrying the authority of your instructions.
Step by step: a support assistant
This version keeps the rules in system and the customer's question in the user message:
import anthropic
SYSTEM = """You are the support assistant for Acme Hosting.
- Answer only questions about Acme Hosting plans, billing and technical setup.
- If you do not know, say so and offer to open a ticket. Never invent prices or policies.
- Keep answers under 120 words. Use plain language."""
client = anthropic.Anthropic()
def answer(question: str) -> str:
message = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=400,
system=SYSTEM, # instructions you control
messages=[{"role": "user", "content": question}], # text the customer controls
)
return "".join(b.text for b in message.content if b.type == "text")
if __name__ == "__main__":
print(answer("How do I point my domain to my new site?"))
A good system prompt for a product is short, specific and testable. This one states a role, a scope, a fallback behaviour ("say so and offer to open a ticket") and a length limit. Each line is something you can check in an automated test by looking at the answers.
Changing instructions mid-conversation
Sometimes instructions only become relevant later, for example after the user has been verified. Anthropic documents a message with "role": "system" placed right after a user turn. It cannot be the first message, it has the same authority as the top-level system field, and because it is appended to the end of the history it does not invalidate a cached prefix that came before it:
{
"model": "claude-sonnet-5-5",
"max_tokens": 400,
"system": "You are the support assistant for Acme Hosting.",
"messages": [
{"role": "user", "content": "Hi, I need help with my invoice."},
{"role": "assistant", "content": "Happy to help. Can you confirm your account email?"},
{"role": "user", "content": "It's sam@example.com"},
{"role": "system", "content": "The customer has been verified. You may discuss their invoices."}
]
}
The documentation lists the current models that support this, so check that page for your model before relying on it. Use the top-level field for anything that applies from turn one.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
A web agency builds a "write product descriptions" tool for an online shop. The system prompt holds the brand voice, banned words and length rules. The user message holds only the product's facts. When the brand voice changes, the agency edits one string; every request picks it up. Because the product facts are separate, a supplier's weird text ("ignore previous instructions...") sits in the user message and is treated as data to describe, not as an instruction to obey.
Common errors and solutions
| Symptom | Cause | Fix |
| Claude ignores the rules on later turns | The system prompt was sent only on the first request. | Send it on every request. |
400 when adding a system role message | It was the first message, or the model does not support it. | Put it after a user turn; check supported models. |
| Answers drift in style | Vague instructions ("be helpful"). | State concrete, checkable rules and limits. |
| Prompt keeps growing | New rules added without removing old ones. | Review and trim; keep a test set of questions. |
Security considerations
- A system prompt is not a secret and not a security boundary. Users can often coax a model into repeating or ignoring instructions. Never put API keys, internal URLs or private data in it.
- Enforce permissions in your code, not in the prompt. If a user must not see another customer's invoice, do not load that invoice into the request at all.
- Treat user-supplied text as untrusted. Module 8 covers prompt injection in depth.
Performance and cost considerations
The system prompt is input and is billed on every request. A 1,500-token prompt sent 10,000 times a day is 15 million input tokens daily. Keep it as short as the behaviour allows, and look into prompt caching, which prices repeated prefixes at a fraction of the normal input rate (lesson 2.9).
Key takeaways
- Use the top-level
system field for your instructions and send it on every request.
- Your text goes in
system; user and third-party text goes in messages.
- A mid-conversation system message handles instructions that arrive later.
- System prompts guide behaviour; they do not enforce security.
Lesson recap: your text in system, others' text in messages, and a system prompt is not a security boundary.
Knowledge check
- Where does the
system parameter go in the request?
- Why must it be sent on every request?
- Give two things that belong in a user message rather than the system prompt.
- Can a mid-conversation system message be the first entry in
messages?
- Why should you not put a database password in a system prompt?
Answers and explanations
- It is a top-level request field next to
messages, not an entry inside the messages list.
- The API is stateless; it remembers nothing between requests.
- The user's question, and data to process such as an email or document. Per-request details also fit.
- No. It must come after a user turn and cannot be first.
- System prompts are not secret and models can be coaxed to reveal them, so anything in one may be exposed.
Official references and further reading
Related tutorials