Using the Ollama REST API with curl
Ollama exposes a simple REST API on http://localhost:11434. The two endpoints you will use most are /api/generate for single prompts and /api/chat for conversations. Both accept JSON and can be tested straight from curl, with no SDK needed.
/api/generate: one prompt, one answer
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Give me a one-line definition of recursion.",
"stream": false
}'
The response is a JSON object. The important fields are response (the text), done (true when finished), and counters such as prompt_eval_count and eval_count. Durations are reported in nanoseconds.
/api/chat: messages with roles
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [
{"role": "system", "content": "You answer in one short sentence."},
{"role": "user", "content": "What is a REST API?"}
],
"stream": false
}'
Each message has a role of system, user or assistant. The reply arrives in message.content. To continue a conversation you send the full history again on the next call, because the server does not remember it for you. Post 44 turns that into Python code.
Passing options
Generation settings go in an options object:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Write a haiku about servers.",
"stream": false,
"options": {
"temperature": 0.7,
"num_ctx": 4096,
"num_predict": 100,
"seed": 42
}
}'
num_ctx is the context window size, num_predict caps the number of generated tokens, and seed makes sampling repeatable when other settings are the same. Settings in a request override those in the model's Modelfile for that call only.
Keeping the model loaded
The keep_alive field controls how long the model stays in memory after the request. A value like "10m" keeps it for ten minutes, 0 unloads it right away, and -1 keeps it loaded indefinitely:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"keep_alive": "30m"
}'
A request with no prompt simply loads the model, which is a neat way to warm it up before users arrive.
Listing and inspecting models
curl http://localhost:11434/api/tags
curl http://localhost:11434/api/ps
curl http://localhost:11434/api/show -d '{"model": "llama3.2"}'
/api/tags lists downloaded models, /api/ps lists running ones, and /api/show returns details for one model. These mirror the list, ps and show commands.
Generate versus chat: which to use
The two endpoints produce text in the same way, but they suit different jobs. Use /api/generate for one-off tasks where there is no conversation: summarise this text, classify this ticket, extract these fields. The prompt is a plain string and the code stays short. Use /api/chat whenever there is a system message, several turns, or when you want the model's own chat template applied for you. The chat endpoint formats the messages with the template that ships with the model, which matters because each model family expects its own special tokens around roles. With /api/generate you can pass a system field and a template too, but most people let the chat endpoint do that work.
Why the API is stateless
The server does not remember earlier requests. Every call is independent, and the model only knows what is in the request body. This surprises people who expect the server to track a session. It is also a strength: you can run many different conversations through one server, retry a failed call safely, and move requests between servers without any shared state. The cost is that your client owns the history, and you decide what to trim when it grows too long.
Timeouts and cold starts
The first request after a model has been unloaded must read many gigabytes from disk into memory, which can take several seconds or more for larger models. Set generous client timeouts, and consider sending a warm-up request at application start. Later requests skip the load and begin generating almost at once, as long as the model stays resident.
Reading errors
If you name a model that is not downloaded, you get a JSON error saying the model was not found, and you fix it with ollama pull. A connection refusal means the server is not running. Add -i to curl to see the HTTP status code when debugging.
Use jq to read the output
curl -s http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Name a prime number.",
"stream": false
}' | jq -r '.response'
The -s flag silences curl's progress meter and jq -r prints the raw string. Without "stream": false, the default is streaming, which the next post covers.
Checklist
- Always include
model; it must already be pulled. - Set
streamexplicitly so you know which format you will get. - Send the whole history on each chat call.
- Put per-request settings in
options, not at the top level.
Comments
No comments yet. Have a question or a tip? Be the first to share it.