Week 4 of 16

Setup & First API Calls

Install the SDK, protect your API key, and talk to Claude from Python for the first time

Day 17 60 minutes Setup + Experiment

Day 17 of 80

What You'll Accomplish Today

Part 1: Install the SDK (5 min)

Open your terminal in Cursor (Ctrl + `) and run:

Terminal
$ pip install anthropic python-dotenv

anthropic is the official Python SDK — it handles all the HTTP complexity for you. python-dotenv is a utility that reads a .env file and loads its values as environment variables, which is how you'll keep your API key out of your code.

Part 2: Get Your API Key (5 min)

  1. Go to the Anthropic Console
    Visit console.anthropic.com. Create an account if you don't have one — free tier credits are provided automatically.
  2. Generate an API key
    In the console, go to API Keys and click Create Key. Give it a name like "Python Training". Copy the key — it starts with sk-ant-.
  3. Create your .env file
    In your project folder, create a new file called exactly .env (the dot is part of the name). Add this single line:
    .env
    ANTHROPIC_API_KEY=sk-ant-your-key-here
    Replace sk-ant-your-key-here with your actual key. No quotes needed.
Never Expose Your API Key

Your API key is a secret. Never paste it directly into your Python code. Never commit the .env file to git. Never share it in a screenshot. Anyone who has this key can make API calls that bill to your account. If you think you've accidentally exposed it, go to the console immediately and revoke it, then generate a new one. This is a non-negotiable rule in every professional project.

Add .env to .gitignore

If you ever initialize a git repo in this folder, add a .gitignore file that contains a single line: .env. This prevents git from ever tracking the file. Make this a habit from day one.

Part 3: Five Cells in Jupyter

Open your terminal and launch Jupyter:

Terminal
$ jupyter notebook

Create a new notebook called week-4-practice.ipynb. Work through each cell below — run it, read the output, then read the explanation before moving to the next.

Cell 1 — Your First API Call

week-4-practice.ipynb — Cell 1 Python
import anthropic
from dotenv import load_dotenv

load_dotenv()
client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=100,
    messages=[{
        "role": "user",
        "content": "In one sentence, what is an API?"
    }]
)

print(message.content[0].text)
What each line does:
  • load_dotenv() reads your .env file and loads ANTHROPIC_API_KEY into the environment. The SDK finds it automatically from there — you never pass the key explicitly.
  • anthropic.Anthropic() creates an API client instance. Think of it as opening a connection to Anthropic's service.
  • model="claude-sonnet-4-6" — specifies which Claude model to use.
  • max_tokens=100 — caps the response length. One token ≈ 0.75 words.
  • messages=[{"role": "user", "content": "..."}] — the conversation history. Always a list, even for one message.
  • message.content[0].text — drills into the response object to get the plain text.

Cell 2 — Inspect the Full Response

week-4-practice.ipynb — Cell 2 Python
print(f"Model: {message.model}")
print(f"Stop reason: {message.stop_reason}")
print(f"Input tokens: {message.usage.input_tokens}")
print(f"Output tokens: {message.usage.output_tokens}")
print(f"Text: {message.content[0].text}")
Reading the metadata: The message object from Cell 1 is still in memory — you're just inspecting more of it here. stop_reason tells you why Claude stopped: end_turn means it finished naturally; max_tokens means it hit your limit and got cut off. Token counts let you estimate cost — Claude Sonnet is roughly $3 per million input tokens and $15 per million output tokens (check current pricing at console.anthropic.com).

Cell 3 — System Prompt: Give Claude a Persona

week-4-practice.ipynb — Cell 3 Python
message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=150,
    system="You are a cinematic director who speaks in short, vivid sentences.",
    messages=[{
        "role": "user",
        "content": "Describe a sunrise over a golf course."
    }]
)
print(message.content[0].text)
The system prompt is separate from the user message. It sets the rules, persona, and context for the entire conversation. Claude reads it first and applies it to everything that follows. This is one of the most powerful features of the API — you can give Claude a specific identity, restrict what it talks about, or tell it exactly how to format its responses. Experiment: change the system prompt and see how dramatically the output changes.

Cell 4 — Variables in Prompts

week-4-practice.ipynb — Cell 4 Python
platform = "Kling"
shot = "slow motion golf swing with morning light"

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=200,
    messages=[{
        "role": "user",
        "content": f"Write a {platform} AI video prompt for: {shot}. "
                   f"Include camera, lighting, mood. Under 50 words. Only the prompt."
    }]
)

result = message.content[0].text
print(f"[{platform}] {result}")
This is the core pattern you'll use all week. Variables go into the prompt via f-strings — {platform} and {shot} get replaced with their values at runtime. Change platform to "Runway" or "Veo" and run the cell again. Change shot to something from your own work. Notice how Claude adapts its response to the platform. This is the foundation of everything you'll build this week.

Cell 5 — Error Handling

week-4-practice.ipynb — Cell 5 Python
try:
    message = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=100,
        messages=[{"role": "user", "content": "Hello"}]
    )
    print(f"Success: {message.content[0].text}")
except anthropic.AuthenticationError:
    print("Bad API key — check your .env file.")
except anthropic.RateLimitError:
    print("Too many requests — wait a minute and try again.")
except anthropic.APIError as e:
    print(f"API error: {e}")
Always wrap API calls in try/except. Networks fail, keys expire, rate limits get hit. Without error handling, your script crashes silently and you have no idea why. With specific except clauses — AuthenticationError, RateLimitError — you can tell the user exactly what went wrong instead of showing them a confusing traceback. The bare APIError at the end catches anything else the SDK throws. This is the pattern you'll use in every script from here on.

End of Day Checklist

Tomorrow

You take everything from today's cells and turn it into a real command-line tool — the DVP Prompt Generator. You'll type a shot description, choose a platform, and get back a production-ready prompt that gets saved to a JSON library. It's a real tool you could use today.