In Chapter 6 your chatbot followed rules you wrote. Now you'll plug in a real AI brain — Claude — so your program can answer almost anything. This is the moment your code becomes genuinely intelligent.
How code talks to an AI
Your program sends a prompt over the internet to the AI company's computers (an API — Application Programming Interface), and gets the reply back. You need three things:
- The
anthropicPython package (the toolkit for talking to Claude). - An API key — a secret password that says "this is my account."
- A few lines of code.
Stay safe
An API key is a secret, like a password — and using it costs a small amount of real money per request.
- Set this up with a parent or guardian. They create the key and handle billing.
- Never share your key, post it online, or put it in code you send to others. Anyone with it can spend your account's money.
- We'll use a cheap, fast model and keep replies short so costs stay tiny.
Step 1 — Install the toolkit
In a new Colab cell (from Chapter 6), run:
!pip install anthropic
The ! tells Colab to install a package. You only do this once per notebook.
Step 2 — Connect with your key
This code asks for your key without showing it on screen, so it stays secret:
from anthropic import Anthropic
from getpass import getpass
api_key = getpass("Paste your Anthropic API key (it stays hidden): ")
client = Anthropic(api_key=api_key)
print("Connected! ✅")
client is now your line to Claude.
Step 3 — Your first AI reply in code
message = client.messages.create(
model="claude-haiku-4-5",
max_tokens=300,
system="You are a friendly study buddy for a curious kid. Keep answers short and clear.",
messages=[
{"role": "user", "content": "Explain what gravity is in 2 sentences."}
],
)
print(message.content[0].text)
Look closely — you already know these ideas from Chapter 3!
model— which AI to use.claude-haiku-4-5is fast and low-cost: perfect for projects.system— the persona and rules (your "standing orders").messages— the conversation. Each message has a role (userorassistant) and content.max_tokens— a limit on how long the reply can be (keeps it short + cheap).
message.content[0].text pulls the words out of the reply.
Tip
Everything you learned about prompting still applies — it's just in code now.
Change the system text to change your buddy's personality, exactly like
Chapter 3.
Step 4 — Turn it into a reusable helper
Wrap it in a function so you can ask anything with one line:
def ask_buddy(question):
message = client.messages.create(
model="claude-haiku-4-5",
max_tokens=300,
system="You are a friendly study buddy for a curious kid. Keep answers short and clear.",
messages=[{"role": "user", "content": question}],
)
return message.content[0].text
print(ask_buddy("Why is the sky blue?"))
print(ask_buddy("Give me one cool fact about octopuses."))
Check yourself
- What is an API key, and why must you keep it secret?
- In the code, which part sets the AI's personality?
- What does
message.content[0].textgive you?
Project — Your command-line study buddy 🛠️
Let's make a program you can actually chat with. It keeps asking questions until you type "bye":
print("🤖 Study Buddy is ready! Ask me anything. Type 'bye' to stop.\n")
while True:
question = input("You: ")
if question.lower() == "bye":
print("Buddy: See you later! 👋")
break
answer = ask_buddy(question)
print("Buddy:", answer, "\n")
- Run it and have a real conversation.
- Make it yours: change the
systemprompt to create a different helper — a joke-teller, a Spanish tutor, a riddle master. - Add a touch: print
"Buddy is thinking..."before the answer so it feels alive.
Your turn
Give your buddy a rule using what you learned in Chapter 3. For example, set the system prompt to: "You are a riddle master. Never give the answer until the user has guessed twice." Then test whether your code-buddy obeys the rule.
Make it simpler · ages 9–11
Do this with a grown-up and focus on Steps 1–3. The big win is seeing your own code make the AI answer a question you typed. Then change the system prompt to "You are a silly pirate" and ask it something — instant fun. 🏴☠️
Level up · ages 13–16
Add error handling so a network hiccup doesn't crash your program:
try:
answer = ask_buddy(question)
except Exception as e:
answer = "Oops, something went wrong. Try again!"
print("(debug:", e, ")")
Then experiment: lower max_tokens to 50 and watch replies get cut off, or
raise it to 600 for longer answers. You're now tuning a real AI app.
What you learned
- Code talks to Claude through an API using a secret key.
client.messages.create(...)sends a system prompt + messages and returns a reply.- Your prompt-engineering skills work the same in code.
- You built a real, interactive AI helper. 🎉
You've earned the API Apprentice badge. 🏅
Your helper answers questions — but it forgets everything between them, and it can't do anything in the world. Next you'll fix both: memory and tools. That's Chapter 8: Giving AI Memory & Tools.