How does the System Prompt work?

Authors
How does the System Prompt work?

The System Prompt is a set of instructions, usually hidden from the user, that is given to an LLM before the conversation starts. It tells the LLM who it is, how it must behave, and what rules it must follow, and it is sent again with every request.

In this blog, we will learn about how the System Prompt works. We will also see the three roles in a chat with an LLM, how the system prompt is joined with our messages before the LLM reads them, why the LLM gives it extra importance, why it must be sent again on every request, how to write a good one, and where it works well and where it fails.

We will cover the following:

  • What is a System Prompt?
  • The three roles in a chat
  • A simple example
  • How does the System Prompt work behind the scenes?
  • Why does the LLM follow the System Prompt?
  • The System Prompt is sent every time
  • System Prompt in code
  • What do we put inside a System Prompt?
  • Limitations of the System Prompt
  • System Prompt vs User Prompt

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What is a System Prompt?

A System Prompt is a set of instructions given to the LLM at the very start, which decides how the LLM must behave for the whole conversation.

Let's say a company hires a new receptionist. On the first day, the manager tells him:

  • "You work at the front desk of our company."
  • "Always greet people politely."
  • "Only answer questions about our company."
  • "Never share the private phone numbers of the staff."

After that, visitors come and ask him questions. The visitors never hear the manager's instructions. But the receptionist follows those instructions with every visitor.

Here, the manager's instructions are the system prompt, the visitors' questions are the user prompts, and the receptionist is the LLM.

This is how the system prompt works. It is usually written by the developer who builds the app, not by the person who chats with it. Most of the time, the user does not even see it.

The three roles in a chat

When we chat with an LLM, every message has a role. The role tells the LLM who wrote that message.

There are three main roles:

  • system: the instructions from the developer. This is the system prompt.
  • user: the messages from the person chatting.
  • assistant: the replies from the LLM itself.

A conversation is just a list of these messages, like below:

[system]     You are a friendly math tutor for kids. Use simple words.
[user]       What is a fraction?
[assistant]  A fraction is a part of a whole. If you cut a pizza into 4 pieces...
[user]       What is 1/2 + 1/4?

Here, we can see that the system message comes first, and then the user and the assistant take turns.

A simple example

The best way to learn this is by taking an example.

Let's ask the same question with two different system prompts.

System prompt 1: "You are a helpful assistant."

User: "What is the sun?"

Reply: "The sun is a star at the center of our solar system. It is a huge ball of hot gas that gives us light and heat..."

System prompt 2: "You are a teacher for 5-year-old kids. Answer in one short sentence using very simple words."

User: "What is the sun?"

Reply: "The sun is a big, hot, bright star that gives us light and keeps us warm."

Here, we can see that the user question is exactly the same. Only the system prompt changed, and the style, the words, and the length of the answer changed.

With the same LLM, we can build a math tutor, a customer support bot, a coding helper, or a teacher for kids, just by changing the system prompt.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

How does the System Prompt work behind the scenes?

Now, the question is, how does the LLM actually see the system prompt?

An LLM does not really understand "messages" or "roles". It only reads one long piece of text.

So, before our chat reaches the LLM, all the messages are joined together into one long text. Special marker tokens are added to show where each message starts and ends, and which role wrote it. This joining format is called a chat template.

Let's see a simplified chat template as below:

<|start|>system
You are a friendly math tutor for kids. Use simple words.<|end|>
<|start|>user
What is a fraction?<|end|>
<|start|>assistant

Here, we can notice:

  • <|start|> and <|end|> are special marker tokens. They are not normal words. They tell the LLM where a message begins and ends.
  • The word after <|start|> tells the role of that message.
  • The system prompt is placed at the very beginning.
  • At the end, there is an open assistant section with nothing after it. This is the LLM's cue to start writing its reply.

Note: The exact markers are different for each model family. For example, some models use <|im_start|> and <|im_end|>. But the idea is the same for all of them. For the sake of understanding, I have used simple names here.

