Phase 2 — Working with LLMs · Lesson 20 · 20 XP
First API calls: messages and system prompts
Every model provider's chat API takes the same basic shape: a list of messages, each with a role (system, user, or assistant) and content. The system message sets persistent instructions for the whole conversation; user and assistant messages are the back-and-forth.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=200,
system="You are a concise assistant.",
messages=[{"role": "user", "content": "Explain recursion in one sentence."}],
)
print(response.content[0].text)The model is stateless between calls — it has no memory of your last request. "Conversation memory" is really just your code resending the entire message history on every call. Forget to include earlier turns, and the model has genuinely never seen them.
Exercise
Make your first direct API call to a model. Then build a loop that appends each turn to a messages list and resends the whole history every request — and prove to yourself the model has no memory by omitting history on one call and watching it lose context.
Check yourself
1. Why does the client have to resend the full conversation history on every single request?
2. What's the difference between a system message and a user message?
How LLMs work: tokens, context windows, sampling
Answer the check-yourself questions to unlock this