By Ryze AI Team
This tutorial builds a minimal but complete chat loop in Python using the official OpenAI SDK. It covers the basic call, conversation history, streaming, and a few production touches.
pip install openai. Set your API key as an environment variable: export OPENAI_API_KEY=sk-.... The SDK reads it automatically. Never hardcode keys in source.
from openai import OpenAI
client = OpenAI()
history = [{"role": "system", "content": "You are a helpful assistant."}]
while True:
user = input("you: ")
if user.strip().lower() in ("quit", "exit"):
break
history.append({"role": "user", "content": user})
r = client.chat.completions.create(model="gpt-4o-mini", messages=history)
reply = r.choices[0].message.content
print("bot:", reply)
history.append({"role": "assistant", "content": reply})
The history list is what gives the model memory. Each turn appends the user message and the assistant reply, and the full list is sent on the next call.
For a responsive feel, stream the response. Pass stream=True and iterate over chunks:
stream = client.chat.completions.create(model="gpt-4o-mini", messages=history, stream=True)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
Trim history to the last 20 turns to keep token usage and latency bounded. Summarize older turns if you need long-term memory. Set max_tokens to a sensible ceiling so a runaway generation does not surprise you on the bill. Use temperature 0.2 for factual assistants and 0.7 to 0.9 for creative ones.
Wrap calls in try/except for rate limits and transient errors. The SDK raises RateLimitError and APIConnectionError. Implement exponential backoff with jitter for retries. Log failures with the request ID from the response for support. Never show raw API errors to users.
Add a system prompt tailored to your use case. Persist history to a database keyed by session. Add tool calling for actions. And measure latency and cost per request so you know where the money goes.
Docs: OpenAI documentation.