A prototype often calls a model provider directly from an endpoint. Production needs stronger boundaries around request size, concurrency, timeouts, provider errors, and cost. FastAPI makes it straightforward to expose an API, but those operational choices remain application responsibilities.
Validate and bound requests
Use Pydantic models for the request contract and enforce limits on prompt length, uploaded data, and requested output. Reject unsupported options rather than silently ignoring them. Keep provider credentials in server-side secrets and avoid including prompts or responses in routine logs unless there is a clear, consented retention policy.
Call external providers with timeouts and handle rate limits, unavailable services, and malformed responses as distinct outcomes. Retries should be bounded and should not repeat non-idempotent work without an idempotency key. If generation takes longer than a normal HTTP request, use a job model with status polling or a streaming contract that can recover from disconnects.
Manage lifecycle and capacity
Create reusable clients and connection pools in application lifespan hooks, and close them during shutdown. Set concurrency according to provider quotas and local resource limits; unrestricted parallel requests can turn a slow provider into a queue explosion. Keep health and readiness checks distinct when dependencies are optional or slow.
Measure latency by provider and phase, failures, token usage, and queue wait. Test provider timeouts, cancellation, oversized input, and startup without optional credentials. A production service is not simply a running endpoint: it has explicit limits and a useful failure contract.
Separate asynchronous I/O from model work
FastAPI calls regular def path-operation functions in an external thread pool. It awaits async def handlers on the asynchronous path, so calling a blocking SDK directly there can delay unrelated requests. Use an asynchronous HTTP client for network-bound provider calls when available. Put heavy inference or CPU-bound preprocessing behind an execution boundary designed for it. FastAPI concurrency guide
Define an observable request boundary before connecting the first model. Check that invalid input is rejected locally, that the provider has a bounded deadline, and that the caller can distinguish a provider timeout from an input error. Reuse long-lived clients through the app's lifespan when appropriate and close them at shutdown. Exercise the failure paths with a small fake provider that delays or fails, then test the actual provider separately under its configured limits. FastAPI lifespan events
Worked example
This FastAPI/Pydantic example validates a request without invoking a model. Verify empty and oversized input are rejected before adding provider calls and timeouts.
pythonfrom fastapi import FastAPI from pydantic import BaseModel, Field app = FastAPI() class Prompt(BaseModel): text: str = Field(min_length=1, max_length=4000) @app.post("/validate") async def validate(prompt: Prompt): return {"characters": len(prompt.text)}




Community
Discussion
Loading comments…