Install the SDK, protect your API key, and talk to Claude from Python for the first time
Day 17 of 80
python-dotenv.env fileOpen your terminal in Cursor (Ctrl + `) and run:
$ 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.
sk-ant-.
.env file.env (the dot is part of the name). Add this single line:
ANTHROPIC_API_KEY=sk-ant-your-key-here
sk-ant-your-key-here with your actual key. No quotes needed.
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.
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.
Open your terminal and launch Jupyter:
$ 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.
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)
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.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}")
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).
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)
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}")
{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.
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}")
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.
pip install anthropic python-dotenv completed without errors.env file — not in your Python codeYou 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.