How to build an AI chatbot in Python
Direct answer
Use the
openai Python SDK v1 to create a chatbot by initializing the client with your API key, sending user messages to client.chat.completions.create, and extracting the response from choices[0].message.content.Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI Examples
inHello, who are you?
outI am an AI chatbot here to assist you.
inCan you help me write Python code?
outAbsolutely! What kind of Python code do you need help with?
inTell me a joke.
outWhy did the programmer quit his job? Because he didn't get arrays.
Integration steps
- Install the OpenAI Python SDK and set your API key in the environment variable
OPENAI_API_KEY. - Import
OpenAIand initialize the client with the API key fromos.environ. - Build a messages list with user input as
{"role": "user", "content": "..."}. - Call
client.chat.completions.createwith the modelgpt-4oand the messages list. - Extract the chatbot reply from
response.choices[0].message.contentand display it.
Full code
import os
from openai import OpenAI
def main():
# Initialize OpenAI client with API key from environment
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
print("AI Chatbot (type 'exit' to quit)")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Goodbye!")
break
messages = [{"role": "user", "content": user_input}]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
reply = response.choices[0].message.content
print(f"Bot: {reply}")
if __name__ == "__main__":
main() output
AI Chatbot (type 'exit' to quit) You: Hello, who are you? Bot: I am an AI chatbot here to assist you. You: Tell me a joke. Bot: Why did the programmer quit his job? Because he didn't get arrays. You: exit Goodbye!
API trace
Request
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello, who are you?"}]} Response
{"choices": [{"message": {"content": "I am an AI chatbot here to assist you."}}], "usage": {"total_tokens": 20}} Extract
response.choices[0].message.contentVariants
Streaming Chatbot ›
Use streaming to improve user experience with partial token outputs for longer responses.
import os
from openai import OpenAI
def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
print("AI Chatbot Streaming (type 'exit' to quit)")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Goodbye!")
break
messages = [{"role": "user", "content": user_input}]
stream = client.chat.completions.create(
model="gpt-4o",
messages=messages,
stream=True
)
print("Bot: ", end="", flush=True)
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)
print()
if __name__ == "__main__":
main() Async Chatbot ›
Use async for integrating the chatbot into asynchronous applications or web servers.
import os
import asyncio
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
print("Async AI Chatbot (type 'exit' to quit)")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Goodbye!")
break
messages = [{"role": "user", "content": user_input}]
response = await client.chat.completions.create(
model="gpt-4o",
messages=messages
)
reply = response.choices[0].message.content
print(f"Bot: {reply}")
if __name__ == "__main__":
asyncio.run(main()) Using Anthropic Claude Model ›
Use Anthropic Claude for a strong alternative chatbot model with a different style and capabilities.
import os
from anthropic import Anthropic
def main():
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
print("Claude AI Chatbot (type 'exit' to quit)")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Goodbye!")
break
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": user_input}]
)
reply = response.content[0].text
print(f"Bot: {reply}")
if __name__ == "__main__":
main() Performance
Latency~800ms for <code>gpt-4o</code> non-streaming response
Cost~$0.002 per 500 tokens exchanged with <code>gpt-4o</code>
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
- Keep prompts concise to reduce token usage.
- Use shorter model variants like <code>gpt-4o-mini</code> for cost savings.
- Cache frequent responses to avoid repeated calls.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Standard Chat Completion | ~800ms | ~$0.002 | Simple chatbot with full responses |
| Streaming Chat Completion | ~800ms initial + streaming | ~$0.002 | Better UX for long answers |
| Async Chat Completion | ~800ms | ~$0.002 | Integration in async apps or web servers |
Quick tip
Always manage your API keys securely via environment variables and avoid hardcoding them in your code.
Common mistake
Beginners often use deprecated SDK methods like <code>openai.ChatCompletion.create()</code> instead of the current <code>client.chat.completions.create()</code> pattern.