What You'll Build
By the end of this guide, you'll have a working chatbot that holds conversations and remembers context. We use Python, but concepts apply to any language.
Step 1: Install the SDK
pip install openaiStep 2: Basic Conversation
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is machine learning?"}
]
)
print(response.choices[0].message.content)Step 3: Add Memory
Keep conversation history so the AI "remembers":
messages = [{"role": "system", "content": "You are helpful."}]
while True:
user_input = input("You: ")
if user_input.lower() == "quit": break
messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="gpt-4o-mini", messages=messages)
reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
print(f"Bot: {reply}")Step 4: Customise
The system message is where the magic happens. For customer support: "You are a support agent for [Company]. Be friendly, concise, and offer to escalate to a human." For writing: "You are a professional editor. Suggest changes and explain why."
Using GPT-4o mini at $0.15/$0.60 per million tokens, a chatbot handling 1,000 conversations/day (~500 tokens each) costs roughly $15/month.