Site

Hello — Your First LLM API Call

Tutorial 1.0  •  AI / Learn

1.0 What This Teaches

This tutorial introduces Large Language Models and makes the smallest possible useful API call: send a message, get a response. It covers:

1.1 What an LLM Is

A Large Language Model (LLM) is a program trained on vast amounts of text. Given a sequence of text as input, it predicts what text should come next. That simple mechanism, scaled up, produces a model that can answer questions, write code, explain concepts, and follow instructions. You interact with an LLM by sending it a message and reading its reply. The model has no memory between separate API calls - each call is independent unless you explicitly pass prior conversation history. This tutorial shows a single-call interaction.

1.2 The Anthropic Python SDK

Anthropic makes the Claude family of models available through a REST API. The Python SDK wraps that API so you can make requests with ordinary Python function calls instead of writing raw HTTP. Install the SDK into your active Python environment:
pip install anthropic
You also need an API key. See Tutorial 2 (Tools) for how to create one and store it safely. For now, assume the environment variable ANTHROPIC_API_KEY is already set.

1.3 Your First API Call

# hello.py - sends one message to Claude and prints the reply.
import anthropic

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

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=[
        {"role": "user", "content": "Write a Python function that adds two numbers."}
    ]
)

print(response.content[0].text)
anthropic.Anthropic() creates a client. It reads ANTHROPIC_API_KEY automatically from the environment - you do not pass the key in code. messages.create sends the request. The three required parameters are:

1.4 The Response Object

The return value of messages.create is a Message object. The generated text lives in response.content, which is a list of content blocks. For a plain text reply there is always exactly one block, so response.content[0].text gives you the string. Other useful fields on the response:
FieldTypeWhat it contains
response.modelstrThe model that actually handled the request
response.stop_reasonstr"end_turn" when the model finished naturally; "max_tokens" when it hit the limit
response.usage.input_tokensintTokens consumed by your messages and system prompt
response.usage.output_tokensintTokens generated in the reply

1.5 Running the Script

python hello.py
Expected output (the exact code will vary each run):
def add(a, b):
    return a + b
The response changes on every run because the model samples from a probability distribution. For most tasks, especially code, the variation is small - the structure of a two-number addition function is constrained enough that it usually looks the same.

1.6 Example: Printing Usage

# hello_usage.py - first API call plus token accounting.
import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=[
        {"role": "user", "content": "Write a Python function that adds two numbers."}
    ]
)

print(response.content[0].text)
print()
print(f"Input tokens:  {response.usage.input_tokens}")
print(f"Output tokens: {response.usage.output_tokens}")
print(f"Stop reason:   {response.stop_reason}")
def add(a, b):
    return a + b

Input tokens:  17
Output tokens: 14
Stop reason:   end_turn

1.7 Exercise

Exercise Modify hello.py to ask the model to write a function that multiplies two numbers. Print both the generated code and the token counts. Then change max_tokens to 10 and run again - observe how the output is cut off and that stop_reason changes to "max_tokens".

1.8 Common Mistakes

API key not set

If ANTHROPIC_API_KEY is not in the environment, the client raises anthropic.AuthenticationError. Set the variable before running: export ANTHROPIC_API_KEY=sk-ant-... on Linux/macOS or $env:ANTHROPIC_API_KEY = "sk-ant-..." in PowerShell.

Indexing content before checking it

print(response.content[0].text)   # safe for normal text responses
print(response.content.text)       # AttributeError: list has no .text
response.content is a list. Always index into it with [0] before accessing .text.

max_tokens too small

Setting max_tokens=10 for a request that needs 200 tokens truncates the output mid-sentence. stop_reason will be "max_tokens" instead of "end_turn". Always set max_tokens to more than you expect the reply to need.

1.9 Key Terms

TermMeaning
LLMLarge Language Model - a model trained to predict and generate text
APIApplication Programming Interface - a defined way to send requests and receive responses
SDKSoftware Development Kit - a library that wraps an API for a specific language
messages.createThe Anthropic SDK method that sends a request and returns a response
max_tokensHard upper limit on how many tokens the model may generate
stop_reason"end_turn" means finished; "max_tokens" means the limit was hit
content blockOne element of response.content; for text responses, content[0].text is the reply