How to build a chatbot using OpenAI API in python
Direct answer
Use the
OpenAI SDK in Python to create a client with your API key, then call client.chat.completions.create with a conversation message array to build a chatbot.Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI Examples
inHello, who won the world series in 2020?
outThe Los Angeles Dodgers won the World Series in 2020.
inCan you help me write a Python function to reverse a string?
outSure! Here's a Python function to reverse a string:
```python
def reverse_string(s):
return s[::-1]
```
inTell me a joke about computers.
outWhy do programmers prefer dark mode? Because light attracts bugs!
Integration steps
- Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY
- Import the OpenAI client and initialize it with the API key from os.environ
- Create a messages list with user input as a dictionary with role 'user' and content
- Call client.chat.completions.create with the model 'gpt-4o' and the messages list
- Extract the chatbot's reply from response.choices[0].message.content
- Print or return the chatbot's response to the user
Full code
import os
from openai import OpenAI
def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
print("Chatbot is ready. Type your message and press enter (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}\n")
if __name__ == "__main__":
main() API trace
Request
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello, who won the world series in 2020?"}]} Response
{"choices": [{"message": {"content": "The Los Angeles Dodgers won the World Series in 2020."}}], "usage": {"total_tokens": 42}} Extract
response.choices[0].message.contentVariants
Streaming Chatbot ›
import os
from openai import OpenAI
def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
print("Streaming chatbot ready. Type your message (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:
print(chunk.choices[0].delta.get("content", ""), end="", flush=True)
print("\n")
if __name__ == "__main__":
main() Async Chatbot ›
import os
import asyncio
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
print("Async chatbot ready. Type your message (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.acreate(
model="gpt-4o",
messages=messages
)
reply = response.choices[0].message.content
print(f"Bot: {reply}\n")
if __name__ == "__main__":
asyncio.run(main()) Use Smaller Model for Cost Efficiency ›
import os
from openai import OpenAI
def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
user_input = "Explain recursion in simple terms."
messages = [{"role": "user", "content": user_input}]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
print("Bot:", response.choices[0].message.content)
if __name__ == "__main__":
main() Performance
Latency~800ms for gpt-4o non-streaming
Cost~$0.002 per 500 tokens exchanged with gpt-4o
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
- Keep messages concise to reduce token usage
- Use smaller models for less critical tasks
- Cache frequent responses to avoid repeated calls
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Standard Chat Completion | ~800ms | ~$0.002 | General chatbot use |
| Streaming Chat Completion | Starts immediately, total ~800ms | ~$0.002 | Real-time user interaction |
| Async Chat Completion | ~800ms (concurrent) | ~$0.002 | High concurrency environments |
| Smaller Model (gpt-4o-mini) | ~400ms | ~$0.0005 | Cost-sensitive or lightweight tasks |
Quick tip
Always keep conversation history in the messages array to maintain context in your chatbot.
Common mistake
Beginners often forget to set the API key in the environment variable or use deprecated SDK methods.