How to intermediate · 3 min read

FastAPI LLM app production checklist

Quick answer
To productionize a FastAPI app integrating an LLM, ensure secure API key management via environment variables, implement robust error handling and rate limiting, use asynchronous calls to the LLM API for scalability, and deploy with a production-ready server like uvicorn behind a reverse proxy. Monitor usage and logs continuously and apply caching where appropriate to optimize performance.

PREREQUISITES

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

Setup

Install required packages and set environment variables securely before starting development.

bash
pip install fastapi uvicorn openai>=1.0

# Set your API key securely in your environment
export OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]

Step by step

Build a minimal FastAPI app that calls the OpenAI gpt-4.1 model asynchronously with proper error handling and environment-based API key management.

python
import os
from fastapi import FastAPI, HTTPException
from openai import OpenAI
from pydantic import BaseModel

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

class PromptRequest(BaseModel):
    prompt: str

@app.post("/generate")
async def generate_text(request: PromptRequest):
    try:
        response = await client.chat.completions.acreate(
            model="gpt-4.1",
            messages=[{"role": "user", "content": request.prompt}]
        )
        return {"response": response.choices[0].message.content}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Run with:
# uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4

Common variations

  • Use synchronous calls if async is not needed, but async improves throughput.
  • Switch models by changing model parameter (e.g., gpt-4.1, claude-3-5-sonnet-20241022).
  • Implement streaming responses for real-time output using SDK streaming methods.
python
from openai import OpenAI
import os

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

# Synchronous example
response = client.chat.completions.create(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)

# Streaming example (async context required)
async def stream_response():
    async for chunk in client.chat.completions.stream(
        model="gpt-4.1",
        messages=[{"role": "user", "content": "Stream this"}]
    ):
        print(chunk.choices[0].delta.content, end="", flush=True)

Troubleshooting

  • If you get authentication errors, verify OPENAI_API_KEY is set correctly in your environment.
  • For rate limit errors, implement exponential backoff and consider request batching.
  • Use logging to capture exceptions and monitor API usage to detect anomalies early.

Key Takeaways

  • Always load API keys from environment variables to keep credentials secure.
  • Use asynchronous API calls in FastAPI to handle multiple LLM requests efficiently.
  • Deploy with a production server like uvicorn with multiple workers behind a reverse proxy.
  • Implement error handling and rate limiting to maintain app stability under load.
  • Monitor logs and usage metrics continuously to detect and fix issues early.
Verified 2026-04 · gpt-4.1, claude-3-5-sonnet-20241022
Verify ↗