A system prompt is the standing instruction an application gives a language model before any user message: who it is, what it should do, which rules apply and how to format answers. This guide shows how to write one in six parts, where it goes in each major API, two templates for assistants and agents, and how to test it before you ship.
- Write a system prompt in six parts: role, context, rules, examples, output format and what to do when unsure.
- Explain why each rule exists; current models generalize from the reason and over-apply rules written in capitals.
- Put long documents above the instructions and the question last, and keep the stable part of the prompt first so it can be cached.
- A system prompt is not a secret or a security control: enforce permissions in code, not in prose.
- Test every change against a fixed set of real and adversarial inputs before you ship it.
How do you write a system prompt?
To write a system prompt, describe the model's role, give it the context it can't guess, state the rules with a reason for each, show two or three examples, define the output format, and say what to do when it is unsure. Then test it on real inputs and revise. That is the whole method; the rest of this guide is how to do each step well.
A system prompt (OpenAI now calls it the developer message, Google calls it a system instruction) is text your application sends to the model ahead of the conversation. It stays in place for every turn, so it shapes every answer: tone, scope, format, what the model refuses, and which tools it reaches for.
Anthropic's prompting best practices frame the job well: think of the model as "a brilliant but new employee who lacks context on your norms and workflows." A good system prompt reads like the onboarding note you would hand that person on day one. If a colleague with no background would be confused by it, the model will be too.
If you are new to prompting in general, start with our prompt engineering guide; this post is about the one prompt that sits underneath everything else.
What goes in a system prompt? The six parts
A system prompt that works has six parts: role, context, rules, examples, output format and an uncertainty policy. Short prompts may fold some together, but each one answers a question the model would otherwise guess at.
| Part | Question it answers | Example line |
|---|---|---|
| Role | Who are you and for whom? | "You are the support assistant for Acme's invoicing app, talking to small business owners." |
| Context | What do you need to know that you can't infer? | Product facts, user plan, today's date, links to docs |
| Rules | What must you always or never do, and why? | "Don't promise refunds; only billing staff can approve them." |
| Examples | What does a good answer look like? | Two or three input and output pairs in <example> tags |
| Output format | What shape should the answer take? | "Plain text, under 120 words, one link at most." |
| Uncertainty | What if you don't know or the request is out of scope? | "Say you're not sure and offer to connect a human." |
OpenAI's prompt engineering guide recommends almost the same order for a developer message: Identity, Instructions, Examples, then Context, which it says "is usually best positioned near the end of your prompt."
Role: one specific sentence
A role focuses tone and judgment. Anthropic's docs note that even a single sentence in the system prompt makes a difference. Make it specific: "You are a senior Python reviewer for a fintech team" beats "You are a helpful assistant", because it implies standards, vocabulary and what to care about.
Rules: say why, and say what to do instead
Rules are where most system prompts go wrong. Three habits fix most of it:
- Give the reason. Anthropic's example: instead of "NEVER use ellipses", write "Your response will be read aloud by a text-to-speech engine, so never use ellipses since the text-to-speech engine will not know how to pronounce them." The model can generalize from the reason to cases you didn't list.
- State the positive form. "Write in flowing prose paragraphs" works better than "Don't use markdown."
- Drop the shouting. Anthropic reports that Claude models from Opus 4.5 on are more responsive to the system prompt than earlier ones, so prompts written to overcome undertriggering ("CRITICAL: You MUST use this tool when...") now cause overtriggering. Write "Use this tool when..." instead.
Examples: few, varied, tagged
Examples are the most reliable way to steer format and tone. Anthropic recommends 3 to 5 examples that are relevant, diverse enough to cover edge cases, and wrapped in <example> tags so the model can tell them apart from instructions. If all your examples share an accidental pattern (same length, same opening word), the model will copy it.
Where does the system prompt go in the Claude, OpenAI and Gemini APIs?
Each major API has a dedicated place for the system prompt, separate from the user's messages. The names differ, and OpenAI has two options with different lifetimes.
| API | Field | Scope |
|---|---|---|
| Anthropic Messages API | system (string or list of text blocks) | Whole request; resend each call |
| OpenAI Responses API | instructions parameter | Current response only |
| OpenAI Responses API | message with role developer | Part of the message list you send; prioritized ahead of user |
| Google Gemini API | system_instruction | Whole request |
OpenAI's guide is explicit that the instructions parameter "only applies to the current response generation request", so for multi-turn chats that pass history along, a developer message is the better fit. OpenAI compares developer and user messages to "a function and its arguments in a programming language."
A minimal Claude call looks like this:
import anthropicclient = anthropic.Anthropic()message = client.messages.create( model="claude-sonnet-5-5", max_tokens=1024, system="You are a senior Python reviewer for a fintech team. ...", messages=[{"role": "user", "content": "Review this function: ..."}],)print(message.content[0].text)Chat apps have the same layer under a different name. ChatGPT's custom instructions and the instructions in a Claude project are standing instructions added on top of the vendor's own system prompt. Anthropic publishes the system prompts used in claude.ai and its mobile apps in its release notes, and they are worth reading as long, real-world examples.
How should you structure a long system prompt?
Structure a long system prompt with clear sections (XML tags or Markdown headings), put large reference material at the top, and put the stable parts first so they can be cached. Order affects both quality and cost.
Use tags or headings. Anthropic recommends wrapping each type of content in its own tag, such as <instructions>, <context> and <input>, with consistent, descriptive names. OpenAI suggests Markdown headers for sections and XML tags to mark where a document starts and ends. Either works; mixing styles at random does not.
Long documents go above the instructions. For inputs of 20,000 tokens or more, Anthropic advises placing the documents near the top and the query and instructions at the end, and reports that queries at the end "can improve response quality by up to 30 percent in tests," especially with several documents.
Stable content goes first. Prompt caching works on prefixes: on Anthropic's API the cache covers tools, system and messages in that order, up to a breakpoint. If you put today's date or the user's name in the first line, every request is a cache miss. Keep the fixed role and rules at the top and the per-request details later.
Our guide to prompt caching covers breakpoints and minimum lengths. For a long agent prompt sent hundreds of times a day, caching is often the largest cost saving available.
A system prompt template for an assistant
Use this template for a customer-facing or internal assistant: fill in the brackets, delete sections you don't need, and keep the reasons next to the rules.
You are [role] for [company or team]. You help [audience] with [scope].<context>- Product: [two or three sentences on what it does]- Plans and limits: [facts the assistant will be asked about]- Today's date is provided in each user message.</context><rules>- Answer only questions about [scope]. For anything else, say it's outside what you can help with and suggest [where to go], because users trust answers that stay inside our docs.- Never promise refunds, discounts or deadlines. Only [team] can approve them, so offer to open a ticket instead.- If an answer depends on the user's account, ask for [identifier] rather than guessing.- Quote the help article you used and link it, so users can check.</rules><format>Plain text, at most [N] words. Use a numbered list only for steps.Write the way a friendly, competent colleague would speak.</format><when_unsure>If the docs don't cover the question, say so plainly and offer to connecta human. Don't fill gaps with guesses.</when_unsure><examples><example>User: [realistic question]Assistant: [ideal answer]</example><example>User: [out-of-scope or tricky question]Assistant: [ideal refusal or handoff]</example></examples>The second example matters more than the first. Showing one ideal refusal or handoff teaches the boundary faster than another paragraph of rules.
A system prompt template for an AI agent
An agent's system prompt needs everything an assistant's does, plus guidance on tools, autonomy and when to stop. Agents act, so the prompt has to say which actions are safe to take alone and which need a human.
You are a coding agent working in [repository] for [team].Goal for each task: a small, reviewed change that passes the test suite.<environment>- Run tests with: [command]. Lint with: [command].- Main branch is protected. Work on a new branch per task.</environment><tools>Use the search tool before guessing file locations. If you intend to callseveral tools and none depends on another, call them in parallel.</tools><autonomy>Consider the reversibility and potential impact of your actions. Takelocal, reversible actions like editing files or running tests on your own.Ask before actions that are hard to reverse, affect shared systems, orcould be destructive: deleting branches, force-pushing, dropping tables,posting comments or sending messages.</autonomy><scope>Only make changes that are directly requested or clearly necessary. Don'tadd features, refactor unrelated code or add configurability nobody askedfor.</scope><done>You are done when tests and lint pass and you have written a short summaryof what changed and why. If you can't finish, stop and explain whatblocked you instead of working around it.</done>The autonomy and scope blocks adapt sample prompts from Anthropic's best practices, which note that without guidance some models take hard-to-reverse actions or overengineer. For coding agents such as Claude Code, the project-level instructions live in a CLAUDE.md file that is loaded into context at the start of a session; our CLAUDE.md best practices cover what to put there. The broader discipline of deciding what goes into the model's window at each step is context engineering.
If those agents run unattended for hours, the system prompt is your main lever on their behavior while you are away. Orkio gives Claude Code and Codex an always-on cloud box where they keep working after you close your laptop, on your own Claude account with no markup on model usage, so a well-tested agent prompt can do a night's work and leave you pull requests to review.
What should never go in a system prompt?
Never put secrets, credentials or access-control logic in a system prompt, because users can often extract it. OWASP's entry on system prompt leakage puts it directly: "the system prompt should not be considered a secret, nor should it be used as a security control."
Keep these out:
- API keys, database names, internal URLs and connection strings.
- Permission logic such as "only admins may see salary data." Enforce that in the code that fetches data, so the model never receives what the user can't see.
- Content you would be embarrassed to see in a screenshot on social media.
The same applies to agents that read web pages, issues or emails: text in those inputs can carry instructions that compete with yours. Our guide to prompt injection in coding agents covers the defenses that work, which live mostly outside the prompt: sandboxing, least-privilege tokens and human review.
Prefilling the start of Claude's answer is no longer supported. Since the Claude 4.6 models, a request whose last message is a partial assistant turn returns a 400 error. Use a direct instruction ("Respond without preamble") or structured outputs instead.

