Build Your First AI App with Claude
Call the Claude API with Python to build an AI study assistant chatbot.
Introduction
30 Second Summary
Every time you ask ChatGPT a question or use an AI writing tool, the same thing happens behind the scenes. Your message travels to an AI model through an API, and the model's response travels back.
In this project, you will build a terminal-based AI study assistant using Python and Claude. You'll send a topic to Claude's API, get back structured study notes, and then upgrade your script into an interactive chatbot that remembers your conversation.
What You'll Build
A Python script that connects to the Claude API to generate study notes on any topic, then an interactive chatbot with conversation memory that lets you ask follow-up questions.
By the end of this project, you'll have:
- A working Python script that calls the Claude API and prints AI-generated study notes in your terminal.
- An interactive chatbot with conversation memory that lets you have multi-turn discussions with Claude.
- Hands-on experience with API keys, prompt engineering, and the message format that powers every AI chat application.
- Secret Mission: Give Claude a custom personality and area of expertise using system prompts.
Are there any prerequisites?
None. This guide walks you through installing Python, setting up your project, and creating your Anthropic account from scratch. All you need is VS Code already installed on your computer.
Install Python and Set Up Your Project
Before you can build your AI study assistant, you need three things on your computer: Python (the programming language your code runs on), the Anthropic SDK (a library that lets Python talk to Claude), and an API key (your personal access pass to Claude's brain).
In this step, you'll install all three and verify everything works together.
In this step, get ready to:
- Install Python on your computer.
- Create a project folder and install the Anthropic SDK.
- Set up your Anthropic API key.
Install Python
Python is the programming language you'll use to write your AI study assistant. You'll install it using the Python Install Manager from the Microsoft Store, which is the recommended way to get Python on Windows.
- In VS Code, open a new terminal by clicking Terminal in the top menu bar, then selecting New Terminal.
- Check if Python is already installed by running this command:
python --version
Look at the output in your terminal and pick the tab below that matches what you see.
✔️ I see version 3.14 or higher
Python is ready to go. You can skip ahead to the next substep.
ⓧ I see an older version
You have Python installed, but you need version 3.14 or higher for this project. Install the Python Install Manager from the Microsoft Store to get the latest version.
- Press the Windows key to open your search bar.
- Type Microsoft Store and press Enter to open it.
- In the Microsoft Store search bar, type Python and press Enter.
- Find the app called Python Install Manager published by the Python Software Foundation.
- Click Install.
What about the PATH prompt?
After the installation completes and you first run python, you may see a prompt asking to add a directory to your PATH. Accept it so that the python and pip commands work from any terminal.
- Go back to VS Code. Close your current terminal by clicking the trash can icon in the terminal panel.
- Open a fresh terminal by clicking Terminal in the top menu bar, then New Terminal.
- Run python --version again. You should now see Python 3.14.5 or similar.
ⓧ Command not found
Python isn't installed yet. You'll install it using the Python Install Manager from the Microsoft Store.
- Press the Windows key to open your search bar.
- Type Microsoft Store and press Enter to open it.
- In the Microsoft Store search bar, type Python and press Enter.
- Find the app called Python Install Manager published by the Python Software Foundation.
- Click Install.
What about the PATH prompt?
After the installation completes and you first run python, you may see a prompt asking to add a directory to your PATH. Accept it so that the python and pip commands work from any terminal.
- Go back to VS Code. Close your current terminal by clicking the trash can icon in the terminal panel.
- Open a fresh terminal by clicking Terminal in the top menu bar, then New Terminal.
- Run python --version again. You should now see Python 3.14.5 or similar.
Still not seeing a version number?
Make sure you closed and reopened the terminal after installing. The terminal needs to reload to recognize new programs.
If it still doesn't work, try restarting VS Code entirely, then opening a new terminal.
help me troubleshoot why Python isn't being recognized after I installed the Python Install Manager from the Microsoft Store
Create your project folder and install the Anthropic SDK
Now you'll create a folder to hold your project files and install the Anthropic SDK. The SDK is a Python library that handles all the details of communicating with Claude's API.
- Move to your Desktop by running this command:
cd ~/Desktop
- Create your project folder and move into it by running these commands:
mkdir ai-study-assistant
cd ai-study-assistant
- Install the Anthropic Python SDK by running:
pip install anthropic
What is an SDK?
SDK stands for Software Development Kit. It's a pre-built library that simplifies working with a service's API. Instead of writing raw network requests yourself, the Anthropic SDK gives you simple Python functions like client.messages.create() that handle all the communication details for you.
- Verify the SDK installed correctly by running:
pip show anthropic
You should see package information including a version number starting with 0.107.
Seeing 'pip is not recognized'?
This usually means the PATH wasn't updated when Python was installed. Close your terminal, open a new one, and try again.
If it still doesn't work, try running py -m pip install anthropic instead.
help me fix pip not being recognized in my VS Code terminal on Windows after installing Python via the Microsoft Store
Get your Anthropic API key
An API key is like a password that identifies you to Claude's servers. You'll create one on Anthropic's website and save it as an environment variable so your Python code can use it automatically.
- Open your browser and go to console.anthropic.com.
- Create an account (or sign in if you already have one).
- Once signed in, navigate to API Keys in your account settings.
- Click Create Key.
- Give your key a name like study-assistant.
- Copy the key immediately. You won't be able to see it again after you leave this page.
What is an API key?
An API key is a unique string that identifies your account when your code makes requests to Claude. It's how Anthropic knows who is using their service and tracks your usage for billing.
Keep your API key secret. Never share it publicly or commit it to a repository.
Now you'll save the key as an environment variable in your terminal. This lets the Anthropic SDK find your key automatically without you hardcoding it into your Python files.
Pick the tab below that matches the shell shown in your VS Code terminal. PowerShell is the default.
PowerShell
- Set your API key by running this command (replace your-api-key-here with the key you just copied):
$env:ANTHROPIC_API_KEY="your-api-key-here"
Command Prompt
- Set your API key by running this command (replace your-api-key-here with the key you just copied):
set ANTHROPIC_API_KEY=your-api-key-here
This is session-only
The environment variable you just set only lasts for this terminal session. If you close the terminal or restart VS Code, you'll need to set it again before running your scripts.
Time to verify everything works together. Run this command to confirm Python can find the Anthropic SDK:
python -c "import anthropic; print('SDK ready!')"
You should see SDK ready! printed in your terminal.
Seeing an import error?
If you see ModuleNotFoundError: No module named 'anthropic', the SDK didn't install to the Python version your terminal is using. Try running pip install anthropic again in the same terminal session.
help me fix a ModuleNotFoundError for anthropic after I ran pip install anthropic on Windows
Your environment is ready. Next up, you'll write your first Python script and make a real API call to Claude.
Make Your First API Call to Claude
Your environment is ready. Python is installed, the Anthropic SDK is in place, and your API key is set.
Now it's time to write the code that actually talks to Claude. You'll create a Python script that sends a prompt to the Claude API and prints a real AI-generated response in your terminal.
In this step, get ready to:
- Create a Python script that sends a prompt to Claude.
- Run the script and see a real AI response in your terminal.
- Make the script interactive so you can ask about any topic.
Create your first Python file
Every AI application starts the same way: import a library, connect to the model, send a message, and read the response. Your first script does exactly that in about 15 lines of Python.
- In VS Code, hover over the Explorer sidebar on the left and click the New File icon (a page with a plus sign).
- Type study_notes.py and press Enter.
- Copy the following code into study_notes.py:
import anthropic
# Create the Anthropic client (reads API key from environment automatically)
client = anthropic.Anthropic()
# Send a message to Claude asking for study notes
message = client.messages.create(
model="claude-haiku-4-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Create concise study notes on the following topic. Include key concepts, definitions, and 2-3 important things to remember:\n\nPhotosynthesis",
}
],
)
# Print Claude's response
print("\n--- Study Notes ---\n")
print(message.content[0].text)
What does this code do?
This script follows the standard pattern for calling any AI model through an API:
- anthropic.Anthropic() creates a client that handles authentication. It automatically reads your ANTHROPIC_API_KEY environment variable.
- client.messages.create() sends your prompt to Claude. The model parameter picks which AI model to use, and max_tokens caps the response length.
- message.content[0].text extracts the text from Claude's response. The API returns a list of content blocks, and for simple text responses, the first block contains your answer.
- Save study_notes.py by pressing Cmd+S (macOS) or Ctrl+S (Windows).
Run the script and see Claude respond
Time to see your code in action. Running this script sends a real request to Claude's servers and prints the AI-generated response.
- Run your script by entering this command in the terminal:
python study_notes.py
After a few seconds, you should see structured study notes about Photosynthesis printed in your terminal, complete with key concepts and definitions.
What just happened?
Your script sent a request over the internet to Anthropic's servers. Claude processed your prompt, generated a response, and sent it back. Your script received that response and printed it.
This is the exact same mechanism behind ChatGPT, Claude.ai, and every other AI chat product. The only difference is that those products add a web interface on top.
Seeing an error instead of study notes?
- If you see an authentication error, your ANTHROPIC_API_KEY environment variable may not be set in this terminal session. Re-run the set (CMD) or $env: (PowerShell) command from Step 1 to set it again.
- If you see a module not found error, make sure you installed the SDK with pip install anthropic in the same terminal session.
- If you see a connection error, check that your computer is connected to the internet.
Help me troubleshoot my Claude API call error
Make the topic dynamic using input()
Right now the topic is hardcoded to "Photosynthesis". Let's make the script ask you what topic you want notes on, so you can use it for any subject.
- In study_notes.py, find this line:
# Send a message to Claude asking for study notes
message = client.messages.create(
- Add these two lines directly above it:
# Ask the user what topic they want notes on
topic = input("What topic would you like study notes on? ")
- Now find the content line inside the messages list:
"content": "Create concise study notes on the following topic. Include key concepts, definitions, and 2-3 important things to remember:\n\nPhotosynthesis",
- Replace it with this f-string version that inserts the user's topic:
"content": f"Create concise study notes on the following topic. Include key concepts, definitions, and 2-3 important things to remember:\n\n{topic}",
What is an f-string?
An f-string (formatted string) lets you embed Python variables directly inside a string. The f before the opening quote tells Python to look for curly braces {topic} and replace them with the variable's value.
This is how you make prompts dynamic. Instead of asking about the same topic every time, the prompt now includes whatever the user typed.
- Save study_notes.py and run it again:
python study_notes.py
This time the script asks you to type a topic before calling Claude. Try typing something like "Machine Learning" or "The French Revolution" and watch Claude generate tailored study notes.
Script not asking for input?
- Make sure the topic = input(...) line is placed above the message = client.messages.create( line. If it's below the API call, Python tries to use topic before it exists.
- Check that the content string starts with f" (not just a plain quote). Without the f prefix, Python treats {topic} as literal text instead of inserting the variable.
Help me fix my input() and f-string code
✔️ Awesome, I've got everything!
Great. Double check you've saved your file before moving on.
ⓧ I'd like to double check the full code
Here's what your complete study_notes.py should look like:
import anthropic
client = anthropic.Anthropic()
topic = input("What topic would you like study notes on? ")
message = client.messages.create(
model="claude-haiku-4-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": f"Create concise study notes on the following topic. Include key concepts, definitions, and 2-3 important things to remember:\n\n{topic}",
}
],
)
print("\n--- Study Notes ---\n")
print(message.content[0].text)
You just built a working AI application. Your script takes user input, sends it to an AI model, and prints an intelligent response. Next up, you'll turn this into an interactive chatbot that remembers your conversation.
Build an Interactive Chatbot with Memory
Your study_notes.py script works, but it has a limitation. Every time you run it, Claude starts fresh with zero memory of what you discussed before.
Real AI assistants like ChatGPT and Claude.ai remember your conversation so you can ask follow-up questions. In this step, you will build an interactive chatbot that maintains conversation history across multiple messages.
In this step, get ready to:
- Understand how conversation memory works in the Claude API.
- Build an interactive chatbot loop that stores and sends message history.
- Test multi-turn conversation where Claude remembers context.
Understand conversation history
In the Claude API, conversation memory works by sending the full message history with every request. Each message has a role (either "user" or "assistant") and content.
How does Claude remember a conversation?
Claude does not store your conversation on its own. To make Claude "remember" what was said before, you store all messages in a Python list and send the entire list with each new API call.
Each message in the list is a dictionary with a role key (set to "user" or "assistant") and a content key (the actual text). Claude reads the full history and responds in context.
- In the VS Code Explorer sidebar, click the New File button (the plus icon at the top of the file list).
- Type chatbot.py and press Enter to create the file.
- Add the following setup code to chatbot.py:
import anthropic
# Create the API client (reads your ANTHROPIC_API_KEY automatically)
client = anthropic.Anthropic()
# Store all messages here so Claude can see the full conversation
conversation_history = []
print("AI Study Assistant (type 'quit' to exit)")
print("-" * 40)
What does this code do?
- conversation_history = [] creates an empty list that will hold every message in the conversation.
- The two print() lines display a welcome banner when the chatbot starts.
- The client is the same pattern you used in study_notes.py. It reads your API key from the environment variable automatically.
You won't run this file yet. The next section adds the conversation loop that brings it to life.
Add the conversation loop
The conversation loop continuously prompts for input, sends the full history to Claude, and appends each exchange to the list. This is the core pattern behind every AI chat application.
- Add the following while True loop below your setup code in chatbot.py:
while True:
# Get the user's message
user_input = input("\nYou: ")
# Exit if the user types 'quit'
if user_input.lower() == "quit":
print("Goodbye!")
break
# Add the user's message to conversation history
conversation_history.append({"role": "user", "content": user_input})
# Send the full conversation history to Claude
response = client.messages.create(
model="claude-haiku-4-5",
max_tokens=1024,
messages=conversation_history,
)
# Extract Claude's text response
assistant_message = response.content[0].text
# Add Claude's response to history so it remembers next time
conversation_history.append({"role": "assistant", "content": assistant_message})
print(f"\nClaude: {assistant_message}")
What does this code do?
- input("\nYou: ") waits for the learner to type something. The loop repeats after each exchange.
- If the user types quit, the loop stops with break.
- Each user message and assistant response gets appended to conversation_history. On every API call, the full list is sent as messages, so Claude always sees the complete conversation.
- response.content[0].text extracts the text from Claude's reply. The API returns a list of content blocks, and for simple text responses the first block contains the answer.
- Save chatbot.py by pressing Ctrl+S.
✔️ Awesome, I've got everything!
Great. Double check you have saved your file, then continue to the next section to test it.
ⓧ I'd like to double check the full code
Here is the complete chatbot.py file. Make sure your code matches this exactly:
import anthropic
client = anthropic.Anthropic()
conversation_history = []
print("AI Study Assistant (type 'quit' to exit)")
print("-" * 40)
while True:
user_input = input("\nYou: ")
if user_input.lower() == "quit":
print("Goodbye!")
break
conversation_history.append({"role": "user", "content": user_input})
response = client.messages.create(
model="claude-haiku-4-5",
max_tokens=1024,
messages=conversation_history,
)
assistant_message = response.content[0].text
conversation_history.append({"role": "assistant", "content": assistant_message})
print(f"\nClaude: {assistant_message}")
Test multi-turn conversation
Time to see conversation memory in action. You will have a multi-turn conversation and watch Claude reference earlier messages.
- Run your chatbot by entering this command in the terminal:
python chatbot.py
You should see the welcome banner appear:
AI Study Assistant (type 'quit' to exit)
----------------------------------------
- Try this conversation sequence to test memory:
- Type Explain what an API is and press Enter.
- After Claude responds, type Give me an analogy for that and press Enter.
- Then type How does this relate to AI engineering? and press Enter.
Notice how Claude's second and third responses clearly reference what was said before. It knows "that" refers to APIs because it can see the full conversation history.
- Type quit and press Enter to exit the chatbot cleanly.
Why does this work?
Every time you send a message, the script sends the entire conversation_history list to Claude. By message three, Claude receives all previous exchanges and can respond in full context. This is the exact same pattern that powers ChatGPT, Claude.ai, and every other AI chat product.
Seeing an error when you run the script?
- If you see AuthenticationError, your API key environment variable may have expired. Re-set it in your terminal with the same command you used in Step 1.
- If you see IndentationError, make sure all lines inside the while True: loop are indented by exactly four spaces.
- If Claude responds but does not seem to remember earlier messages, check that you are appending both the user and assistant messages to conversation_history before and after the API call.
Help me debug my chatbot.py script
Secret mission
Add a System Prompt Personality
Right now, your chatbot is a blank slate. It responds helpfully, but it doesn't have a defined personality or area of expertise. In this secret mission, you'll use system prompts to transform Claude into a specialized assistant with a unique voice and focus area.
Clean Up Your Resources
Clean Up Your Resources
This project runs entirely locally and only costs money when you actively run your scripts. There are no ongoing charges from leaving your files on your computer.
Decide whether to keep your resources for future experimentation, pause them to come back later, or delete them entirely.
Resources you used:
- ai-study-assistant project folder (containing study_notes.py and chatbot.py).
- Anthropic Python SDK (anthropic pip package).
- Anthropic API key (stored in the Anthropic Console).
- Python (installed via the Microsoft Store).
Keep everything running
No action needed. Choose this if you plan to keep experimenting with your AI study assistant or building more Python projects.
- Your project files stay in the ai-study-assistant folder. You can reopen them in VS Code anytime.
- Python and the Anthropic SDK remain installed and ready for future projects.
- Your API key remains active in the Anthropic Console. It only costs money when you run scripts that call the API.
- Remember that the ANTHROPIC_API_KEY environment variable only lasts for your current terminal session. You will need to set it again the next time you open a new terminal.
Pause - I'll come back to this later
Shut down running processes and free up your terminal. Your files and installations stay in place so you can pick up where you left off.
- Close any running Python scripts by pressing Ctrl+C in your terminal, or by typing quit if the chatbot is active.
- Close the terminal window in VS Code.
- Your project files, Python installation, and Anthropic SDK stay on your machine. No ongoing costs are incurred while the scripts are not running.
Delete - I don't want to use this again
Remove all project resources and start fresh. Follow each step below to fully clean up.
- Delete your project folder. In your VS Code terminal, navigate to the parent directory of your project, then remove the folder:
- CMD:
cd ..
rmdir /s /q ai-study-assistant
- PowerShell:
cd ..
Remove-Item -Recurse -Force ai-study-assistant
- Uninstall the Anthropic SDK by running:
pip uninstall anthropic -y
- Delete your API key from the Anthropic Console. Go to https://console.anthropic.com/.
- Navigate to your API keys list.
- Find the key you created for this project.
- Click the ellipsis next to it and select Delete API Key.
- Confirm the deletion.
- Uninstall Python. Open Settings on your computer.
- Go to Apps, then Apps & features.
- Find Python in the list, select it, and click Uninstall.
- Delete your Anthropic Console organization (optional). Log in to the Console, navigate to Settings then Organization, and click the red Delete organization button. Follow the on-screen prompts to confirm.
That's a Wrap!
That's a Wrap!
Nice work! You just built an AI-powered study assistant that calls the Claude API, generates structured study notes, and holds multi-turn conversations with memory.
You've learned how to:
- Built a Python script that calls the Claude API and generates AI-powered study notes on any topic.
- Created an interactive chatbot with conversation memory, the exact pattern behind ChatGPT and Claude.ai.
- Learned how APIs, prompt engineering, and message history work together to power AI applications.
- Secret Mission: Customized Claude's behavior using system prompts, the technique AI engineers use to build specialized AI assistants.
Ready to quiz yourself?