---
title: "Serve the Model Over HTTP"
description: "Put a loaded LLM behind an HTTP endpoint with FastAPI and uvicorn, call it from your laptop, stream tokens over HTTP, and see where a simple server stops."
canonical_url: "https://fanout.sh/inference-eng/curriculum/01-05"
md_url: "https://fanout.sh/inference-eng/curriculum/01-05.md"
last_updated: "2026-11-11"
access: "public"
---

# Serve the Model Over HTTP

Put a loaded LLM behind an HTTP endpoint with FastAPI and uvicorn, call it from your laptop, stream tokens over HTTP, and see where a simple server stops.

A model that only answers the person holding the Python session is a demo. Products talk to models over HTTP, and this lesson puts the model [model loading and tokenization](/inference-eng/curriculum/01-02) loaded behind an endpoint: a real server on the GPU box, called from your laptop through the tunnel the lesson on [remote GPU setup](/inference-eng/curriculum/00-07) taught. Stage 5 of [the setup lab](/inference-eng/curriculum/00-09) rehearsed this call with a stand-in; today the server is real. The server skeleton on this page was run and curled this session (with a stub in the model's place, since the skeleton is what a laptop can verify); the generation outputs are representative runs from the box.

This is also a homecoming. The course's first lesson, [inference engineering](/inference-eng/curriculum/in-01), opened with a dozen-line serve.py as a cautionary artifact, the thing whose failure under real traffic defines the discipline. You now know enough to build it for real and to see exactly where its edges are.

## The server

The field's default pair for a first server is FastAPI, which turns a Python function into an HTTP endpoint, and uvicorn, which runs the app. On the box:

Apart from the two new imports, everything above the app line is [model loading and tokenization](/inference-eng/curriculum/01-02) verbatim, run once at startup so the 16 GB load happens before the first request, not during it. The endpoint itself is the generation from [token generation and streaming](/inference-eng/curriculum/01-03) with a request body around it. The lock line is load-bearing: FastAPI runs a plain def endpoint in a thread pool, so without it two callers' generations would run at once and interleave on the one GPU, both slowing. With it, the server takes one request at a time, a choice the last section returns to. Start it inside tmux:

The server binds to localhost on purpose: the tunnel is the front door, and putting real authentication and admission in front of a model is the production spaces' business, not a first server's.

## Calling it

From your laptop, with the tunnel from [remote GPU setup](/inference-eng/curriculum/00-07) open (ssh -L 8000:localhost:8000 gpubox):

The sentence coming back is the greedy continuation from [token generation and streaming](/inference-eng/curriculum/01-03), now arriving over a network. That response crossing your laptop's terminal is the whole inference lifecycle [what inference is](/inference-eng/curriculum/in-01) drew, operated by you end to end: an artifact chosen and downloaded, loaded onto rented hardware, wrapped in a service, answering requests over a network. Any program that can speak HTTP, a notebook, a script, another service, can now use your model.

## Streaming over HTTP

The streaming from [token generation and streaming](/inference-eng/curriculum/01-03) had one consumer: your terminal. Over HTTP, the same effect uses a streaming response, with the generation running in a background thread while the endpoint hands fragments out as they arrive:

Call it with curl -N localhost:8000/stream ... (the -N turns off curl's buffering) and the continuation arrives in your laptop's terminal the way it arrived in the box's terminal in [token generation and streaming](/inference-eng/curriculum/01-03), fragment by fragment. This is, at skeleton level, how every chat product delivers its answers. The lock rides along in locked_generate, held by the generation thread itself, so a stream and a plain request never share the device either.

## What this server is not

Run two curl calls at the same moment and the second answer arrives only after the first finishes: the lock admits one request at a time, and every other caller waits in line. How fast callers can arrive before that line stops draining is a measured boundary, located in [saturation and failure boundaries](/inference-eng/curriculum/05-05). Nothing here shares one read of the weights across users, the opportunity the lesson on [inference engineering](/inference-eng/curriculum/in-01) said nobody serving production traffic leaves unused; nothing admits, queues, retries, or measures. This is the naive stack, complete and working as designed, and it is exactly the artifact inference engines picks up: its opening lesson is titled "Why Plain PyTorch Serving Breaks Down," and its lab benchmarks this very server against a real engine.

The module closes with the [lab](/inference-eng/curriculum/01-06): this server exists because you assembled it by hand this afternoon, and the lab makes it come back identically from a fresh start.

---
This representation contains public Fanout content only. Protected Pro lessons, account data, billing, checkout, and pricing are not included.

Browse the public content map: https://fanout.sh/sitemap.md
