Search

Search papers and lessons

System Prompt

Level 2 · 10 min read

A system prompt is a block of instructions that an application puts in front of every conversation, so the model's role, tone and limits are set before the user types anything.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Algorithms
  • Losses & regularization
  • Pre-training
  • ReadReady to readNot yet

TL;DR

A system prompt is a block of instructions that the application places at the start of every request, before the user's message. The model reads it as ordinary text, and it shapes every reply that follows: the role the model plays, the tone, the topics it should refuse, and the format of its answers. The user usually cannot see it, and it is not a setting stored inside the model. It is text that is sent again with every request.

Builds on: Language Model, In-Context Learning · Leads to: Alignment faking in large language models

Where it's used

A customer-support chat. A company puts a system prompt in front of every customer conversation. It says: answer questions about our products only, use a friendly tone, and pass billing disputes to a human agent. The customer never sees these lines, but every reply the bot gives is written in their light.

A coding assistant. Tools that help programmers edit code keep a long system prompt that lists the rules for changing files and the tools the model may call, such as "run the tests before reporting success". Changing that text changes how the assistant behaves without retraining anything.

A children's learning app. The same underlying model can be deployed in a reading app for ten-year-olds with a system prompt that limits topics, vocabulary and length. The app's designers use the system prompt as the first layer of product behaviour, and they add checks in code for anything that must always hold. The system prompt alone is not a guarantee, as the common mistakes below explain.

How it works

Take one tiny app. Its system prompt is "Reply in one word." The user types "Is the sky blue?" and the model answers "Yes." Here is what happens between the two.

  1. The application builds the input. It joins three pieces into one string: the system prompt, a marker that says the user is speaking, and the user's message, then a marker that says the model should answer. The pieces are plain text, so the model will see all of them as one document. Check: the system prompt comes first, and it is in every request, not only the first one.
  2. The string is split into tokens. A tokenizer cuts the text into pieces from a fixed vocabulary (see Subword Tokenization). Pretend the tokenizer returns 6 tokens for the system prompt, 5 for the user's message and 3 for the markers, 14 in all. Each token is then turned into a vector of numbers. Check: the 14 tokens include the words "Reply in one word", even though the user never typed them.
  3. The transformer reads every token. Its attention layers (Attention Mechanism) let each position look back at all the earlier positions. So when the model is about to write its answer, the instruction "one word" is one of the things it attends to. Check: the instruction has no special channel. It is just earlier text that the attention layers can use.
  4. The model scores every possible next token. It produces a score for each token in its vocabulary, and a softmax (Softmax) turns the scores into probabilities. Suppose the instruction makes the probabilities look like this: "Yes" 0.60, "Yes," 0.10, "Sure" 0.05, and everything else together 0.25. The probabilities add up to 1. Check: the long answers are not impossible, but they now have low probability.
  5. A token is chosen and appended. The simplest rule picks the most likely token, "Yes". It is added to the input, and the model runs again to choose the next token. It picks the end-of-reply marker, and the reply is "Yes". Check: the weights of the model did not change between steps 1 and 5. Only the probabilities did.
flowchart LR
  S["System prompt"] --> J[Joined input]
  U["User message"] --> J
  J --> T[Tokenizer]
  T --> M["Transformer reads all tokens"]
  M --> P["Next-token probabilities"]
  P --> C["Choose one token"]
  C -->|append and repeat| M

The math

Definition: System prompt

Let ss be the sequence of tokens in the system prompt and uu the sequence of tokens in the user's message. The model conditions on the whole context c=(s,u)c = (s, u) and assigns a probability to each reply y=(y1,…,yT)y = (y_1, \dots, y_T): p(y∣s,u)=∏t=1Tp(yt∣s,u,y1,…,yt−1)p(y \mid s, u) = \prod_{t=1}^{T} p(y_t \mid s, u, y_1, \dots, y_{t-1}) The system prompt is the part ss of the conditioning that the application fixes and the user does not write.

Step 2 in symbols: counting tokens

Let ns=6n_s = 6 be the system prompt tokens, nu=5n_u = 5 the user's tokens and nm=3n_m = 3 the marker tokens. The input length is N=ns+nu+nm=6+5+3=14.N = n_s + n_u + n_m = 6 + 5 + 3 = 14.

Step 4 in symbols: the softmax

The model produces a score zvz_v for each vocabulary token vv. The softmax turns the scores into probabilities: p(v∣context)=ezv∑wezw.p(v \mid \text{context}) = \frac{e^{z_v}}{\sum_{w} e^{z_w}}. The system prompt changes the scores zvz_v through attention. It does not appear as a separate term in this formula.

Step 5 in symbols: choosing a token

Greedy decoding picks the token with the largest probability, yt=arg⁡max⁡v  p(v∣s,u,y1,…,yt−1).y_t = \arg\max_{v} \; p(v \mid s, u, y_1, \dots, y_{t-1}). Sampling instead draws yty_t at random with those probabilities. The probability of a whole reply is the product of its token probabilities, as the definition shows. Each factor is at most 1, so a reply of several tokens is less likely than any one of its token probabilities taken alone (see Conditional Probability).

Worked examples

Example
A one-word rule

