Calling an LLM from Python
Before an agent can think, it needs a brain to think with: a language model. In this lesson you make your first call to a model from Python, read the reply, and wrap it in a clean function you will reuse for the rest of the course.
We use the Anthropic SDK directly, with no agent framework on top. That is deliberate. Once you can call a model and read its answer, you have the one ingredient an agent's "think" step is made of, and the pattern transfers to any provider or framework you use later.
What You'll Learn
- The mental model of a chat API: messages in, one message out
- How to install the SDK and call a model
- Where the API key goes and how to keep it out of your code
- How to pull the text out of the response
- How to wrap the call in a small, reusable function
A note on running this code
The code in this lesson talks to a live service over the internet, so it cannot run inside the in-page playground (the playground has no network access and no API key). Read it carefully here, then copy it into a file on your own machine once you have a key. Later lessons use the in-page playground for the parts that can run offline, like the agent loop logic.
The mental model: messages in, one message out
Every modern chat model works the same way. You send a list of messages, each with a role, and you get back one message of text.
- A system message sets behavior ("You are a concise research assistant").
- A user message is your actual request.
- The model replies as an assistant message.
Once you internalize "messages in, one message out," every provider looks familiar, because they all copy this shape. An agent is just this same call, made repeatedly inside a loop.
Get a key and protect it
To call a real model you need an API key from a provider. Sign up on the provider's site, create an API key, and add a small amount of credit (each of these short calls costs a fraction of a cent).
Never paste the key directly into your code. If you do, it can leak the moment you share the file or push it to GitHub. Instead, store it in an environment variable and read it at runtime. During development, the simplest cross-platform way is a .env file plus the python-dotenv package.
Create a file named .env next to your script:
ANTHROPIC_API_KEY=your-key-goes-here
Then add .env to your .gitignore so it is never committed. Now your code reads the key from the environment and the secret lives outside your codebase.
Install what you need
In a terminal, inside your project folder:
pip install anthropic python-dotenv
Your first call
import os
from dotenv import load_dotenv
from anthropic import Anthropic
load_dotenv() # reads .env into the environment
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = client.messages.create(
model="claude-opus-4-8", # a current Claude model
max_tokens=300,
system="You are a concise research assistant.",
messages=[
{"role": "user", "content": "In one sentence, what is an AI agent?"}
],
)
# The reply text lives inside the response object:
print(response.content[0].text)
Three things to notice:
load_dotenv()plusos.environ[...]keeps the key out of the code.modelnames a specific model.claude-opus-4-8is a capable current model; if you want lower cost for simple tasks,claude-sonnet-4-6works the same way. Name a specific model rather than relying on a moving alias.max_tokenscaps how long the reply can be, which protects you from runaway cost.
Reading the response
The reply does not come back as a plain string. It comes back as a structured object so it can carry more than text later (like the tool requests you will see in Module 2). For a normal text reply, the text lives at response.content[0].text.
It is worth printing the whole object once while you are learning, so the shape stops being a mystery:
print(response) # see the full structure
print(response.content) # the list of content blocks
print(response.content[0].text) # the text of the first block
Wrap it in a reusable function
You will call the model many times inside an agent, so wrap the call once. A small function keeps every later lesson short.
import os
from dotenv import load_dotenv
from anthropic import Anthropic
load_dotenv()
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def ask_model(messages, system="You are a helpful assistant."):
"""Send a list of messages, return the model's text reply."""
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=500,
system=system,
messages=messages,
)
return response.content[0].text
# Try it:
reply = ask_model([{"role": "user", "content": "Name three uses for an AI agent."}])
print(reply)
This ask_model function is the "think" step from Lesson 1. In Module 2 you will give the model a tool, and the loop around this call becomes a real agent.
Practice the message shape (runs in your browser)
You cannot call the live model here, but you can practice building the message list and reading a fake response that has the same shape. Run this to get comfortable with the structure before you wire up a real key.
The real SDK gives you objects, so you write response.content[0].text. Here we use a dictionary so it runs offline, so you write response["content"][0]["text"]. The structure is identical; only the access style differs.
Key Takeaways
- A chat call is messages in, one message out. System sets behavior, user is your request, assistant is the reply.
- Keep your API key in a
.envfile and read it withos.environ. Never hardcode it. pip install anthropic python-dotenv, then callclient.messages.create(...).- Text lives at
response.content[0].text; print the whole object once to learn its shape. - Wrap the call in a small
ask_modelfunction. It is the "think" step your agent loop will call repeatedly.

