Install an SDK, set your key safely, and make a model call you understand line by line — the request shape, the message list, and how to read the response.
Why: every AI feature is an HTTP call to a model behind an API — you send messages, you get back generated text. When: set this up once; every later lesson assumes it works. Where: keep the key in the environment, never in the code, so committing to git never leaks it.
You need three things:
[ ] Python 3.10+ -> python --version
[ ] The provider SDK -> pip install anthropic
[ ] An API key -> console.anthropic.com -> API Keys
The mechanics are the same on every provider: a client, a
"messages" list, and a response object. Only the field names differ.Why: the SDK turns a raw HTTPS request into one function call and handles auth, JSON, and errors. Where: export the key for this shell session — the client reads it automatically, so it never appears in your source.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install anthropic
export ANTHROPIC_API_KEY="sk-ant-..." # Windows: set ANTHROPIC_API_KEY=...Why: three parameters do the real work — the model, a token ceiling for the reply, and the messages. When: max_tokens caps the response, not your input; set it high enough for the answer you expect. Where: content comes back as a list of blocks, so pull the text out rather than assuming it is a plain string.
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the env
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
messages=[{"role": "user", "content": "Explain what an API is in one sentence."}],
)
print(response.content[0].text)
print(response.usage) # input_tokens / output_tokens — you pay per tokenWhy: the API is stateless — it remembers nothing between calls, so a conversation is just the full message list you resend each time. When: append the model’s reply as an "assistant" message, then add the next user turn. Where: this growing list is the "context" you will learn to manage in a later lesson.
messages = [
{"role": "user", "content": "My name is Sam."},
]
r1 = client.messages.create(model="claude-opus-4-8", max_tokens=256, messages=messages)
# Feed the reply back in so the next turn "remembers" it.
messages.append({"role": "assistant", "content": r1.content[0].text})
messages.append({"role": "user", "content": "What's my name?"})
r2 = client.messages.create(model="claude-opus-4-8", max_tokens=256, messages=messages)
print(r2.content[0].text) # "Your name is Sam."