Building your own AI chatbot is no longer exclusive territory for large companies. With Python and Anthropic's Claude API, you can have a functional chatbot in less than 30 minutes. In this tutorial I guide you step by step, from zero to a real conversation with memory.
What We're Going to Build
By the end of this tutorial you'll have:
- A console chatbot that responds in natural language.
- Conversation memory: the bot remembers what you told it before.
- Customizable personality: you can make it a cooking assistant, a language tutor, a finance expert, whatever you need.
Prerequisites
- Python 3.8 or higher installed.
- Basic Python knowledge (variables, functions, loops).
- An account at console.anthropic.com.
Step 1: Get Your Anthropic API Key
Go to console.anthropic.com, create a free account and go to "API Keys". Generate a new key and store it in a safe place. Never share it or upload it to GitHub.
Step 2: Install the Anthropic SDK
Open the terminal and run:
pip install anthropic
And to handle the API key securely, also install python-dotenv:
pip install python-dotenv
Step 3: Set Up Environment Variables
Create a .env file in your project:
ANTHROPIC_API_KEY=your_api_key_here
Important: add .env to your .gitignore to avoid accidentally exposing it.
Step 4: Your First Message to Claude
Create a file chatbot.py with this code:
import anthropic
from dotenv import load_dotenv
load_dotenv()
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, who are you?"}
]
)
print(message.content[0].text)
Run with python chatbot.py and you'll see your first response from Claude.
Step 5: Add Conversation Memory
For the bot to remember context, you need to maintain the message history:
import anthropic
from dotenv import load_dotenv
load_dotenv()
client = anthropic.Anthropic()
history = []
print("Chatbot ready. Type 'quit' to exit.\n")
while True:
user_input = input("You: ").strip()
if user_input.lower() == 'quit':
break
if not user_input:
continue
history.append({"role": "user", "content": user_input})
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
messages=history
)
text = response.content[0].text
history.append({"role": "assistant", "content": text})
print(f"Claude: {text}\n")
Now the bot remembers the entire conversation. You can tell it "my name is Alex" and then ask "what is my name?" and it will remember.
Step 6: Customize the Personality
The system prompt defines how the bot behaves. Add it when creating the message:
response = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
system="""You are a professional chef specializing in Mediterranean cuisine.
You always respond in English with enthusiasm and passion for food.
If asked about something outside your specialty, you kindly redirect
the conversation toward culinary topics.""",
messages=history
)
Change the system prompt and you have a completely different bot: math tutor, legal assistant, sports coach, whatever you need.
Step 7: Optional Improvements
For a more complete chatbot, consider adding:
- History limit: Use only the last N messages to avoid exceeding the token limit.
- Web interface: With Flask or FastAPI you can expose it as a REST API and build a UI.
- Streaming: Use
client.messages.stream()to display responses word by word, like ChatGPT. - Persistence: Save history in a JSON file or SQLite database.
Next Steps
With this foundation you can build incredible things: an assistant for your business, a personalized tutor, a bot that analyzes documents, or an agent that executes tasks automatically. The official Anthropic API documentation at docs.anthropic.com has everything you need to go further.
If you have questions or want to share what you've built, write to me. I love seeing what people create with these tools.