FastAPI: the default backend for AI in Python.
FastAPI is a modern Python web framework for building APIs: you declare endpoints as typed functions, it validates requests, generates documentation and runs asynchronously on Uvicorn. Because AI code lives in Python and AI endpoints spend their time waiting on models, its async design fits. FaceVision's backend and the RAG APIs I describe on this site are FastAPI services.
- Style
- Typed functions become validated endpoints
- Runs on
- Uvicorn, an ASGI server; async by design
- Gives you
- Validation, docs, streaming, WebSockets
- I use it in
- FaceVision, RAG APIs, extraction services
- 5production AI apps
- 14engineers led
- 200+students mentored
Questions students ask
The FastAPI questions searched most, answered from shipping AI services on it.
What is FastAPI?
FastAPI is an open-source Python framework, created by Sebastián Ramírez in 2018, for building web APIs. You write a function, annotate its parameters and return type with Python type hints, decorate it with a path and method, and FastAPI turns it into an endpoint that parses and validates the request, serialises the response, documents itself in an interactive page, and runs asynchronously. It is built on Starlette for the web layer and Pydantic for data validation. It became the default choice for AI and data services because it is quick to write, hard to misuse and fast enough for production.
Why FastAPI is fast
Two meanings, both true. Fast to run: it is an ASGI framework, so a single process handles many concurrent requests by switching between them while they wait on I/O, such as a model API call or a database query, instead of blocking; with Uvicorn's event loop and Pydantic's compiled validation it is among the quickest Python frameworks. Fast to write: type hints replace hand-written parsing and validation, errors are caught before your code runs, and the generated documentation means less to maintain. For AI services, where most request time is spent waiting on a model, the concurrency is what keeps latency low under load.
What is FastAPI used for?
- Serving machine learning models and LLM pipelines as HTTP endpoints, with streaming responses for chat.
- Backends for web and mobile apps that need validated JSON APIs.
- Microservices that wrap a specific capability: OCR, embeddings, retrieval, extraction.
- Internal tools and dashboards' data layers.
- WebSocket services for live features such as transcription or collaborative editing.
- Any Python service where typed, documented, async endpoints save time; FaceVision's API that stores and matches face embeddings is one of mine.
Why FastAPI is better than Flask
Better for this kind of work, not in general. Flask is minimal and synchronous: simple to start, flexible, with a large ecosystem, but validation, serialisation and documentation are add-ons, and concurrency needs workers or extensions. FastAPI builds validation, docs and async in from the start, so an AI endpoint that calls a model API, streams tokens and validates a JSON schema is less code and fewer bugs. Flask remains a fine choice for small synchronous apps and for teams that know it well. If the service will call models, databases or other APIs and must stay responsive, FastAPI's design is the better fit.
How does FastAPI handle multiple requests?
Async endpoints run on an event loop: when one request awaits a network call, the loop serves others, so one process handles many simultaneous requests whose work is mostly waiting. Endpoints declared as ordinary synchronous functions run in a thread pool so they do not block the loop, which is right for CPU-bound work such as running a local model, though heavy computation is better moved to a separate worker or a dedicated inference server. For more CPU, run several worker processes with Uvicorn or Gunicorn, and scale horizontally behind a load balancer. The rule: await I/O, offload CPU, and measure with a load test before launch.
Does FastAPI use Uvicorn?
FastAPI is the framework; it needs an ASGI server to run, and Uvicorn is the usual one. Uvicorn listens for HTTP connections, parses requests and hands them to FastAPI, using a fast event loop. In development you run Uvicorn with auto-reload; in production you run it with several workers, often managed by Gunicorn with Uvicorn worker processes, behind a reverse proxy such as Nginx or a platform's load balancer. FastAPI's own command-line tool wraps Uvicorn for convenience. Alternatives such as Hypercorn and Granian exist and work the same way.
Does FastAPI support WebSockets?
Yes, natively, through Starlette: declare a WebSocket endpoint, accept the connection, and read and send messages in an async loop. WebSockets suit two-way, long-lived connections: live transcription where audio chunks go up and text comes down, collaborative features, or an agent that sends progress events and receives user replies. For one-way streaming of a model's output, server-sent events over a normal streaming response are simpler and pass through more proxies, which is what I use for chat answers; WebSockets are for when the client also needs to talk continuously.
When to use FastAPI
- The service is in Python and calls models, databases or external APIs: async pays off.
- You want request validation and documentation without writing them by hand.
- You need streaming responses or WebSockets for AI features.
- You are building microservices that other systems call.
- Not the best fit for: server-rendered websites with templates and sessions (Django is more complete), tiny scripts (a plain function is enough), or extremely CPU-bound code (put it in a worker or a dedicated inference server).
Where to host FastAPI
For a student or a prototype: Render's free web service tier (it sleeps when idle), Hugging Face Spaces with a Docker space, or Google Cloud Run's free allowance, all of which run a container with Uvicorn. Railway offers a trial credit and then paid plans. Vercel can run FastAPI as serverless functions for light, short requests, which works for simple endpoints but not for long streams or stateful workloads. For production: a container on Cloud Run, Fly.io, AWS or a small VPS with Docker, behind a load balancer, with secrets in the platform's environment settings. Keep the model calls external or in a separate inference service so the API tier stays light.
How to install FastAPI in VSCode
Open your project folder in VS Code, create a virtual environment from the integrated terminal and select it as the interpreter in the status bar so IntelliSense and linting use it. Install fastapi with its standard extras (which include Uvicorn) with pip. Create a file with a FastAPI app and one endpoint, run it with the FastAPI command-line tool or Uvicorn in development mode from the terminal, and open the interactive docs page in your browser to test. Add the Python extension for debugging; you can launch Uvicorn under the debugger and set breakpoints in endpoints. Commit a requirements file and an ignored env file for keys from the first day.
An AI endpoint in an hour
The skeleton every service on this site starts from.
- 01
Typed request and response
Pydantic models for input and output; validation and docs come free.
- 02
Async model call
Await the provider client; stream tokens back as server-sent events.
- 03
Limits and logs
Auth header, rate limit, timeout, structured logs with token usage.
- 04
Container and deploy
A Dockerfile with Uvicorn; secrets in the platform; a health endpoint.
In the room
Where these systems get explained to students.


Read next
Building and deploying the service.
A backend that stays fast under AI load?
I build FastAPI services with streaming, limits and load tests, and deploy them where they cost the least.

