Back to Blog

Build Your First Python AI Bot in 15 Minutes

Here’s the minimal Python code to get a conversational AI bot running locally. No frameworks, no cloud setup — just Python and an OpenAI key.

What you need


Step 1 — Install

bash
pip install openai python-dotenv

Step 2 — The simplest possible bot

Create a .env file in your project folder:

env
OPENAI_API_KEY=sk-...your-key-here...

Then create bot.py:

python
import os
import openai
from dotenv import load_dotenv

load_dotenv()

client = openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"])

SYSTEM_PROMPT = """You are a helpful assistant. Be concise and direct.
Answer the user's question, then stop."""

conversation = [{"role": "system", "content": SYSTEM_PROMPT}]

print("Bot ready. Type 'quit' to exit.\n")

while True:
    user_input = input("You: ").strip()
    if user_input.lower() in ("quit", "exit"):
        break
    if not user_input:
        continue

    conversation.append({"role": "user", "content": user_input})

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=conversation,
    )

    reply = response.choices[0].message.content
    conversation.append({"role": "assistant", "content": reply})
    print(f"\nBot: {reply}\n")

That’s the entire pattern. The conversation list grows with each turn, so the bot remembers context. gpt-4o-mini costs roughly $0.15 per million input tokens — cheap enough to leave running while you build.


Step 3 — Run it

bash
python bot.py

Expected output:

Bot ready. Type 'quit' to exit.

You: What's the difference between a list and a tuple in Python?

Bot: Lists are mutable (you can change them after creation), tuples are not.
Use lists when the contents will change, tuples when they're fixed — like
coordinates or function return values. Tuples are also slightly faster.

You:

What to do next

This is the core pattern. If you want a customer support bot, a sales bot, or a social media content bot — those just need a more specific system prompt and a few extra behaviors. The pre-built packs at RoboSmith skip the iteration so you go straight to something production-ready.

Right now: buy any bot pack, get a second one free instantly.

Ready to go further?

Need the full template?

Pre-built system prompts, conversation memory, retry logic, and production patterns — ready to run.

Grab a Python bot pack