The terminal, the desktop app, coding tools, Python, Java: they all talk to Ollama the same way, by sending JSON over HTTP to http://localhost:11434. That is the Ollama REST API, and once you can use it directly, you can use Ollama from any language or tool.
This guide covers the two endpoints you will use most, /api/generate and /api/chat, and the difference between them. We test them with curl and then with Postman, first with only a user message and then with a system message too, and we read every field of the response. Finally we look at two settings that confuse everyone at first: stream and think.
It is part six of a series, but it stands on its own. All you need is Ollama installed and a model pulled, for example ollama pull qwen3.5:4b.
The base URL
Ollama runs as a small server on your own computer. Every endpoint starts with:
http://localhost:11434
localhost means “this computer” and 11434 is Ollama’s port. (Why it works this way is explained in What is Ollama?.) A quick check that the server is up is to list your models:
curl http://localhost:11434/api/tags
Endpoint 1: /api/generate (one prompt in, one answer out)
/api/generate is the simplest endpoint. You send one prompt, you get one answer. No conversation, no roles.
Windows Command Prompt (the inner quotes must be escaped as \"):
curl http://localhost:11434/api/generate -d "{\"model\":\"qwen3.5:4b\",\"prompt\":\"Explain RAG in simple terms\",\"stream\":false}"
macOS, Linux or Git Bash (wrap the JSON in single quotes, no escaping):
curl http://localhost:11434/api/generate -d '{"model":"qwen3.5:4b","prompt":"Explain RAG in simple terms","stream":false}'
The request body, formatted so it’s easier to read:
{
"model": "qwen3.5:4b",
"prompt": "Explain RAG in simple terms",
"stream": false
}
- model: which model to use.
- prompt: your text.
- stream:
falsemeans “send me one complete JSON when you’re done”. More on this below.
The response (example output; yours will differ):
{
"model": "qwen3.5:4b",
"response": "RAG means the model first looks up relevant facts, then answers with them...",
"done": true,
"prompt_eval_count": 15,
"eval_count": 118
}
The answer is in the response field. eval_count is how many tokens the model wrote, which is how speed and, on cloud APIs, cost are measured.
Endpoint 2: /api/chat (a conversation of messages)
/api/chat takes a list of messages instead of one prompt. Each message has a role (who wrote it) and content (what it says).
Windows Command Prompt:
curl http://localhost:11434/api/chat -d "{\"model\":\"qwen3.5:4b\",\"messages\":[{\"role\":\"user\",\"content\":\"Explain MCP with an example\"}],\"stream\":false}"
macOS, Linux or Git Bash:
curl http://localhost:11434/api/chat -d '{"model":"qwen3.5:4b","messages":[{"role":"user","content":"Explain MCP with an example"}],"stream":false}'
Formatted:
{
"model": "qwen3.5:4b",
"messages": [
{ "role": "user", "content": "Explain MCP with an example" }
],
"stream": false
}
And the answer comes back as a message, with the role assistant:
{
"model": "qwen3.5:4b",
"message": {
"role": "assistant",
"content": "MCP lets an AI app plug into tools like a calendar..."
},
"done": true,
"eval_count": 142
}
/api/generate vs /api/chat: the difference
/api/generate |
/api/chat |
|
|---|---|---|
| Input | One prompt string |
A list of messages, each with a role |
| Instructions | Optional system field |
A message with role system |
| Conversation history | Not built in | Send earlier user and assistant messages |
| Answer is in | response |
message.content (role assistant) |
| Best for | One-off tasks: summarise, translate, classify | Chatbots, assistants, agents, tool calling |
The short version: use /api/chat for almost everything. It does everything /api/generate does, plus conversations, plus tools. /api/generate is handy for quick single-shot jobs and for learning.
Notice that neither endpoint remembers anything between requests. If you want a conversation, you send the history in messages every time. That idea, being stateless, is the heart of the Python and Java guides.
Testing the API in Postman
curl is fine for one line, but the JSON comes back as one long unformatted line, which is hard to read. Postman is a free app for testing APIs. It formats the request and the response neatly, colours the JSON, shows status and timing, and lets you save requests to reuse. That is why we use it here: to see the messages clearly.
To set up a request:
- Click New → HTTP request.
- Set the method to POST and the URL to
http://localhost:11434/api/chat. - Open the Body tab, choose raw, and pick JSON from the dropdown.
- Paste a request body (below) and click Send.
Test 1: only a user message
Our first body has a single user message. We also set think to false (explained below) and stream to false so we get one tidy JSON back:
{
"model": "qwen3.5:4b",
"messages": [
{
"role": "user",
"content": "Explain AI in two sentences."
}
],
"think": false,
"stream": false
}

Let’s read the response, field by field, using the real numbers from this run:
| Field | Value here | What it means |
|---|---|---|
model |
qwen3.5:4b |
Which model answered |
created_at |
2026-11-15T06:19:52.6448518Z |
When the answer finished (UTC) |
message.role |
assistant |
Written by the model |
message.content |
“AI is a branch of computer science…” | The answer |
done |
true |
The answer is complete |
done_reason |
stop |
It stopped naturally (it could also stop because it hit a length limit) |
total_duration |
52318088200 | Total time for the request, in nanoseconds: about 52.3 s |
load_duration |
28710500 | Time to load the model: about 0.03 s (it was already in memory) |
prompt_eval_count |
19 | Tokens the model read (your input, including the chat template) |
prompt_eval_duration |
567510000 | Time to read them: about 0.57 s |
eval_count |
60 | Tokens the model wrote |
eval_duration |
6122402000 | Time to write them: about 6.1 s |
All the durations are in nanoseconds, so divide by 1,000,000,000 to get seconds. A useful number to work out is speed: 60 tokens in 6.12 seconds is about 9.8 tokens per second on this laptop.
You may notice that total_duration (52 s) is far bigger than load + reading + writing added together (about 6.7 s). The total counts everything from the moment the request arrived, so when it is much bigger than the parts, the request spent time outside those steps, such as waiting its turn. The writing itself took about 6 seconds, and that is the number to compare between models.
Test 2: a system message plus a user message
Now the same question, but with a system message first. The system message sets the rules: who the model is and how it must reply.
{
"model": "qwen3.5:4b",
"messages": [
{
"role": "system",
"content": "You are AI Explainer who can explain any complex topic with a metaphore. Reply with STRICT JSON"
},
{
"role": "user",
"content": "Explain AI in two sentences."
}
],
"think": false,
"stream": false
}

Three things changed:
- The style changed completely. The answer is now built around a metaphor: “Imagine an AI as a student who has read every book ever written…”, with a second line, “A library’s omniscient reader that never stops studying.” The user didn’t ask for a metaphor. The system message did.
- The format changed.
contentis now JSON withexplanationandmetaphorkeys. Notice it is JSON inside a string (you can see the escaped\"and\n), so your code readsmessage.contentand then parses it as JSON a second time. - The input got bigger.
prompt_eval_countwent from 19 to 44 tokens. The system message is sent, and read, on every request. Keep system messages focused: on a cloud API you would pay for those tokens every time.
The roles themselves (system, user and assistant) are explained in depth in System, user and assistant roles explained.
stream: true or false?
"stream": true is the default, if you leave the field out. Ollama sends the answer as it is written: many small JSON objects, one per line (a format called NDJSON). Each piece carries a little bit of the text:
{"model":"qwen3.5:4b","message":{"role":"assistant","content":"RAG"},"done":false}
{"model":"qwen3.5:4b","message":{"role":"assistant","content":" means"},"done":false}
{"model":"qwen3.5:4b","message":{"role":"assistant","content":" look"},"done":false}
{"model":"qwen3.5:4b","message":{"role":"assistant","content":""},"done":true,"eval_count":118}
Your app joins the pieces together. The last line has "done": true and all the timing and token stats. This is how chat apps show words appearing one by one: the user sees the answer start in under a second instead of staring at a spinner.
"stream": false makes Ollama wait until the whole answer is ready and send one complete JSON, like the Postman responses above. Simpler to read and to code, so we use it while learning. The trade-off is that nothing appears until the very end.
stream: true (default) |
stream: false |
|
|---|---|---|
| What arrives | Many small JSON lines | One JSON object |
| First words appear | Almost immediately | Only when the answer is finished |
| Your code | Read line by line, join the pieces | Read one JSON, take message.content |
| Good for | Chat UIs, long answers | Learning, scripts, Postman, parsing JSON |
The Python guide includes a working streaming example.
think: true or false?
Some models, including Qwen 3.5, are thinking models: they can reason step by step before giving the final answer. The think field controls that:
"think": trueasks the model to reason first. The reasoning comes back in a separate field,message.thinking(orthinkingon/api/generate), and the final answer stays inmessage.content. This often helps with maths, logic and tricky questions, but it writes many more tokens, so it is slower."think": falseskips the reasoning and goes straight to the answer. Faster, and fine for simple questions.- Leave it out, and the model uses its own default. Qwen 3.5 thinks by default, which is why our examples set
"think": false. On a laptop, thinking can be slow: in the Ollama desktop app, askingqwen3.5:4bto “Explain Ramayana in 10 lines” showed Thought for 331.9 seconds before the ten lines appeared (see the desktop app guide).
With thinking on, a response looks like this (example):
"message": {
"role": "assistant",
"thinking": "The user wants AI in two sentences. I should define it, then say how it works...",
"content": "AI is a branch of computer science..."
}
Show thinking to users only if it helps them; most apps hide it and show just content.
Other fields worth knowing
| Field | Example | What it does |
|---|---|---|
format |
"json" or a JSON schema |
Forces the reply to be valid JSON, or to match a schema |
options |
{ "temperature": 0.3, "num_ctx": 8192 } |
Generation settings: lower temperature is more focused; num_ctx sets the context window size |
keep_alive |
"10m", 0, -1 |
How long the model stays in memory after this request (default about 5 minutes; 0 unloads now; -1 keeps it loaded) |
tools |
a list of functions | Lets the model ask your code to call functions, the basis of AI agents |
Two more endpoints from this series
# Which models are on my disk?
curl http://localhost:11434/api/tags
# Turn text into an embedding (a vector of numbers), after: ollama pull nomic-embed-text
curl http://localhost:11434/api/embed -d '{"model":"nomic-embed-text","input":"Spring Boot is a Java framework"}'
Embeddings are what make semantic search and RAG work. See tokens and embeddings for the idea behind them.
When something goes wrong
- “Connection refused”: the Ollama server isn’t running. Open the Ollama app, or run
ollama serve. - A “model not found” error (HTTP 404): you haven’t downloaded that model, or the name or tag is spelled differently. Run
ollama pull qwen3.5:4band checkollama list. - Very slow first answer: the model is being loaded into memory. Later requests within about 5 minutes are faster.
- Invalid JSON from curl on Windows: a quote isn’t escaped. Use
-d @body.jsonor Postman instead.
Key takeaways
- Every client talks to Ollama with JSON over HTTP at
http://localhost:11434. /api/generate: onepromptin,responseout./api/chat: a list of role-taggedmessagesin, amessageout. Prefer/api/chat.- A system message changes style and format for every answer, and its tokens are read on every request.
- Durations are in nanoseconds.
eval_count / eval_durationgives you tokens per second. stream:true(default) sends many small pieces;falsesends one JSON.think:trueadds a reasoning step inthinking;falseskips it.
The full series: Run LLMs locally with Ollama
Each part stands on its own, so jump to whichever you need:
- How to run an LLM on your own computer
- What is Ollama? The video player for AI models, and localhost:11434 explained
- Ollama CLI commands: the complete cheat sheet
- The Ollama desktop app: chat, pick models, and launch Claude Code or Copilot
- System, user and assistant roles explained
- Ollama REST API: /api/generate vs /api/chat (you are here)
- Call a local LLM from Python: a stateless request, then a chatbot with memory
- Call a local LLM from Java: HttpClient, a stateless request, then a chatbot with memory
See it in action
Prefer watching? This video walks through the same ideas with live, hands-on demos.
Watch on YouTube ↗
Comments