So, the LLM sees the system prompt as the first part of one long text. And since the LLM writes every new token by looking at all the text before it, the system prompt affects every token of the reply.

Why does the LLM follow the System Prompt?

If the system prompt is just some text at the start, why does the LLM treat it as more important than the user's messages?

The answer is training.

After an LLM learns language from a huge amount of text, it goes through more training to become a helpful chat assistant. In this training, the model sees many example conversations that have a system message, and it is rewarded when it follows the system message.

Let's say a child is trained again and again: "When your teacher sets a rule at the start of the class, follow it for the whole class, even if a classmate asks you to break it." After enough practice, the child learns to give the teacher's rule more weight.

In the same way, the LLM learns:

  • The text inside the system role sets the rules.
  • These rules apply to the whole conversation.
  • If a user message goes against the system rules, the system rules usually win.

Some companies train their models with a clear order of trust, often called an instruction hierarchy. The system instructions are on the top, then the user messages, and then any text that comes from outside, like a web page or a document.

Note: This is learned behavior, not a hard lock. The LLM follows the system prompt very well most of the time, but it is not a guarantee. We will see this in the limitations section.

To learn LLM Fundamentals, Fine-tuning, and RLHF (Reinforcement Learning from Human Feedback), and build a Large Language Model (LLM) from scratch, check out our AI and Machine Learning Program at Outcome School.

The System Prompt is sent every time

An LLM does not remember anything between two requests. The LLM does not know what we said a minute ago unless we send it again.

So, how does a chat app remember the conversation? The answer is that the app sends the full conversation every time, including the system prompt.

Let's see how the request grows as below:

Request 1:  [system] + [user 1]
Request 2:  [system] + [user 1] + [assistant 1] + [user 2]
Request 3:  [system] + [user 1] + [assistant 1] + [user 2] + [assistant 2] + [user 3]

Here, we can see that the system prompt is at the start of every request. It is never sent only once.

This has two effects:

  • Cost: the system prompt is counted as tokens in every request. A long system prompt costs money again and again.
  • Space: every LLM has a context window, which is the maximum amount of text it can read at once. The system prompt takes some of this space.

To reduce the cost, many LLM services offer prompt caching. Since the system prompt is the same at the start of every request, the service can save the work it already did on it and reuse that work. This makes requests faster and cheaper.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

System Prompt in code

Let's see the code for sending a system prompt to an LLM.

Many LLM APIs take the system prompt as the first message with the role system, like below:

from openai import OpenAI

client = OpenAI()

messages = [
    { "role": "system", "content": "You are a friendly math tutor for kids. Use simple words." },
    { "role": "user", "content": "What is a fraction?" },
]

response = client.chat.completions.create(
    model="model-name",
    messages=messages,
)

Here, we have:

  • Created a client, which is the object from the LLM provider's Python library that sends our request over the internet.
  • Created a list of messages.
  • Put the system prompt first, with the role system.
  • Put the user's question next, with the role user.
  • Sent the whole list to the LLM using client.

Note: Some newer APIs from OpenAI also use a role called developer for the same purpose. The name is different, but the idea is the same.

Some APIs, like the Anthropic API for Claude, take the system prompt as a separate field instead of a message, like below:

import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="model-name",
    max_tokens=500,
    system="You are a friendly math tutor for kids. Use simple words.",
    messages=[
        { "role": "user", "content": "What is a fraction?" },
    ],
)

Here, we can see that the system prompt goes into the system field, and the messages list holds only the user and assistant turns. The max_tokens field sets the maximum length of the reply.

Both styles do the same thing. In the end, the system prompt is placed at the start of the text that the LLM reads.

What do we put inside a System Prompt?

