oc-ls-stats

A TUI plugin for OpenCode that displays live prefill rate (PP) and generation rate (TG) from a llama.cpp-based llama-server.

There are many plugins to show tokens per second, but the reason for this one is that when running a local model I often found myself not knowing what the server was currently doing, which meant constantly switching over to a console where I could see its output. Especially during prefill/prompt processing which can take more than a minute with no feedback in the UI from other plugins I tried.

Another motivation was to display the data of interest, but in a non-intrusive way with no UI elements jumping around. This plugin thus only shows a single line, with one numeric value per active phase (tokens per second) and an indicator as to whether the model is currently doing prompt processing or inference (token generation). When parallel slots are in different phases both values are shown.

I'm using the llama-server /slots endpoint to get the needed data. Strata (a llama.cpp-derived server with its own HTTP layer) and Basalt (a Strata fork) are also supported: they expose live prefill/decode rates under /metrics, and the plugin detects them automatically. If you connect opencode to an unrelated provider the plugin will just display "n/a" since it's not getting any data to display.

Note: As explained in further detail below the data that's displayed has to be deduced from llama-server's output. Sometimes the plugin might display PP for prompt processing while in reality the model is doing TG. If additional developments are made to the llama-server output data the plugin might be able to discern between them in a better way, but I think is as good as it gets for now.

I made this for my own usage. If you find it useful as well I'm just happy.

/Troed

thanks to Tarquinen for their oc-tps, which I used as a base although I guess most of the code has now been replaced

Installation

opencode plugin @troed/oc-ls-stats@latest --global

Requires opencode 1.3.14 or newer.

TUI plugins are loaded from ~/.config/opencode/tui.json, which after installation should look like this:

{
  "plugin": ["@troed/oc-ls-stats@latest"]
}

Display Format

The plugin renders a single line in the session prompt right slot:

1247 tps (PP)                  -- during prefill
  25 tps (TG)                  -- during generation
1180 tps (PP) · 46 tps (TG)    -- one slot prefilling, another generating
   - tps (TG)                  -- idle
          n/a                  -- unable to reach llama-server

The figures are totals across every processing slot; see Multi-slot aggregation.

Detection and Calculation

Server Discovery

By default the plugin discovers the llama-server URL from OpenCode's resolved provider list (id or name containing "llama", excluding "ollama"), extracting baseURL/base_url from the provider options. This covers providers from all sources (config, environment, API). If the provider list is unavailable, it falls back to parsing the OpenCode configuration directly, and finally to http://localhost:8080.

When several llama-server providers are detected, stats follow the provider used by the current session; until a session has picked a model, every detected server is polled.

If your llama-server provider is named differently, configure the endpoint explicitly via the plugin's server option in tui.json. An explicit server replaces discovery entirely:

{
  "plugin": [
    ["@troed/oc-ls-stats@latest", { "server": "http://<server-host>:8080" }]
  ]
}

A baseURL-style value ending in /v1 is also accepted.

Discovery runs when the plugin loads. If it yields no provider servers (for example the TUI started while the OpenCode service was still booting and answered 503), the plugin keeps retrying every 5 seconds instead of sticking to the http://localhost:8080 fallback. A session that is not on a discovered server (e.g. one using a cloud provider) shows n/a.

The plugin detects each server's type automatically by probing GET /health: a Strata server reports "service": "strata" and a Basalt server reports "service": "basalt". You can bypass detection when needed with a serverType option:

{
  "plugin": [
    ["@troed/oc-ls-stats@latest", { "serverType": "basalt" }]
  ]
}

serverType accepts "llama", "strata" or "basalt"; omit it (or set "auto") for automatic detection.

Slot Polling

Every 500ms, the plugin polls each discovered server, always carrying the selected model as ?model=<model> — the prompt's live selection, falling back to the session record. For a llama.cpp server it calls GET /slots?model=<model>; for a Strata or Basalt server it calls GET /health?model=<model> (to identify the server) and GET /metrics?model=<model> (reading the live object). Plain llama.cpp, Strata and Basalt servers ignore the extra parameter; a routing layer that dispatches by model (e.g. one fronting several nodes) uses it to forward the call to the backend serving that model. If no model is known yet, the plugin polls without it — direct servers still answer, while a model-dispatched router rejects the bare call and the plugin shows n/a.

When a server returns an error (e.g. HTTP 502) or is unreachable, the plugin backs off with an exponentially increasing delay per server (1s, 2s, 4s, ...) capped at 10s, with a small random jitter to stagger multiple servers. Backoff resets as soon as the server responds successfully again.

A single failed poll is not treated as an outage: a routing layer in front of a single-slot backend briefly answers 503 (or holds /slots open) while the backend switches tasks or loads a model. Requests carry a 5s timeout, an in-flight poll is never started twice, and failures inside a 10s grace window keep the last reading on screen while retrying at the normal 500ms cadence. Only once the window outlives the grace does the readout switch to n/a and the backoff kick in.

State Classification

Each processing slot is classified as prefill (PP) or generation (TG) by comparing n_decoded and n_prompt_tokens against a per-slot, per-task baseline. The plugin keeps one tracker per slot, keyed by the slot id as a string — routers report ids such as "ws-router-0", plain llama.cpp reports 0, 1, …:

  • slotTrackers[slotId] — stores { idTask, prevNd, prevNt, classification, rate, sampledAt }

The idTask field ensures baselines are invalidated when a slot starts a new task (task reuse).

