top of page

Tutorial 1: Call Your First LLM API

  • Contributor
  • 4 days ago
  • 2 min read

Before building anything sophisticated, you need a working API call. This tutorial walks through it cleanly, in 15 minutes.

What You'll Build

A Python script that makes an authenticated API call to an LLM and prints the response.

Step 1: Get an API Key (5 min)

  • Anthropic: console.anthropic.com → API Keys → Create

  • OpenAI: platform.openai.com → API Keys → Create

  • Other: vendor's developer portal

Keep the key secret. Don't commit to git.

Step 2: Set Environment Variable (2 min)

export ANTHROPIC_API_KEY="sk-ant-..."
# Or for OpenAI:
export OPENAI_API_KEY="sk-..."

Or put in a .env file (gitignored):

ANTHROPIC_API_KEY=sk-ant-...

Step 3: Install the SDK (2 min)

pip install anthropic
# Or
pip install openai

Step 4: First Call (5 min)

import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["ANTHROPIC_API_KEY"]
)

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello! What's 2+2?"}
    ]
)

print(response.content[0].text)

Run:

python first_call.py

Expected: a response.

Step 5: Understand the Response (2 min)

The full response includes:

print(response.id)              # Unique request ID
print(response.model)           # Model used
print(response.role)            # "assistant"
print(response.content[0].text) # The actual response
print(response.usage.input_tokens)
print(response.usage.output_tokens)
print(response.stop_reason)     # "end_turn" or "max_tokens"

Save this knowledge; you'll use each field eventually.

Step 6: Handle Errors (5 min)

import anthropic

try:
    response = client.messages.create(...)
except anthropic.APIError as e:
    print(f"API error: {e}")
except anthropic.RateLimitError as e:
    print(f"Rate limited: {e}")
    # Wait and retry
except anthropic.AuthenticationError as e:
    print(f"Bad API key: {e}")

API calls fail. Plan for it from the start.

Step 7: Try OpenAI Equivalent (5 min)

For comparison:

import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="gpt-4",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello! What's 2+2?"}
    ]
)

print(response.choices[0].message.content)

Similar shape, slightly different details. Most LLM APIs follow this pattern.

Step 8: Add a System Prompt (3 min)

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system="You are a helpful math tutor. Always show your work.",
    messages=[
        {"role": "user", "content": "What's 2+2?"}
    ]
)

The system prompt steers behavior. Tutorial 2 of Practical Prompt Engineering goes deeper.

Step 9: Use Environment-Specific Config (5 min)

import os

# Read from environment with sensible defaults
MODEL = os.environ.get("LLM_MODEL", "claude-sonnet-4-6")
MAX_TOKENS = int(os.environ.get("LLM_MAX_TOKENS", "1024"))

response = client.messages.create(
    model=MODEL,
    max_tokens=MAX_TOKENS,
    ...
)

Different envs (dev/staging/prod) can use different settings.

Step 10: Verify You Can Iterate (3 min)

Modify the prompt; rerun. See different output.

prompts = [
    "What's 2+2?",
    "Explain why 2+2=4 to a 5-year-old.",
    "Write a haiku about 2+2.",
]

for p in prompts:
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": p}]
    )
    print(f"Q: {p}")
    print(f"A: {response.content[0].text}\n")

Iteration cycle is now: 5 seconds per call.

What You Just Did

You have a working LLM integration. From here, you can build anything.

Common Failure Modes

Hardcoded API key. Committed to git; key gets stolen. Use environment.

No error handling. API failure crashes your code.

Forgetting tokens. Default max_tokens is low for some models; outputs truncated.

No system prompt. Default behavior; can't steer.

Vendor lock-in. Code only works with one vendor. Abstract behind an interface for portability.

bottom of page