Dependency Injection for LLM Clients
What you'll learn: How to inject configured LLM clients into FastAPI endpoints using dependency injection to share resources efficiently across requests.
The Core Problem
When building LLM APIs, you don't want each endpoint creating its own LLM client from scratch. That would mean redundant configurations, wasted connections, and higher latency. Instead, you want to create one properly configured client and reuse it across all requests.
Why Dependency Injection?
Think of dependency injection like a restaurant's prep station: instead of each chef walking to the pantry every time they need olive oil, the kitchen maintains a shared bottle that's always ready. FastAPI's dependency system lets you prepare shared resources (like LLM clients) once and inject them wherever needed.
How It Works
You define a function that returns your configured LLM client, then FastAPI automatically calls that function and passes the result to your endpoints:
from fastapi import FastAPI, Depends
from openai import AsyncOpenAI
app = FastAPI()
# The dependency function
async def get_llm_client():
return AsyncOpenAI(api_key="your-key", timeout=30.0)
# Inject it into your endpoint
@app.post("/chat")
async def chat(
message: str,
client: AsyncOpenAI = Depends(get_llm_client)
):
response = await client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": message}]
)
return {"reply": response.choices[0].message.content}
FastAPI calls get_llm_client() automatically and passes the client to your handler. You can inject the same client into multiple endpoints without duplicating configuration logic.
Key Takeaway: Dependency injection in FastAPI lets you centralize LLM client configuration and reuse properly configured, pooled connections across all endpoints, improving both code maintainability and performance.