How do you test and improve a system prompt?
Test a system prompt by running it against a fixed set of inputs with clear pass criteria, changing one thing at a time, and comparing results before you ship. Reading a few chat transcripts and deciding it "feels better" doesn't count as a test.
Anthropic's guide to defining success criteria says good criteria are specific, measurable, achievable and relevant. "Safe outputs" is a bad criterion; a measurable rate of flagged outputs over a fixed number of trials is a good one. A practical loop:
- Write 20 to 50 test inputs before you tune anything: common questions, edge cases, out-of-scope requests, and a few attempts to break the rules or extract the prompt.
- Define pass criteria per input: the facts that must appear, the format, the refusal you expect.
- Run the current prompt and record the results as your baseline.
- Change one thing (a rule, an example, the order) and rerun the whole set.
- Keep the change only if the pass rate goes up without new failures elsewhere. Delete lines that make no measurable difference.
- Grade at scale with code or a second model once the set grows: exact-match checks for format, a model with a rubric for tone.
- Rerun the set whenever you switch models. Anthropic publishes a separate prompting page for each current model because behavior shifts between versions.
Our post on LLM evaluation goes further into building eval sets and graders.
Common mistakes and their fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Answers are too long | No length or format rule | Give a word limit and an example of the right length |
| Model ignores a rule | Rule buried in a long paragraph, or no reason given | Move it into the rules section and explain why |
| Tool called on every turn | Aggressive "MUST use" wording | Describe when the tool helps, in normal language |
| Same phrasing in every reply | Examples too similar | Vary length, structure and opening of examples |
| Prompt costs too much | Dynamic data at the top breaks caching | Stable text first, per-request data last |
| Model only suggests, doesn't act | Request phrased as a question | Tell it to make the change, or set a default-to-action rule |
Your system prompt checklist
Before you ship a system prompt, check it against this list:
- The first lines state a specific role and audience.
- Every rule says why, and is phrased as what to do.
- There are 2 to 5 varied examples, including one boundary case.
- The output format is explicit: length, structure, markup.
- There is a clear instruction for "I don't know" and out-of-scope requests.
- Long documents sit above the instructions; stable text sits first.
- No secrets, internal URLs or permission logic appear anywhere.
- Agents have explicit rules on irreversible actions and a definition of done.
- A test set with pass criteria exists, and the prompt passes it.
Start with the template closest to your use case, write the test inputs before you tune, and treat the prompt like code: versioned, reviewed and rerun when the model changes.
Frequently asked questions
What is the difference between a system prompt and a user prompt?
The system prompt is written by the developer and applies to the whole conversation: role, rules, tone and format. The user prompt is the individual request a person types. OpenAI's docs describe developer messages as prioritized ahead of user messages, and Anthropic's API takes the system prompt as a separate system parameter.
How long should a system prompt be?
As long as it needs to be to cover the role, rules, examples and format, and no longer. A narrow support bot needs far less than a coding agent that must know tools, environments and safety rules. Every token is sent with every request, so cut lines that your tests show change nothing and use prompt caching for long, stable prompts.
Can users see or extract my system prompt?
Assume they can. Prompt injection and plain asking can often get a model to reveal its instructions, and OWASP lists system prompt leakage as a top risk for LLM applications. Keep API keys, internal URLs and permission logic out of the prompt and enforce access rules in your own code.
Should I write a system prompt in Markdown or XML?
Both work. OpenAI's guide suggests Markdown headers and lists to mark sections and XML tags to wrap documents, and Anthropic's docs recommend XML tags such as instructions, context and example for Claude. Pick one style, use descriptive tag or heading names, and keep it consistent across prompts.
Do ChatGPT and Claude have system prompts?
Yes. The consumer apps run their own system prompts, and Anthropic publishes the ones used in claude.ai and the Claude mobile apps in its release notes. When you write custom instructions in ChatGPT or project instructions in Claude, you are adding your own layer of standing instructions on top.
SOURCES
- Anthropic: Prompting best practices
- Anthropic: Prompt caching
- Anthropic: Define success criteria and build evaluations
- Anthropic: System prompts release notes
- OpenAI: Prompt engineering guide
- Google: Gemini API text generation (system instructions)
- OWASP Top 10 for LLM Applications: LLM07:2025 System Prompt Leakage


