Sub topic 1
Running a model locally behind an HTTP server
Prerequisites
This chapter assumes you have:
- Basic Python and
pip. - Familiarity with HTTP requests and JSON.
- A general idea of what a large language model and a prompt are.
- A local model server installed, with at least one model downloaded.
No Python code runs in this chapter. You only check that your server is up. The later chapters build on this check.
The working model
A language model is a large file of numbers called weights. To get text out of it, a program must load the weights into memory and run the model's computation. That program is an inference server. It does the following:
- It loads the weights into RAM or GPU memory.
- It listens on a network port.
- It accepts HTTP requests that contain a prompt.
- It runs the model and sends back the generated text.
Your application never touches the weights. It only sends HTTP requests, so the same code can talk to any server that speaks the same request format.
Common local servers
Several projects provide this kind of server. They differ in setup and speed, but they all bind to localhost (your own machine) by default.
| Server | Typical use | Default port |
|---|---|---|
| Ollama | Simple setup, model downloads handled for you | 11434 |
| llama.cpp `llama-server` | Lightweight, runs GGUF model files on CPU or GPU | 8080 |
| LM Studio | Desktop app with a built-in server | 1234 |
| vLLM | High-throughput serving on GPUs | 8000 |
Ports can be changed in each tool's settings, so confirm yours before you rely on these numbers.
Starting a server and checking it
The examples below use Ollama. On most installs, Ollama already runs as a background service. If it does not, start it yourself.
# Download a model once. This can take several minutes.
ollama pull llama3.2
# Start the server in the foreground (skip if it already runs as a service).
ollama serveOpen a second terminal and check that the port answers:
curl http://localhost:11434/A running Ollama server replies with the text Ollama is running. Other servers return a different message or a status page. Any HTTP response means a process is listening. A "connection refused" error means no server is running on that port.
You can also confirm which models are downloaded:
ollama listThe model name in this list is the name you will send in requests later. A model must be downloaded before the server can use it.
What the server holds in memory
Loading a model has a cost, and it explains behavior you will see later.
- First request is slow. Many servers load the weights on first use, not at startup.
- Memory limits model size. A model must fit in RAM or GPU memory. Smaller or more compressed (quantized) versions need less.
- Idle models may be unloaded. Ollama, for example, frees memory after a period of inactivity, so the next request pays the load cost again.
Why an HTTP interface
Each project could have invented its own request format. Many instead copy the request and response shapes of the OpenAI API. That gives you one client library and one set of habits for many servers. The next chapter defines exactly what this compatibility covers.
Exercise
You run curl http://localhost:11434/ and get "Failed to connect to localhost port 11434: Connection refused". What does this tell you, and what do you try first?
Show answer
It tells you that no process is listening on that port, so the server is not running or is using a different port. Start the server (for example with ollama serve) or check which port your server is configured to use. A missing model would not cause this error, because the server would still answer on its port.