← Building with AI

Your first AI request

A request names a model, gives it messages, and sets a size limit.

Start this lesson

An AI model like Claude lives on a company's computers. Your program talks to it through an API: you send a request, and it sends back a reply. A basic request has three parts: which model to use, a list of messages (what you are saying), and max_tokens (the longest reply you are willing to accept). Everything else in this course is ordinary Python that you write around that one call.

# To run on your own computer with an API key (it cannot run in this browser).
# First: pip install anthropic, and set the ANTHROPIC_API_KEY environment variable.
import anthropic

client = anthropic.Anthropic()  # reads the ANTHROPIC_API_KEY environment variable

reply = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=500,
    messages=[{"role": "user", "content": "Explain photosynthesis to a 10-year-old."}],
)
print(reply.content[0].text)

This is the real thing. The client sends your messages over the internet to the model, waits, and gives you back a reply object. reply.content[0].text is the words the model wrote.

The exercises here run in your browser with no internet and no API key, so each one starts with a small pretend model called ask_model. It follows simple rules instead of really thinking, but it takes and returns the same kinds of things: you give it a list of messages, and it gives back a dict with the reply text in reply['text']. Please don't change the pretend part. Your code goes below it.

Each message is a dict with a role and some content, like {'role': 'user', 'content': 'Hello!'}. Even when you send just one question, it goes inside a list, because later you will send whole conversations.

Other companies (OpenAI, Google and others) have very similar SDKs: a client, a create-style call, a model name, and messages. Model names change often, so check the provider's documentation for the current names.