When a local model feels slow, generation speed and request latency can tell different stories. A model can generate at 40 output tokens per second and still take much longer than expected because it first has to load and process a prompt. Ollama’s generation response exposes timings for different stages. Use output-token count and generation duration for the decode rate, then keep total duration as its own metric.

Run a completed-response fixture

Use Node.js 24 LTS or a newer supported Node.js release. Save as speed.mjs and run node speed.mjs. No server or npm dependency is needed. The program parses one synthetic JSON record with the fields documented for Ollama’s generation endpoint.

import assert from 'node:assert/strict';

function generationRate(body) {
  const result = JSON.parse(body);
  if (result?.done !== true) throw new TypeError('final record required');
  const { eval_count: count, eval_duration: ns } = result;
  if (!Number.isSafeInteger(count) || count < 0 ||
      !Number.isSafeInteger(ns) || ns < 0) {
    throw new TypeError('invalid generation counters');
  }
  return ns === 0 ? null : count / (ns / 1e9);
}

const fixture = { done: true, eval_count: 24, eval_duration: 600_000_000,
                  prompt_eval_duration: 300_000_000, total_duration: 1_500_000_000 };
const rate = generationRate(JSON.stringify(fixture));
assert.equal(rate, 40);
assert.equal(generationRate(JSON.stringify({ ...fixture, eval_duration: 0 })), null);
assert.equal(generationRate(JSON.stringify({ ...fixture, eval_count: 0 })), 0);
for (const bad of [{ done: false }, { ...fixture, eval_count: '24' },
                   { ...fixture, eval_duration: -1 }, { done: true }]) {
  assert.throws(() => generationRate(JSON.stringify(bad)), TypeError);
}
console.log(`generation=${rate.toFixed(1)} tokens/s`);

Expected output

generation=40.0 tokens/s

The fixture produced 24 tokens in 0.6 generation seconds. Dividing by the 1.5-second total would give 16 tokens per second, which answers a different question. Zero tokens with a positive generation duration yields an observed rate of zero. Zero duration yields no rate and is returned as null. Missing counters, negative timings and numeric strings are rejected rather than blended into a performance average.

Use the helper with captured metrics

For an existing caller of /api/generate, a request with stream: false makes the response a single JSON object. With streaming enabled, use the final parsed record, not the concatenated response body or an intermediate chunk. If parsing fails, first check HTTP status and whether the response was a stream or an error. If the server metric looks healthy but the user experience is slow, also measure client wall time and time to first token. Neither is measured by this helper.

For a run-level decode rate, sum eligible output counts and divide by summed generation seconds. Also record model, prompt length, generation settings and warm or cold conditions so comparisons are interpretable. Do not assume a server timing field measures network time.

References and related reading: Ollama generation endpoint fields, Ollama API metrics units, and Java Samples node.js tutorials.