Skip to main content

ollama-usage

ollama-usage · Log, budget, and visualise token usage for local Ollama models.


Status License Org


Overview

Ollama's API already returns prompt/output token counts and timings on every response — this project makes sure that data lands somewhere instead of vanishing after each request, warns you before it collides with your context window, and gives you a network-reachable dashboard to actually look at it.

Companion code for fynesforge.dev/blog — see the post for the full write-up.

This is a Fynes Forge project built with precision over cleverness.


Getting Started

Clone and run:

git clone https://github.com/fynes-forge/ollama-usage.git
cd ollama-usage
uv sync

Or install a released version directly, no clone needed — every release has a wheel attached:

uv tool install ollama-usage --from https://github.com/fynes-forge/ollama-usage/releases/download/v0.1.0/ollama_usage-0.1.0-py3-none-any.whl

uv tool install puts the ollama-usage command on your PATH in its own isolated environment — no virtualenv to manage, and it doesn't touch any other project's dependencies. Swap uv tool install for pip install if you'd rather it land in your current environment. Check the release's SHA256SUMS.txt if you want to verify the download.

Development

Common dev commands are wrapped in a Makefile — run make help to list them:

make sync       # install everything, runtime + dev deps
make check      # lint, format-check, typecheck, test — same as CI
make test-cov   # tests with a coverage report
make dashboard
make proxy

These are just short aliases for the equivalent uv run ... commands shown throughout this README — use whichever you prefer.

Use

Log a call and check it against your context budget:

uv run ollama-usage log \
  --model qwen2.5-coder-agent \
  --prompt "Summarise breaking changes in go_router 14 to 17." \
  --tag dependabot-review

Every call appends one line to ~/ollama-usage.jsonl. If the prompt is eating into your model's context window, you'll see a warning before Ollama silently starts dropping tokens:

⚠️  Context at 91% of budget (29820/32768 tokens)

Adjust --num-ctx to match whatever num_ctx your Modelfile sets.

Tracking Cline (or any client that talks to Ollama directly):

ollama-usage log only logs calls it makes itself. Cline talks to Ollama's API on its own — it never goes through this tool — so nothing above captures it. Fix that with the proxy:

uv run ollama-usage proxy

This starts a transparent proxy on :11435 that forwards every request to your real Ollama server and logs the token usage on the way through, without changing the response. Point Cline's Ollama Base URL setting at http://localhost:11435 instead of http://localhost:11434, and its traffic gets logged like everything else.

Two things worth knowing before you rely on this:

  • Every request through one proxy instance shares one --tag (cline by default), since the proxy has no way to know what task Cline is actually working on. If you want that level of granularity, restart the proxy with a different --tag per work session.
  • Only traffic that's actually routed through the proxy gets logged. Anything still pointed at :11434 directly — Jan, a curl command, another tool — stays invisible to this log.

Launch the dashboard:

uv run ollama-usage dashboard

Starts a Streamlit app bound to 0.0.0.0:8501 by default — reachable from any device on your network, not just the machine running it. Pass --host 127.0.0.1 to keep it local-only, or --port to change the port.

The dashboard shows total requests, total tokens, average tokens/sec, a breakdown by tag, tokens/sec over time, and requests per day.

Or work from the terminal instead:

uv run ollama-usage report   # per-tag summary table
uv run ollama-usage plot     # tokens/sec over time, matplotlib window

Use it as a library

from ollama_usage.logger import call_and_log
from ollama_usage.budget import check_context_budget

resp = call_and_log(model="qwen2.5-coder-agent", prompt=my_prompt, tag="my-task")
check_context_budget(resp["prompt_eval_count"], num_ctx=32768)

Documentation

Document Description
CONTRIBUTING.md How to contribute to this project
CHANGELOG.md Version history and release notes
AGENTS.md AI agent context and conventions

Project Structure

ollama-usage/
├── .github/
│   ├── workflows/          ← CI/CD pipelines
│   ├── ISSUE_TEMPLATE/     ← Bug reports, feature requests
│   ├── PULL_REQUEST_TEMPLATE/
│   └── copilot/            ← GitHub Copilot instructions
├── docs/                   ← Documentation
src/ollama_usage/
    |
    ├── config/
        ├── __init__.py
        ├── branding.py    # shared brand colour tokens
        └── config.py      # load_config() — read env vars into a dict
    ├── __init__.py
    ├── logger.py      # call_and_log() — call Ollama, append usage to JSONL
    ├── budget.py      # check_context_budget() — warn before context overflow
    ├── proxy.py       # transparent proxy — captures Cline/other direct clients
    ├── report.py      # summary() and plot() — query the log with DuckDB
    ├── dashboard.py   # Streamlit dashboard
    └── cli.py         # `ollama-usage log|report|plot|dashboard|proxy`
├── tests/                  ← Test suite
├── AGENTS.md               ← AI agent conventions
├── CONTRIBUTING.md         ← Contribution guide
├── CHANGELOG.md            ← Release history
└── README.md               ← This file

Contributing

Contributions are welcome. Please read CONTRIBUTING.md before opening a PR.


Licence

MIT © Fynes Forge — see LICENSE for details.

Metadata

Release files for ollama-usage 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ollama-usage 1.0.1
File Size Uploaded Provenance
ollama_usage-1.0.1.tar.gz 13.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ollama-usage 1.0.1
File Interpreter ABI Platform
ollama_usage-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 30.6 kB

Release files / ollama_usage-1.0.1.tar.gz

Download URL ollama_usage-1.0.1.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1c8bd9a747334fb37734bca7faa8173e440cea1629bd33ebcabc2f8ce7449a0b
BLAKE2b-256 checksum
How to use checksums
47079ada094682cd856f1bf9a87302c98ddb6bea39466b9c25b121ef44b05115
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / ollama_usage-1.0.1-py3-none-any.whl

Download URL ollama_usage-1.0.1-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73a8d286b7dc76cd6a4fa948dc2b9ea065bb04f80fe1d068c3d8e6ecd0906d18
BLAKE2b-256 checksum
How to use checksums
c8bfc790cc062e70ed7667ebd66294a501204312ef78097af6334c01619d3e9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for ollama-usage 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page