Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 7,797 Bytes
a8fb296 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | ---
sidebar_position: 30
title: LLM and GPU usage tracking
description: How every model call and GPU step is recorded, and how to read the numbers.
---
# LLM and GPU usage tracking
Every model call and every GPU step writes one usage row, so a model, prompt or batching choice can be compared on
measured numbers. The work is logged in [#262](https://github.com/getcommunityone/open-navigator/issues/262). The
code is `packages/llm/src/llm/usage/`.
## What is recorded
| Record | One row per | Written by | What it holds |
|---|---|---|---|
| Model call | call, after its retries | `call_gemini_text` (Gemini and DeepSeek), the local Gemma backend | input, cached input, output and thinking tokens; latency including retries; attempts; outcome |
| GPU step | step wrapped in `gpu_run(...)` | WhisperX diarization, a local Gemma generation | wall time, GPU count and name, GPU-hours, mean utilization, peak memory, energy |
A failed call is recorded too, because its latency and attempts are what the retries cost. A count the API does not
report is stored as null. It is never estimated.
Rows are appended to JSON-lines files under `LLM_USAGE_LOG_DIR` (default `data/logs/llm_usage/`):
- `llm_calls_YYYYMMDD.jsonl`
- `gpu_runs_YYYYMMDD.jsonl`
- `gpu_samples/<gpu_run_id>.csv`, the raw `nvidia-smi` samples for one step
The transcript pipeline also stores each Part 1 call's numbers in the meeting's `*.meta.json`, under `usage`.
Set `LLM_USAGE_LOG=0` to turn recording off. A recording failure is logged as a warning and never stops a run.
## Labelling calls
A caller labels the calls it makes with `llm_call_context`. Only `stage`, `video_id`, `jurisdiction_id` and
`api_mode` are kept, so a prompt or transcript cannot reach the log by accident.
```python
from llm.usage.calls import llm_call_context
with llm_call_context(stage="part1", video_id=video_id, jurisdiction_id=jurisdiction_id):
result = call_gemini_text(...)
result.usage.thinking_token_count
```
A GPU step is wrapped the same way. On a machine with no `nvidia-smi`, or with `device="cpu"`, it records nothing.
```python
from llm.usage.gpu import gpu_run
with gpu_run("whisperx_diarization", device=args.device, video_id=video_id):
diarize_audio_whisperx(...)
```
## Reading the numbers
```bash
python -m llm.usage.report # by day, model and API mode
python -m llm.usage.report --since 2026-11-01 --by stage
python -m llm.usage.report --json
```
To query them in Postgres, load the logs. The loader applies migration `111_create_bronze_llm_usage.sql` if the
tables are missing, and a re-run adds only new rows.
```bash
python -m llm.usage.load_bronze --dry-run
python -m llm.usage.load_bronze
```
| Table | Grain | Primary key |
|---|---|---|
| `bronze.bronze_llm_call` | one model call | `llm_call_id` |
| `bronze.bronze_gpu_run` | one GPU step | `gpu_run_id` |
## Cost
Cost is not stored. The report multiplies the token counts and GPU-hours by the prices in
`packages/llm/src/llm/usage/prices.yaml`, so a corrected price reprices all history.
A price is used only when it has a value, a `source` and a `read_date`. Anything else shows as a dash. The Gemini
rates, interactive and batch, come from Google's Gemini API pricing page (paid tier, text rates, read
November 14, 2026; #266). A rate can end: `valid_until` is the last day it applies, and a call after that day shows
no cost until the new rate is added (Google doubles the 3.6 and 3.8 Flash rates on January 1, 2027; #333). Gemma 4
has no paid price (Google offers it on the free tier only). DeepSeek prices by time of day, so its entries
(`deepseek-flash`, `deepseek-v4-pro`, and the legacy name `deepseek-v4-flash`) carry `peak` hours and an `off_peak`
rate. A call is priced at the rate for its UTC time, and a call with no time of day shows no cost. Peak is 01:00 to
04:00 and 06:00 to 10:00 UTC on weekdays. DeepSeek also makes Chinese public holidays off-peak; those dates aren't
recorded, so on them the estimate errs high (read November 14, 2026; #328). `deepseek-chat`, the name used before,
is no longer listed, so its calls show no cost. Google has deprecated `gemini-2.5-flash`: from December 1, 2026 its calls are billed as
`gemini-3.5-flash-lite` (#329). The GPU prices are estimates of what the GPU time would rent for on a cloud (a
Colab or Kaggle GPU is paid in compute units or not at all).
## Decision: the same method as health-transformation-lab-nlp
The method follows the health-transformation-lab-nlp project, so the two projects' numbers can be read the same
way: token counts taken from the API's own response, an `nvidia-smi` loop sampled to a CSV, GPU-hours as session
time by GPU count, and a prices file where every price carries its source and the date it was read.
Two things differ, on purpose:
- **Where the rows go.** That project puts token counts on OpenTelemetry spans. Here the pipelines are standalone
package CLIs, which this repository logs with loguru (`CLAUDE.md`), so the rows go to JSON-lines files and a bronze
table. OpenTelemetry stays for the FastAPI service.
- **Cost is derived, not stored.** That project stores an estimated cost on each run. Here the report computes it,
so no row holds a cost computed from a price that was later corrected.
## The governance pipeline
The governance (Colab) pipeline calls the Gemini API directly in `gatekeeper_triage.py` and
`governance_meeting_llm.py`, through its own retry wrapper (`governance/genai_quota_retry.py`). Given a `model`,
that wrapper writes the same usage row (#268). Each call is labelled by stage: `gatekeeper`, `shield`, or the demo
that made it (`demo1` to `demo4`, set by the demo runners in `colab_demos.py`), else `governance`.
## In dbt
`bronze.bronze_llm_call` and `bronze.bronze_gpu_run` are dbt sources (#268):
| Model | Grain | Key |
|---|---|---|
| `stg_llm__call` (view) | one model call | `llm_call_id` |
| `stg_llm__gpu_run` (view) | one GPU step | `gpu_run_id` |
| `llm_usage_daily` (gold, contract enforced) | one day (UTC), model and API mode | (`usage_date`, `model`, `api_mode`) |
Both staging views build empty until `python -m llm.usage.load_bronze` creates the bronze tables, so `dbt build`
passes before any usage is loaded. `llm_usage_daily` has no cost column, for the reason above, and is not published
to `public`.
## DeepSeek thinking mode (#866)
DeepSeek turns thinking on by default, at effort `high`, and bills the thinking tokens with the output. The
DeepSeek call in `llm.gemini.genai_text_client` sent no thinking setting until #866, so every light analysis and
report call paid for it. The usage report for November 23 and 24, 2026 (`deepseek-flash`, stages `part1` and `part2`):
| | Tokens |
|---|---|
| Input | 52,162,224 (36,665,650 of them cache hits) |
| Output | 5,036,514 |
| Thinking | 22,836,836 |
Thinking was 82% of the tokens billed at the output rate. The report priced those two days at 24.01 USD.
Since #866 the call sends thinking **off**. `DEEPSEEK_THINKING` changes it:
| Value | Request |
|---|---|
| `off` (default) | `extra_body={"thinking": {"type": "disabled"}}` |
| `low`, `high`, `max` | `extra_body={"thinking": {"type": "enabled"}}` and `reasoning_effort` set to the value |
Any other value fails before a request is made. The report still shows a thinking column, so the effect of the
setting can be read from the next run: with `off` the column should be empty or zero for DeepSeek.
What this does not settle: whether answers are as good without thinking. The Gemini path ran these stages with
thinking off, but DeepSeek without thinking has not been compared on our prompts. Check a sample with
`python -m llm.gemini.public_comment_checks` before a large run, and set `DEEPSEEK_THINKING=low` if quotes stop
being found.
|