# Measure Ollama Tokens Per Second With a Short Python Script

Tinker note: measure Ollama tokens per second on your own hardware with a short Python script, and compare models using real numbers.

Author: Steven Phan
Published: 2026-09-03
Article: https://stevenphan.ca/blog/measure-ollama-tokens-per-second/

## Quick answer

Published speed numbers rarely match your machine, so measure it yourself. Ollama reports timing data with every response, which means you can calculate tokens per second for any local model with a dozen lines of Python. This is a quick tinker note for comparing models on your own hardware.

If you have not installed Ollama yet, start with [Run a Local LLM with Ollama](https://stevenphan.ca/blog/run-a-local-llm-with-ollama/).

<nav class="toc" aria-label="Table of contents">
<p><strong>On this page</strong></p>
<ol>
<li><a href="#what-ollama-tells-you">What Ollama tells you</a></li>
<li><a href="#the-script">The script</a></li>
<li><a href="#tips-for-fair-comparisons">Tips for fair comparisons</a></li>
<li><a href="#faq">FAQ</a></li>
<li><a href="#where-to-go-next">Where to go next</a></li>
</ol>
</nav>

## What Ollama tells you

When you call `/api/generate` with `"stream": false`, the response includes several timing fields. They are listed in the [Ollama usage documentation](https://docs.ollama.com/api/usage), and all durations are in nanoseconds:

- `eval_count`: how many tokens the model generated.
- `eval_duration`: time spent generating those tokens.
- `load_duration`: time spent loading the model into memory.
- `prompt_eval_count` and `prompt_eval_duration`: the same, for reading your prompt.

The [API reference on GitHub](https://github.com/ollama/ollama/blob/main/docs/api.md) gives the formula for generation speed: `eval_count / eval_duration * 10^9`.

## The script

Save this as `speed.py`:

```python
import sys

import requests

model = sys.argv[1] if len(sys.argv) > 1 else "llama3.2"
prompt = "Write a 200 word story about a robot who tends a garden."

response = requests.post(
    "http://localhost:11434/api/generate",
    json={"model": model, "prompt": prompt, "stream": False},
    timeout=600,
)
if not response.ok:
    # A 404 here usually means the model has not been pulled yet.
    raise SystemExit(f"Ollama returned {response.status_code}: {response.text}")
data = response.json()

tokens = data["eval_count"]
seconds = data["eval_duration"] / 1e9
load_seconds = data["load_duration"] / 1e9

print(f"model:        {model}")
print(f"tokens:       {tokens}")
if seconds > 0 and tokens >= 0:
    print(f"speed:        {tokens / seconds:.1f} tokens/second")
else:
    print("speed:        unavailable: invalid generation timing")
print(f"model load:   {load_seconds:.2f} seconds")
```

Run it with `python speed.py`, or pass another model name. Pull any other model first, for example `ollama pull gemma3` (a few gigabytes), and then run `python speed.py gemma3`. The script needs Python 3.9 or newer and the `requests` package.

## Tips for fair comparisons

- **Run it twice and compare.** Load time is reported separately from generation speed, so it does not distort the tokens-per-second figure. The first run can still be slower from cold caches, and the `load_duration` line shows how long loading took.
- **Check where the model runs.** If the number is far lower than you expected, run `ollama ps`. A model that does not fit in graphics memory gets split between GPU and CPU, which can slow generation.
- **Use the same prompt every time.** Output length changes the numbers, so keep the test consistent.
- **Close heavy apps.** Browsers and games compete for memory.
- **Test the size you will actually use.** Larger models are slower, and the gap is often bigger than people expect.

Generation speed excludes prompt processing and loading. Different models may use different tokenizers, so tokens per second is not a direct measure of equivalent work. Also measure time to finish the same useful task and inspect answer quality.

## FAQ

### How do you measure Ollama tokens per second?

Call `/api/generate` with `"stream": false` and read `eval_count` and `eval_duration` from the response. Generation speed is `eval_count / eval_duration * 10^9`, because all durations are in nanoseconds. The script in this post does that for any model name you pass it, and it needs Python 3.9 or newer and the `requests` package.

### Why is your Ollama tokens per second lower than expected?

Run `ollama ps` to check where the model runs. A model that doesn't fit in graphics memory gets split between GPU and CPU, which can slow generation. Browsers and games also compete for memory, so close heavy apps before you test.

### Does model load time change the tokens per second number?

No. Load time is reported separately as `load_duration`, so it doesn't distort the tokens-per-second figure. The first run can still be slower from cold caches, so run the script twice and compare. Generation speed also excludes prompt processing.

### Can you compare tokens per second between different models?

Only with care. Different models may use different tokenizers, so tokens per second is not a direct measure of equivalent work. Use the same prompt every time, because output length changes the numbers, and test the model size you will actually use. Also measure the time to finish the same useful task and inspect answer quality.

### How do you test a second model with this script?

Pull it first, for example `ollama pull gemma3` (a few gigabytes), and then run `python speed.py gemma3`. If you pass no model name, the script uses `llama3.2`. A 404 from Ollama usually means the model hasn't been pulled yet.

## Where to go next

Run the script across two or three models and write the results in a small table: model, tokens per second, and a one-line note on answer quality. Speed alone is not the goal. You are looking for the fastest model that is still good enough for your task.

Then try the same idea on a real workload, such as the retrieval step in [Chat With Your Own Notes: A Simple RAG App in Python](https://stevenphan.ca/blog/chat-with-your-notes-rag-python/).