Classification logic:

  1. First time or after task change/reset: If we have no baseline for this slot+task, or n_decoded or n_prompt_tokens dropped below baseline (indicating a new request), use a heuristic: n_decoded > 0 → TG, otherwise → PP.
  2. Compare against baseline: If n_decoded > baselineNd → TG. If n_prompt_tokens > baselineNt → PP.
  3. Neither increased: Persist the previous classification. This handles frequent polling where neither field increases on every poll cycle.

When a slot stops being processed, its tracker is removed.

Prefill Rate (PP)

During prefill, the plugin calculates the instantaneous prompt processing rate:

  1. On first detection of a prefill slot, the current n_prompt_tokens is captured as the baseline.
  2. On subsequent polls, the delta in n_prompt_tokens is divided by the elapsed time in seconds.
  3. The rate is updated only when both dt > 0 and delta > 0.

The per-slot n_prompt_tokens field is used instead of the global llamacpp:prompt_tokens_total from /metrics because the global counter already includes tokens from all slots; the plugin derives each slot's share itself, and using the global counter as well would double-count when multiple slots are active simultaneously.

Generation Rate (TG)

During generation, the plugin calculates the instantaneous token generation rate:

  1. On first detection of a generation slot, the current n_decoded is captured as the baseline.
  2. On subsequent polls (same slot ID), the delta in n_decoded is divided by the elapsed time in seconds.
  3. The rate is updated only when both dt > 0 and delta > 0.

A task change (id_task bump) or a counter reset starts a fresh baseline for that slot.

Multi-slot aggregation

A llama-server started with --parallel N reports N slots in /slots. Each slot's rate is computed independently and the readout shows the sum across all slots in a phase: two slots prefilling at ~128 tok/s each read 256 tps (PP), the server's total prompt throughput, and two generating slots add their decode rates likewise. When slots are in different phases both are shown, e.g. 1180 tps (PP) · 46 tps (TG). While slots are batched their per-slot rates are usually equal, so a single active slot and several batched slots can look similar, but the total is what the server is delivering.

/slots carries no session identity (id, id_task, is_processing, … only), so the plugin cannot attribute a slot to the session whose prompt bar it renders. Every OpenCode instance connected to the same server therefore shows the same server-wide figure.

A slot whose counters stop advancing for 5 seconds is treated as not producing: its rate drops to zero and it leaves the total until it advances again. This matters when one slot is starved behind another slot's long prefill — it otherwise kept showing the rate from its last single token for minutes. A slot also reports its average over the time since its previous advance, so a resume after a pause shows the true low rate rather than a short-interval spike.

Strata and Basalt Support

Strata is a llama.cpp-derived server with its own Python HTTP layer. Basalt is a fork of Strata with the same stats surface. Their /slots route is a minimal llama-server compatibility stub (id, n_ctx, is_processing only) with no next_token, id_task, or n_prompt_tokens, so the delta-based classification above cannot produce a rate against them.

Both instead report the live rates directly in GET /metrics, under live:

  • state — reading (prompt processing), generating, idle, or unloaded; Basalt also reports loading and asleep
  • prefill_tok_s_mean — prompt processing rate while reading (PP)
  • tok_s — instantaneous decode rate while generating (TG)

The plugin detects them per server URL from GET /health (service: "strata" or service: "basalt") and maps these fields straight onto the display, so PP/TG are shown without delta estimation. Detection is cached per URL and model for a short TTL (10 s), and a failed /metrics read drops the cached kind immediately: a router that swaps the backend behind a stable URL (for example a conduct/bazinga profile swap between Basalt and llama.cpp) is re-detected instead of leaving the plugin polling the wrong endpoint. Servers of all kinds can be polled side by side. Like the /slots requests, the plugin sends no API key; if Strata or Basalt is configured to require one, it cannot read the stats.

Limitations

Progress Percentage

The plugin cannot display prefill progress percentage. The /slots endpoint returns n_prompt_tokens (current prompt size) and n_prompt_tokens_processed (tokens processed), but not the final prompt size (task->n_tokens() from llama.cpp). Progress requires the ratio n_prompt_tokens_processed / task->n_tokens().

What Would Improve Compatibility

The following changes to the /slots endpoint would improve the plugin's functionality:

  1. Expose final prompt size: Add n_prompt_tokens_total (or n_tokens) to the /slots output, representing task->n_tokens() from llama.cpp. This would enable prefill progress percentage calculation as (n_prompt_tokens_processed / n_prompt_tokens_total) * 100.

  2. Per-slot metrics endpoints: Currently, the /metrics endpoint provides only global counters (llamacpp:prompt_tokens_total, llamacpp:prompt_tokens_seconds). Per-slot metrics would allow independent rate tracking without relying on slot state classification.

  3. Slot transition notifications: The plugin polls every 500ms to detect state transitions. A WebSocket or SSE-based notification system for slot state changes would reduce polling overhead and improve detection latency.

  4. Stall detection: When generation stalls (e.g., due to context window limits), n_decoded remains constant while n_remain stops decreasing. The plugin detects this via zero delta but has no way to distinguish a stall from normal generation. An explicit stalled flag in the slot output would help.

  5. Model-agnostic slot data: The /slots endpoint requires a model parameter. Returning all slots without model filtering, or supporting * as a wildcard, would simplify discovery when multiple models are loaded.

Source code repo

For known issues, posting new ones, forking or contributing:

https://git.sync.wtf/troed/oc-ls-stats

Debug Logging

Debug logging is controlled by the DEBUG_ENABLED constant in tui.tsx. When enabled, full slot state data is written to /tmp/oc-ls-stats-debug.log on every poll.

License

Creative Commons Zero (CC0 1.0 Universal)

S
Description
No description provided
Readme
6.5 MiB
0 Stars 1 Watchers 0 Forks
Languages
TypeScript 100%