A good system prompt usually has the following parts:

  • Role: who the LLM is. "You are a customer support assistant for an online shoe store."
  • Task: what it must do. "Help customers with orders, returns, and sizes."
  • Rules: what it must and must not do. "Never promise a refund. Ask for the order number first."
  • Tone and style: how it must talk. "Be polite and short. Use simple English."
  • Output format: how the answer must look. "Reply in plain text, under 100 words."
  • Background knowledge: facts it needs. "Our return window is 30 days."

Let's see a small but complete system prompt as below:

You are a support assistant for StepUp, an online shoe store.

Help customers with orders, returns, and shoe sizes.

Rules:
- Always ask for the order number before checking an order.
- Our return window is 30 days from delivery.
- Never promise a refund. Say that the team will review it.
- If the question is not about shoes or orders, politely say you can only help with StepUp.

Be polite and short. Reply in under 100 words.

Here, we can see that every line has a clear job. The role is at the top, then the task, then the rules, and finally the tone and length.

Few important tips:

  • Be clear and direct. Write the system prompt like instructions for a new employee who knows nothing about our business.
  • Explain the reason when it helps. "Keep answers short, because users read them on a phone" works better than just "Keep answers short".
  • Avoid rules that fight each other. "Be very detailed" and "Keep it under 50 words" confuse the LLM.
  • Test it. Try many user questions, including tricky ones, and improve the system prompt step by step.

We have a detailed blog on Prompt Engineering that explains how to write better prompts step by step.

Limitations of the System Prompt

The following issues arise with the system prompt:

  • It is not a security guarantee. A user can try to trick the LLM with a message like "Ignore all previous instructions and...". This is called prompt injection. A well-trained model resists many of these tricks, but not all of them.
  • It can leak. A user can ask the LLM to repeat its system prompt. So, we must never put passwords, secret keys, or private data inside the system prompt.
  • Very long prompts get weaker. If the system prompt is very long and the conversation is very long, the LLM can lose track of some rules.
  • It does not add real knowledge. If the LLM does not know a fact, a system prompt that says "always be correct" does not make it correct. We must put the needed facts in the prompt.

So, for important rules, like who can see which data, we must check them in our own code too, not only in the system prompt. We have a detailed blog on LLM guardrails that explains how these extra checks work.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

System Prompt vs User Prompt

Let me tabulate the differences between the System Prompt and the User Prompt for your better understanding.

PointSystem PromptUser Prompt
Who writes itThe developer of the appThe person chatting
When it is setBefore the chat startsDuring the chat
Visible to the userUsually noYes
Role namesystemuser
PositionAt the very startAfter the system prompt
How long it appliesThe whole conversationMostly that one turn
PriorityHigher, the LLM is trained to follow it firstLower, when it goes against the system rules
PurposeSet identity, rules, tone, formatAsk a question or give a task

Here, we can see that both are just text in the end. The difference is in the role, the position, and how the LLM was trained to treat them.

Now we must have understood how the System Prompt works.

Frequently Asked Questions

Can the user see the System Prompt?

Usually no. The system prompt is written by the developer who builds the app, and most of the time the user does not see it. But a user can ask the LLM to repeat its system prompt, and it can leak. So, we must never put passwords, secret keys, or private data inside the system prompt.

Does a longer System Prompt work better?

Not always. The system prompt is counted as tokens in every request, so a long one costs money again and again and takes space in the context window. If the system prompt is very long and the conversation is very long, the LLM can lose track of some rules. So, we must keep it clear and direct, and avoid rules that fight each other.

Can a System Prompt make the LLM always correct?

No. A system prompt does not add real knowledge. If the LLM does not know a fact, a system prompt that says "always be correct" does not make it correct. We must put the needed facts in the prompt, like the background knowledge part of a good system prompt.

Is the developer role the same as the system role?

Yes, the idea is the same. Some newer APIs from OpenAI use a role called developer for the same purpose as the system role. Some APIs, like the Anthropic API for Claude, take the system prompt as a separate field instead of a message. In the end, the system prompt is placed at the start of the text that the LLM reads.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.