Sunday, 06 September 2026
Advertisement Advertise Your advert could be here Reach thousands of learners and ICT professionals across Rwanda. Contact us
Advertisement Opportunity Jobs, scholarships & hackathons Fresh openings from Rwandan job boards are pulled in every hour. See openings

From chat window to API call

Building AI applications with Python · lesson 1 of 12

In this lesson: Describe the request/response shape of a model API and why it is stateless.

A chat window hides four things that become your responsibility the moment you call the model from code. Understanding them up front prevents most beginner architecture mistakes.

1. The API is stateless

There is no conversation on the server. Every request carries the entire history, and the model sees only what you send this second. The chat interface you have used maintains that history for you; in your program, you own it — storing it, trimming it, and paying for it on every turn.

2. You choose the model, per request

Model choice is a parameter, not a setting. A classification job that runs a million times a day and a hard reasoning task can and should use different models. Current Anthropic models look like this:

ModelModel IDUse it for
Claude Opus 5claude-opus-5Hard reasoning, agents, code. The default.
Claude Sonnet 5claude-sonnet-5Balanced work at lower cost
Claude Haiku 4.5claude-haiku-4-5High-volume, latency-sensitive, simple tasks

Model IDs are exact strings. Do not append a date suffix, and do not guess an ID from a marketing name — a wrong ID is a 404, and a plausible-looking wrong ID is worse because it looks correct in review.

3. Output is a list of blocks, not a string

This is the shape mistake that catches everyone once. A response contains a list of typed content blocks — text, thinking, tool calls — and reaching straight for the first one breaks as soon as the model thinks or calls a tool. Always filter by type.

4. Everything can fail

Networks time out, you get rate limited, the model refuses, the answer hits the length ceiling. In a browser you press retry; in a program you write the retry, the backoff and the fallback. Budget for this — it is a real part of the work, not an afterthought.

The mental model: the API is a stateless function from (instructions, history, tools, settings) to (blocks, stop reason, usage). Everything in this track is about controlling the input side and being disciplined about the output side.

What you need before the next lesson

python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install anthropic

You will also need an API key from the provider's console, kept in an environment variable — never in your source code, and never in a file you commit.

export ANTHROPIC_API_KEY="sk-ant-..."     # Windows PowerShell:
                                          # $env:ANTHROPIC_API_KEY="sk-ant-..."

Try it yourself

Before writing any code, sketch on paper the four inputs and three outputs above for an application you would like to build. If you cannot say what the instructions are and what the output must look like, you are not ready to write the call yet — and that is the most common reason early AI projects wander.

Create a free account to save progress

All lessons in this track

  1. 1
  2. 2
  3. 3
  4. 4
    Getting JSON your program can rely on ~22 min account needed
  5. 5
    Giving the model tools it can call ~25 min account needed
  6. 6
  7. 7
  8. 8
  9. 9
    Making retrieval actually work ~20 min account needed
  10. 10
  11. 11
    Cost, latency and prompt caching ~22 min account needed
  12. 12
Advertisement Yanjye Learn a new digital skill this week ICT, programming and professional courses with graded weekly assignments. Start free