System prompts and chat templates
Course contents (20 lessons)
- Getting to know Meta AI and Llama
- What Meta AI is and where you can use it
- Meta AI versus Llama: the assistant and the models
- Your first chat with Meta AI
- Prompt basics that work
- The anatomy of a good prompt
- Context, tone, length and follow-ups
- Everyday uses
- Writing and replying to messages
- Captions, posts and bios
- Planning and study help
- Customer replies for a small business
- Images, groups and privacy
- Writing image generation prompts
- Group chats, etiquette and privacy
- Working with Llama models directly
- Running a Llama model on your own computer
- System prompts and chat templates
- Parameters: temperature, top_p and max tokens
- Few-shot examples and structured JSON output
- Advanced prompting
- Prompting Llama for code
- Retrieval-augmented prompting: answering from your own documents
- Evaluating and comparing answers
- Safety, limits and a final project
- Safety, limits and responsible use
- Final project: build a study buddy assistant
Three roles in a chat
When you chat with a Llama chat model, the text is split into roles:
- system: standing instructions that apply to the whole conversation. Who the assistant is, how it should answer, what it must not do.
- user: your messages.
- assistant: the model's replies.
In Meta AI, someone else wrote the system part. When you run a model yourself, it is yours to write, and it is the most powerful tool you have.
What a chat template is
A language model reads one long stream of text. To tell it where the system part ends and the user part begins, special marker tokens are inserted. The exact layout of those markers is called the chat template, and each model family has its own.
For the Llama 3 family, a conversation looks roughly like this:
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a patient maths tutor. Use short sentences.<|eot_id|><|start_header_id|>user<|end_header_id|>
What is a fraction?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
The model then continues from the empty assistant part and writes its answer. Older generations, such as Llama 2, used a different layout with [INST] and <<SYS>> markers. Newer versions may change again, so do not rely on my example. Check the model card for the version you use.
Do you need to type these markers?
Almost never. Tools such as Ollama and LM Studio know the right template for each model and add the markers for you. You write the system prompt in a text box, and your messages in the chat. The markers matter in two cases: when you write code that talks to a model through a raw text interface, and when answers come out strange. A wrong template, such as using Llama 2 markers with a Llama 3 model, can make a model ramble or ignore instructions. If that happens, check the template first.
Writing a good system prompt
A system prompt should be short and clear. I use this structure:
- Role: who the assistant is.
- Audience: who it is talking to.
- Rules: how to answer, what to avoid.
- Output format: the shape of every answer.
- What to do when unsure.
You are a support assistant for a small online bookshop.
You talk to customers who are not technical.
Rules:
- Answer in plain English, at most 80 words.
- Use only the shop information given in the conversation. Do not invent prices or dates.
- If you do not know, say "I am not sure, a team member will follow up" and ask for the order number.
Format: one short paragraph. No lists.
Why it works
Each line closes a door. The word limit controls length. The rule about shop information reduces guessing. The "if you do not know" line gives a safe exit. A system prompt like this will make the model behave the same way across hundreds of chats, which is the whole point.
Setting it in Ollama
Ollama lets you save a system prompt and settings in a small file called a Modelfile, then build a named model from it.
FROM llama3.2
SYSTEM """
You are a support assistant for a small online bookshop.
Answer in plain English, at most 80 words.
If you do not know, say so and ask for the order number.
"""
Save that as Modelfile, then build and run it:
ollama create bookshop-helper -f Modelfile
ollama run bookshop-helper
System prompt versus user prompt
Put things that stay the same in the system prompt: role, rules, format. Put the specific request in the user message. If the model keeps forgetting a rule, repeat the rule briefly at the end of the user message. Smaller models follow the latest instruction best.
Do not treat it as a lock
A system prompt is guidance, not a security wall. A determined user can often talk a model into ignoring it. Never put secrets in a system prompt, such as passwords or private keys, and do not rely on it to protect anything important.
The common mistake
The common mistake is a huge system prompt of fifty rules, many of them contradictory. Models, especially small ones, lose track. Start with five rules. Add one at a time when you see a real failure, and test after each change.
Try it yourself
Write a system prompt for an assistant that helps one specific person, such as a tutor for your younger brother. Include role, audience, three rules, format and the "if unsure" line. Test it with five questions, including one that it should refuse or not know. Note which rule failed first and rewrite only that line.
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account