How to beginner · 3 min read

Fix chatbot slow response time

Quick answer
Fix chatbot slow response time by using stream=True in client.chat.completions.create to stream tokens as they arrive, reducing wait time. Also, optimize prompt length, use faster models like gpt-4o-mini, and cache frequent responses to improve speed.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the latest openai Python SDK and set your API key as an environment variable.

bash
pip install openai>=1.0

Step by step

Use streaming to receive partial chatbot responses immediately, reducing perceived latency. Also, choose a faster model and keep prompts concise.

python
import os
from openai import OpenAI

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

messages = [
    {"role": "user", "content": "Explain the benefits of AI in healthcare."}
]

# Stream response to reduce wait time
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    stream=True
)

print("Chatbot response:")
for chunk in stream:
    delta = chunk.choices[0].delta.content or ""
    print(delta, end="", flush=True)
print()
output
Chatbot response:
AI improves healthcare by enabling faster diagnosis, personalized treatment, and efficient data analysis.

Common variations

You can also optimize response time by using asynchronous calls or switching to different models like gpt-4o for better speed-quality balance.

python
import asyncio
import os
from openai import OpenAI

async def async_chat():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [{"role": "user", "content": "Summarize AI trends in 2026."}]
    stream = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        stream=True
    )
    print("Async chatbot response:")
    async for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        print(delta, end="", flush=True)
    print()

if __name__ == "__main__":
    asyncio.run(async_chat())
output
Async chatbot response:
AI trends in 2026 include widespread adoption of multimodal models, improved reasoning, and real-time collaboration tools.

Troubleshooting

  • If responses are still slow, check your network latency and API rate limits.
  • Reduce prompt size and avoid unnecessary context to speed up processing.
  • Use caching for repeated queries to avoid redundant API calls.

Key Takeaways

  • Use stream=True to receive tokens incrementally and reduce wait time.
  • Choose faster models like gpt-4o-mini for quicker responses.
  • Keep prompts concise and cache frequent queries to improve speed.
  • Use asynchronous calls to handle multiple requests efficiently.
  • Monitor network and API limits to avoid bottlenecks.
Verified 2026-04 · gpt-4o-mini, gpt-4o
Verify ↗