System prompt: "Answer with a number." User: "What is 2 plus 3?"

  1. The instruction is part of the context, as in the walkthrough.
  2. Suppose the next-token probabilities are "5" 0.70, "five" 0.20, and everything else 0.10.
  3. Greedy decoding picks the largest, "5".
  4. The model then picks the end marker, so the reply is "5".

Interpretation: the instruction changed the probabilities from a long sentence toward a bare number. Nothing in the model was edited.

Example
A token budget for the system prompt

A chat product has a context limit of 4,096 tokens. Its system prompt is 60 tokens, the user's message is 25 tokens, and the markers add 6 tokens.

  1. Prompt length: 60+25+6=9160 + 25 + 6 = 91 tokens.
  2. Room left for the reply: 4,096−91=4,0054{,}096 - 91 = 4{,}005 tokens.
  3. The app handles 1,000 requests a day. Each one resends the 60-token system prompt, so the system prompt alone costs 1,000×60=60,0001{,}000 \times 60 = 60{,}000 tokens a day.

Interpretation: the system prompt takes space from every request, and it is paid again each time. A short, clear prompt saves both room and cost.

Example
A support bot on its fourth turn

A support bot has a 180-token system prompt. Each user message averages 35 tokens and each reply averages 120 tokens. The fourth request in a conversation resends the system prompt, the three earlier exchanges, and the new message.

  1. System prompt: 180 tokens.
  2. Three earlier exchanges, each 35+120=15535 + 120 = 155 tokens: 3×155=4653 \times 155 = 465 tokens.
  3. The new user message: 35 tokens.
  4. Total for the fourth request: 180+465+35=680180 + 465 + 35 = 680 tokens.

Interpretation: the earlier turns cost more than the system prompt by this point. The system prompt is a fixed cost that every request carries, while the history grows.

Try it: in the fourth request, what share of the 680 tokens is the system prompt? Answer: 180/680≈0.265180 / 680 \approx 0.265, so about 26 percent.

Common mistakes and tradeoffs

It is tempting to think a system prompt is a secret the model will keep. In fact the model reads it as ordinary context, and a user can sometimes get the model to repeat or describe it. Treat everything in a system prompt as potentially visible, and do not put passwords or private keys in it.

It is tempting to think the system prompt is a rule the model always obeys. In fact it changes probabilities, so a user message can push the reply away from it, especially with a clever or contradictory request. For anything that must hold, such as "never issue a refund without approval", the application should enforce it in code with permission checks, and use the prompt only to make the normal behaviour likely.

It is tempting to think a longer prompt is always safer. In fact every token costs room and money on every request, and contradictory or padded instructions make replies less consistent. Keep the prompt to the rules that matter.

When to use a system prompt: for stable behaviour, tone, scope and output format that should apply to every conversation. When not to use it: for facts that change often (put them in retrieved documents instead), for guarantees (use code), and for behaviour that must be built into the model itself. For the last case, Instruction Tuning and Fine-Tuning change the weights, which a prompt cannot do. Examples placed in the conversation are a different tool, covered by In-Context Learning.

History

Early language models were trained to continue text. A prompt was one block of text, and there was no separate place for instructions addressed to the assistant. Papers on language models showed that examples in the prompt could steer the output, which made the prompt a control surface in its own right.

Training models to follow instructions made the instruction part of the input a model is expected to respond to. InstructGPT (Ouyang et al., 2022) was one of the influential demonstrations of this. In 2023, chat model interfaces formalised separate message roles, with a "system" role for the developer's instructions. That role became the common name for the block described here.

Recap

  • A system prompt is text that an application places before the user's message in every request.
  • The model reads it through attention, which changes the next-token probabilities. The weights do not change.
  • Each token of it counts against the context limit, and it is sent again on every request.
  • Because behaviour follows probabilities, a system prompt sets strong defaults, not hard rules.
  • Read next: Instruction Tuning for building instruction-following into the weights, and In-Context Learning for examples in the prompt. The alignment faking study uses system prompts in its experiments.

Check yourself

QuestionIn one sentence, what does a system prompt change, and what does it leave alone?Show answer

It changes the context the model conditions on, so the next-token probabilities shift toward the requested behaviour. It leaves the model's weights alone, so the model is not retrained by the prompt.

QuestionA system prompt has 80 tokens. A user message has 20 tokens and the markers add 4. What is the total input length, and how many tokens does the system prompt take per 500 requests?Show answer

The input length is 80+20+4=10480 + 20 + 4 = 104 tokens. Over 500 requests the system prompt contributes 500×80=40,000500 \times 80 = 40{,}000 tokens, because it is resent each time.

QuestionWhy can a user message sometimes override a system prompt, even though the system prompt comes first in the input?Show answer

The system prompt is only earlier text. Attention lets the model use it, but the next-token probabilities come from all the tokens together. A strong or contradictory user message can shift those probabilities away from the instruction. That is why rules that must always hold belong in code.

QuestionA children's reading app wants a rule that the model never discusses a topic outside its scope. Describe a design that does more than a system prompt alone.Show answer

Put the scope in the system prompt so that normal replies stay on topic. Then add a check outside the model, such as a classifier on the user's message and on the reply, that blocks or redirects anything out of scope. The prompt sets the default, and the code enforces the rule.

Sources

System Prompt — explained simply · ungoon.me