How to Intermediate · 3 min read

How to optimize LLM inference speed

Quick answer
Optimize LLM inference speed by using batching to process multiple requests simultaneously, selecting smaller or faster models like gpt-4o-mini, and applying quantization or caching techniques. Efficient API usage and asynchronous calls also reduce latency.

PREREQUISITES

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

Setup

Install the OpenAI Python SDK and set your API key as an environment variable to securely authenticate requests.

bash
pip install openai>=1.0

Step by step

This example demonstrates batching multiple prompts in one API call to reduce overhead and speed up inference using the gpt-4o-mini model.

python
import os
from openai import OpenAI

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

prompts = [
    {"role": "user", "content": "Translate 'Hello' to French."},
    {"role": "user", "content": "Summarize the benefits of AI."}
]

# Batch requests by sending multiple messages in one call
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=prompts
)

print(response.choices[0].message.content)
output
Bonjour
AI improves efficiency, automates tasks, and enables new innovations.

Common variations

Use asynchronous calls to avoid blocking your application, switch to faster models like gpt-4o-mini for lower latency, or implement caching to reuse frequent responses.

python
import asyncio
import os
from openai import OpenAI

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

async def async_inference():
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Explain quantum computing in simple terms."}]
    )
    print(response.choices[0].message.content)

asyncio.run(async_inference())
output
Quantum computing uses quantum bits to perform complex calculations much faster than classical computers.

Troubleshooting

If you experience high latency, check your network connection and reduce batch size to avoid timeouts. Also, verify you are using the latest SDK version and a low-latency model. For rate limit errors, implement exponential backoff retries.

Key Takeaways

  • Batch multiple prompts in one API call to reduce overhead and improve throughput.
  • Choose smaller or optimized models like gpt-4o-mini for faster inference.
  • Use asynchronous API calls to prevent blocking and improve responsiveness.
  • Implement caching for repeated queries to avoid redundant inference.
  • Monitor and handle rate limits and network issues to maintain consistent speed.
Verified 2026-04 · gpt-4o-mini
Verify ↗