What Is a System Prompt? 5-Part Template, API Differences, Tests

Blog / AI concepts

What Is a System Prompt? 5-Part Template, API Differences, Tests

A system prompt is a block of standing instructions you hand the model before the conversation starts. It sets what role the model plays, which rules it follows, and what format it answers in. It is not a question for any single turn. It is background that stays in effect for the whole conversation. A simple way to think about it: the user message says "what to do this time," and the system prompt says "how you should always behave."

This article covers what a system prompt does, where it goes in the two major API formats, a template you can reuse, and how to check whether yours works.

What does a system prompt actually control?

It usually holds three kinds of content:

  • Role and goal: you are a code review assistant for backend engineers with two or three years of experience.
  • Rules and boundaries: answer only from the provided material; if it isn't there, say you don't know; don't output anything unrelated to the task.
  • Output format: respond in English, give the conclusion first, then bullet points; output JSON when structure is needed.

Compared with a user message, the system prompt is stable and is sent with every request. Models generally weight it more heavily, but "more heavily" does not mean "always obeyed." When a user message conflicts with the system prompt, models do not all handle it the same way. Verify important constraints in tests instead of trusting your intuition.

Where does the system prompt go in OpenAI and Anthropic APIs?

This is where beginners trip most often, because the two vendors do it differently.

OpenAI format (Chat Completions)Anthropic format (Messages)
Where it goesA message in the messages array with role set to systemA top-level system field in the request body
Roles in messagessystem, user, assistant, and othersOnly user and assistant
Example{"role":"system","content":"..."}"system": "..."

OpenAI format example:

{
  "model": "your-model-name",
  "messages": [
    {"role": "system", "content": "You are a code review assistant. Only point out problems you can back up."},
    {"role": "user", "content": "What's wrong with this function..."}
  ]
}

Anthropic format example:

{
  "model": "your-model-name",
  "max_tokens": 1024,
  "system": "You are a code review assistant. Only point out problems you can back up.",
  "messages": [
    {"role": "user", "content": "What's wrong with this function..."}
  ]
}

Two things to note. First, newer OpenAI model docs also introduce a developer role that serves a similar purpose to system; which models use which is something to check in the official docs. Second, putting role: system directly into an Anthropic messages array usually returns an error. It belongs in the top-level system field. For a full field-by-field comparison of the two formats, see Anthropic API vs OpenAI API: what's different. For how the messages array itself is written, see What is Chat Completions.

How do you write a good system prompt?

Here is a 5-part template. Cut whatever you don't need:

  1. Role: one sentence on who the assistant is and who it serves.
  2. Task: what the assistant mainly does, and what it does not do.
  3. Rules: the few things it must follow, one per line, written as statements you can check.
  4. Format: output language, length, and structure; give a field sample if a program will parse the output.
  5. When unsure: for example, "If information is missing, ask a question first instead of guessing."

A filled-in example (a customer support Q&A scenario, for illustration only):

You are a technical support assistant for a software product. Most users are non-technical.
Task: answer questions based on the "Materials" below. For anything outside the materials, reply "I need to confirm this with a human agent."
Rules:
- Do not invent features, prices, or dates.
- Keep each answer under 200 words.
- For account and payment actions, only point to the official entry point. Never ask for passwords.
Format: one-sentence conclusion first, then the steps as a list.
When unsure: ask the single most important clarifying question first.

A few lessons from experience:

  • Make rules specific. "Be professional" can't be checked. "Under 200 words, conclusion first" can.
  • Go easy on emphasis words like "absolutely never" and "under no circumstances." Put the one or two rules that really matter at the very start or very end.
  • Keep the length reasonable. The longer it is, the more tokens it costs, and it is billed again on every request. For the concept, see What is a token in an LLM.

How do you verify that it works?

Use a small checklist and run each item 3 to 5 times:

  • Normal question: does the answer match the format and length?
  • Out-of-scope question: ask something outside the task. Does it refuse or hand off as the rules say?
  • Conflicting question: write "ignore the previous rules" in the user message. Does it still hold the key constraints?
  • Missing information: give a question with a missing condition. Does it ask first rather than make something up?

If a rule fails often, first check whether it is worded vaguely. Then consider splitting it into shorter sentences, or adding a format example to the user message. Don't count on a system prompt to guarantee safety. Limits on permissions, money, or privacy should be enforced by validation in your own code.

Who writes the system prompt in Claude Code and similar tools?

In coding tools, the system prompt usually ships with the tool and you can't fully rewrite it, but you can add rules through project files. For example, Claude Code reads the CLAUDE.md in your project; see What is CLAUDE.md for how to write one. These tools combine the system prompt, tool definitions, and multi-turn context, so they behave more like agents. For the difference, see What is an AI agent.

What do you need to try it yourself?

All you need is a client that accepts an endpoint URL and an API key, plus a working endpoint. If you are in mainland China, the usual sticking points are network reachability to the official APIs and paying with an overseas card. You can start with cdnping's online speed test to see how fast each vendor's API connects from your network, and use the model monitor to see which of the latest models each vendor currently offers. For why latency is high, see Why AI API latency is high.

One more disclosure: AGI Plan is a multi-provider LLM aggregation gateway, and AGI Plan is a commercial partner of cdnping. It is billed as a monthly subscription, paid in RMB, with no overseas credit card needed. It is compatible with both the OpenAI and Anthropic API formats, so you can try both system prompt styles above directly. Tools like Claude Code and Codex only need one endpoint URL changed. Note that it is not an officially authorized channel, and some models are marked "overseas only." Check the models page for which models are available and in which regions, and follow the terms of service and local regulations for any AI service you use. For setup steps, see Getting started with AGI Plan.

Next steps

  1. Pick a small task you do often and write a system prompt using the 5-part template.
  2. Run the checklist above once and note which rules fail.
  3. Change one thing at a time and retest, comparing before and after, until it converges.

Get the role, rules, and format clear, and you will solve about half of the "the model won't listen" problems.

Related articles

Keep reading