Skip to content
Streak:0Loading your progress…Sign in

Module 9 · Lesson 1 of 3

Calling a model

Messages, system prompts, tokens and context windows. ~25 min

Lesson notes

The Anthropic Python SDK (pip/uv: anthropic)
import anthropic

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

response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=2048,
    system="You are a concise assistant for a logistics company.",
    messages=[{"role": "user", "content": "Summarise this shipment note: ..."}],
)
for block in response.content:
    if block.type == "text":
        print(block.text)
print(response.usage.input_tokens, response.usage.output_tokens)

Mental model: an LLM predicts the next token (~¾ of an English word). You pay per input and output token. The context window is the max tokens per request (prompt + output). Conversations are stateless: you resend the history every call. The system prompt sets behaviour; messages alternate user/assistant.

Practice

  1. 1. Fit history into a budget

    medium25 XP

    Write fit_history(messages, budget_tokens) that keeps the most recent messages whose estimated tokens fit in the budget. Estimate tokens as ceil(len(content) / 4). The result must start with a user message (drop leading assistant messages after trimming) and keep chronological order. Always keep the final message even if it alone exceeds the budget.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor