🐍 Python Tutorial

How to Build a Custom Chatbot with Claude API in 2026 — The Weekend Project

How to Build a Custom Chatbot with Claude API in 2026 — Python code on screen, NeuraPulse
PL
Prashant LalwaniJuly 8, 2026 · NeuraPulse · 15 min read
15 min readPythonAPITutorial

I was tired of generic chatbots. You know the ones — they sound helpful but give you the same canned responses. I wanted a bot that actually understood my specific niche, could remember context from earlier in the conversation, and didn't hallucinate as much as a politician.

So, I spent last weekend building my own. No wrappers, no third-party platforms, just raw Python and the Anthropic API. It was easier than I thought, and the result? It's now the most useful tool in my daily workflow.

If you've been looking to build something similar, or just want to understand what's under the hood of those expensive AI subscriptions, you're in the right place. I'm going to walk you through the exact code I wrote, the prompts that make it smart, and how to deploy it so you can use it forever.

🎯 What You'll Build: A persistent, context-aware chatbot running locally on your machine. It uses Claude 3.5 Sonnet, remembers your conversation history, and follows a custom persona you define. Total time: ~2 hours.

Step 1: The Setup (Don't Skip This)

Before we write code, we need the keys to the kingdom. I used Cursor IDE to write this because its built-in terminal made switching between code and running scripts seamless, but VS Code works perfectly too.

First, you need an API key from Anthropic. Go to the console, create a key, and save it somewhere safe. Do not commit this to GitHub (I learned that the hard way).

# Install the official Anthropic SDK
pip install anthropic

Next, set your environment variable. This keeps your key out of your code.

# On Mac/Linux
export ANTHROPIC_API_KEY='your-key-here'

# On Windows (CMD)
set ANTHROPIC_API_KEY=your-key-here

If you are completely new to the API or get stuck on the environment setup, my detailed guide on using Claude API in Python covers the virtual environment quirks and troubleshooting steps.

Step 2: The "Hello World" Bot

Let's get a response first. Create a file named bot.py. We'll start with a simple loop that sends your input to Claude and prints the reply.

import anthropic
import os

client = anthropic.Anthropic() # Automatically finds your API key

def chat():
    print("Bot: Hello! I'm ready. Type 'quit' to exit.")
    
    while True:
        user_input = input("You: ")
        if user_input.lower() == 'quit':
            break
            
        message = client.messages.create(
            model="claude-3-5-sonnet-20260708",
            max_tokens=1024,
            messages=[
                {"role": "user", "content": user_input}
            ]
        )
        
        print(f"Bot: {message.content[0].text}")

if __name__ == "__main__":
    chat()

Run this (python bot.py). It works, right? But it's dumb. It has no memory. If you ask "What is my name?" and then "What was the first thing I asked?", it will fail. We need to fix that.

Step 3: Giving It Memory (Context Window)

The magic of LLMs is the context window. To make the bot remember, we have to send the *entire* conversation history back to the API with every new message. It sounds inefficient, but with Claude's 200k token limit, it's actually fine for most use cases.

We'll use a list to store our messages.

import anthropic
import os

client = anthropic.Anthropic()
conversation_history = []

def chat():
    print("Bot: I'm listening...")
    
    while True:
        user_input = input("You: ")
        if user_input.lower() in ['quit', 'exit']:
            break
            
        # Add user message to history
        conversation_history.append({"role": "user", "content": user_input})
        
        message = client.messages.create(
            model="claude-3-5-sonnet-20260708",
            max_tokens=1024,
            messages=conversation_history
        )
        
        bot_response = message.content[0].text
        
        # Add bot response to history so it remembers next time
        conversation_history.append({"role": "assistant", "content": bot_response})
        
        print(f"Bot: {bot_response}")

if __name__ == "__main__":
    chat()

Now, try it. Ask it a fact, then ask it to recall that fact later. It works. But it's still a bit... generic. It doesn't have a personality.

Step 4: The Secret Sauce (System Prompts)

This is where the real engineering happens. The system parameter allows you to give the model instructions that persist across the whole conversation. This is how you turn a generic assistant into a specialized expert.

I wanted my bot to be a "Senior Python Mentor" — strict about best practices but encouraging.

SYSTEM_PROMPT = """You are a Senior Python Mentor. 
Your goal is to help the user write clean, efficient, and Pythonic code.
Rules:
1. Never write code that uses global variables unless absolutely necessary.
2. Always suggest type hinting.
3. If the user makes a mistake, explain *why* it's bad practice, not just how to fix it.
4. Keep responses concise. No fluff."""

# Inside your loop:
message = client.messages.create(
    model="claude-3-5-sonnet-20260708",
    max_tokens=1024,
    system=SYSTEM_PROMPT, # <--- THE MAGIC LINE
    messages=conversation_history
)

If you want to dive deeper into how to structure these prompts for complex reasoning, I wrote a full breakdown on chain-of-thought prompting that will make your bot significantly smarter at solving logic puzzles.

Step 5: Giving It a Brain (RAG vs. Context)

What if you want the bot to answer questions about *your* specific PDF documentation or codebase? You have two choices:

  1. Paste it in: If your docs are short (under 50k words), just paste them into the system prompt. It's lazy but it works.
  2. RAG (Retrieval-Augmented Generation): For massive datasets, you need to vectorize your data and fetch only the relevant chunks.

If you are trying to decide which path to take, my comparison of RAG vs Fine-tuning explains when you should just use context (RAG) and when you actually need to train the model (Fine-tuning).

For this weekend project, let's stick to the "lazy" method. Just load your text file and inject it into the system prompt.

with open('my_knowledge_base.txt', 'r') as f:
    knowledge = f.read()

SYSTEM_PROMPT = f"""You are a helpful assistant.
Here is the context you must use to answer questions:
---
{knowledge}
---
Answer only based on the context above. If you don't know, say "I don't know."
"""

Alternative: The "I Hate Code" Route

Look, if reading the code above made you want to close your laptop, that's okay. Not everyone wants to be a Python engineer. There are incredible visual builders now that let you drag-and-drop these exact same concepts (memory, system prompts, tools) into a workflow.

If you want the power of a custom agent without writing a single line of Python, check out my guide on how to build AI agents without coding. It covers tools that use the same underlying tech but give you a visual interface.

Model Showdown: Which One to Use?

Not all Claude models are created equal. Here is how I choose which one to power my bots.

ModelSpeedIntelligenceBest Use Case
Claude 3.5 Haiku⚡⚡⚡⚡⚡⭐⭐⭐Simple Q&A, classification, high-volume tasks.
Claude 3.5 Sonnet⚡⚡⚡⭐⭐⭐⭐⭐The sweet spot. Complex chatbots, coding agents, analysis.
Claude 3 Opus⭐⭐⭐⭐⭐+Deep research, nuanced creative writing, PhD-level logic.

💡 Pro Tip: For 90% of chatbot use cases, Sonnet is the right answer. It's fast enough for real-time chat but smart enough to follow complex instructions. Use Haiku only if you are processing thousands of messages a day and need to save money.

Frequently Asked Questions

No, the Claude API is paid usage-based. However, Anthropic offers free credits when you sign up, which is enough to build and test your first chatbot extensively. After that, you pay per token, but it's surprisingly cheap for personal use (usually pennies per conversation).

Claude 3.5 Sonnet is the best balance of speed and intelligence for most chatbots. It follows instructions better than Haiku and is much faster than Opus. Use Haiku for simple, high-speed tasks, and Opus only for extremely complex reasoning.

Yes. If you don't want to write Python, you can use no-code platforms like Flowise or Voiceflow to build AI agents. Check out our guide on building AI agents without coding for the best visual tools.

The code above remembers things *within* a session. To make it remember across different sessions (days/weeks), you need to save the conversation_history list to a local database (like SQLite or a JSON file) and load it every time the script starts.

Final Verdict: Just Build It

Building your own chatbot used to be a massive undertaking. Today, it's a weekend project. The code above is less than 50 lines, but it gives you a tool that is completely private, customizable, and free from the "guardrails" that make commercial bots so frustrating.

Start with the basic script. Get it running. Then, tweak the system prompt until it feels like yours. Once you see it understand a complex instruction perfectly, you'll be hooked.

🚀 Start Today: Don't overthink it. Open your terminal, pip install anthropic, and paste the code from Step 2. Get the "Hello World" working first. The rest is just tweaking.