Basic

Understanding prompts

The prompt is the entire conversation you hand to the model. Learn how a role like 'assistant' and clear instructions turn a raw call into a well-behaved chatbot.

What you will see

Model remembers: Ada

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

The prompt is the whole conversation you hand to the model. I define roles for each message, write a specific system prompt, and send history every call so the model can remember who you are.

1

A chat answer depends on who is speaking, so the API takes a list of messages each with a role. I print a system message and a user message to show how the roles divide the conversation.

messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hi, can you help me?"} ] for m in messages: print(f"{m['role']}: {m['content']}")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
system: You are a helpful assistant. user: Hi, can you help me?
The system message is like whispering instructions to the model before it speaks. It shapes how every reply behaves.
2

A specific system prompt changes how the model answers. I pass one that gives a job, a length, and a tone, then print the reply a vague one could never produce.

def make_reply(prompt): if "coding tutor" in prompt: return "I am a friendly coding tutor. Answer in 2-3 sentences." else: return "You are an assistant." print(make_reply("You are a friendly coding tutor."))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
I am a friendly coding tutor. Answer in 2-3 sentences.
A vague prompt gives vague answers. State the length, tone, and what to avoid so the model has real constraints to follow.
3

The model has no memory between calls, so you must resend the whole history each time. I keep the early messages and the latest question and print that the model can answer from the context.

history = [ {"role": "user", "content": "My name is Ada."}, {"role": "assistant", "content": "Nice to meet you, Ada."}, {"role": "user", "content": "What is my name?"} ] last_user = history[-1]["content"] if "name" in last_user: print("Model remembers: Ada")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Model remembers: Ada
Memory lives in your message list, not in the model. Forget to resend the history and the model forgets everything too.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Messages with roles are sent together; the model reads the whole list and produces a reply consistent with the history.
1 App collects the system message
↓
2 Add previous user and assistant messages
↓
3 Add the latest user message
↓
4 All messages are sent together
↓
5 The model reasons over the full list
↓
6 It replies consistently with context
Why this works
Why context matters so much

The model has no memory between calls. It only sees the messages you send. If you want it to remember the earlier part of a chat, you must send that history back every time.

This is the core trick of chatbots:

const messages = [
  { role: 'user', content: 'My name is Ada.' },
  { role: 'assistant', content: 'Nice to meet you, Ada.' },
  { role: 'user', content: 'What is my name?' }
];

Send all of this together, and the model can answer 'Ada' correctly. The memory lives in your message list, not in the model.

Examples

Roles in a conversation

A chat answer depends on who is speaking. The API takes a list of messages, each with a role and content:

const messages = [
  { role: 'system', content: 'You are a helpful assistant.' },
  { role: 'user', content: 'Hi, can you help me?' }
];

  • system: instructions that shape how the model behaves.
  • user: what the person said.
  • assistant: what the model replied.

The system message is like whispering instructions to the model before it speaks.

Writing a good system prompt

The system prompt tells the model its job. A vague one gives vague answers:

// Too vague
'You are an assistant.'

// Better, clear and specific
'You are a friendly coding tutor. Answer in 2-3 sentences. Never give full code unless asked.'

A specific prompt changes how the model answers. It can say how long to be, what tone to use, and what to avoid.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Keeping your secret safe
Previous Lesson
Keeping your secret safe
Next Lesson
A simple chat interface
A simple chat interface →