Day 5, 25 minutes · beginner
Why it matters
So far every request stood alone: one question, one answer. Real use needs two more things. Linde wants every reply in the same voice, without repeating "be short, be warm, sign with my name" in every request. And she wants to say "make it shorter" after the first draft, without pasting the review again.
The API has a tool for each. A system prompt holds the standing rules. A conversation is something you keep yourself, because the API forgets everything after each answer. Today you build a small chat script that does both, and you see exactly what the API remembers: nothing. At the end you have a system prompt written for Harbour Lane Tea Co and a reply drafted with it.
The system prompt: standing rules
The system field of a request holds instructions that apply to the whole conversation. It is not part of the messages list; it sits next to it:
{
"model": "claude-sonnet-5-5",
"max_tokens": 1000,
"system": "You write replies to customer reviews for Harbour Lane Tea Co ...",
"messages": [ ... ]
}
The prompting guide in the docs advises giving Claude a role: even one sentence such as "You are a helpful coding assistant specializing in Python" changes the tone and focus. It also says Claude does well with clear, explicit instructions. For Linde that means writing the rules down plainly:
- who Claude writes as, and for which shop;
- tone and length: warm, at most three sentences;
- what to do with a complaint: apologise once, say what happens next;
- what never to do: no discounts or refunds, because Linde decides those herself;
- how to end: her name and the shop.
A line like the last two is worth more than "be professional". Claude cannot guess Linde's rules about refunds.
Roles in the messages list
Each message in messages has a role:
user: what you, or your app's user, say;assistant: what Claude said earlier.
Every response comes back with the role assistant. The conversation goes user, assistant, user, and the last message you send is a user message.
There is also a way to add a system instruction halfway through a conversation, as a message with the role system. It works only on some current models and has placement rules; you do not need it in this course.
The API forgets everything: you keep the history
The docs put it in one sentence: the Messages API is stateless, so you send the full conversation history with each request. Stateless means the API keeps nothing between requests. Any memory you see in a chat is something the app around the model takes care of. In your own script, that job is yours.
So a conversation in your own code works like this:
- Add the user's message to a list.
- Send the whole list.
- Add Claude's answer to the list, unchanged, as an
assistantmessage. The docs say to keep it exactly as it came back, because it can hold more than text. - Repeat.
Two things follow from this:
- Longer conversations cost more. Every request sends all earlier messages again, so the input tokens go up each turn. Today's script prints them, so you can watch.
- You decide what Claude remembers. Nothing is in the conversation unless your code puts it in the list. Start the script again and the history is empty.
The steps
- Create the chat script with a system prompt and a history list.
- Paste a made-up review and check the reply against the rules.
- Ask a follow-up and watch the input tokens grow.
- Restart and ask the same follow-up, to see that the API remembers nothing.
- Write your own system prompt and test it on a happy review and a complaint.
Follow the steps below.
Common mistakes
- Putting the rules in the first user message instead of in
system. It works for one turn, but standing rules belong in the system prompt. - Adding only Claude's text to the history instead of the full
content. The docs say to send it back unchanged, and once you use tools that is a must. - Never trimming the history. For a review reply you only need a few turns, so start fresh for each review.
Today's actions
Ticks are not saved without an accountDo it in Claude API
Claude API
- In your harbour-lane-replies folder, with the virtual environment on, create
chat.pywith this code. The system prompt sets the rules; the history list holds the conversation.from dotenv import load_dotenv import anthropic load_dotenv() client = anthropic.Anthropic() SYSTEM = ( "You write replies to customer reviews for Harbour Lane Tea Co, " "a small online tea shop run by Linde. Be warm and short: at most " "three sentences. Thank the customer for what they actually said. " "If they mention a problem, apologise once and say what happens next. " "Never promise discounts or refunds. " "End with: Linde, Harbour Lane Tea Co" ) history = [] while True: text = input("You (empty line to stop): ") if not text: break history.append({"role": "user", "content": text}) message = client.messages.create( model="claude-sonnet-5-5", max_tokens=1000, system=SYSTEM, messages=history, ) history.append({"role": "assistant", "content": message.content}) for block in message.content: if block.type == "text": print("Claude:", block.text) print(f"[history: {len(history)} messages, input tokens: {message.usage.input_tokens}]") - Run
python chat.pyand paste a made-up review, for example:Five stars for the rooibos, my kids love it too.Read the reply and check it against the rules in SYSTEM. - Type a follow-up such as
Make it shorter and leave out the kids.Claude knows what it refers to, because the script sends the whole history again. Watch the input tokens go up. - Press Enter on an empty line to stop. Start the script again and type only
Make it shorter.Claude now has no idea what you mean: the API remembers nothing between requests. - For Node instead of Python: save this as
chat.mjsand runnode --env-file=.env chat.mjs.import Anthropic from "@anthropic-ai/sdk"; import readline from "node:readline/promises"; const client = new Anthropic(); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const SYSTEM = "You write replies to customer reviews for Harbour Lane Tea Co, " + "a small online tea shop run by Linde. Be warm and short: at most " + "three sentences. Thank the customer for what they actually said. " + "If they mention a problem, apologise once and say what happens next. " + "Never promise discounts or refunds. " + "End with: Linde, Harbour Lane Tea Co"; const history = []; while (true) { const text = await rl.question("You (empty line to stop): "); if (!text) break; history.push({ role: "user", content: text }); const message = await client.messages.create({ model: "claude-sonnet-5-5", max_tokens: 1000, system: SYSTEM, messages: history }); history.push({ role: "assistant", content: message.content }); for (const block of message.content) { if (block.type === "text") console.log("Claude:", block.text); } console.log(`[history: ${history.length} messages, input tokens: ${message.usage.input_tokens}]`); } rl.close(); - Change SYSTEM to your own rules for Linde's shop: tone, length, one thing Claude must never do and how to sign off. Run it on two made-up reviews, one happy and one with a complaint.
- Copy the best reply into a new file
replies.txt, with the review above it.
Note: Do not end the messages list with an assistant message to make Claude start its answer a certain way (a prefill): current models return a 400 error. Put format rules in the system prompt instead.
Paid planPay as you go: buy prepaid credits in the Claude Console (minimum 5 US dollars). There is no free tier. Max, Ultra and Team plans include monthly API credits.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up