Skip to main content

llm-meter 📊

PyPI Version Python Version License

Accurate LLM usage & cost tracking for Python backends.

llm-meter solves the "black box" of LLM costs by providing framework-native (FastAPI) instrumentation that attributes every token, cent, and millisecond to your business-level concepts (User ID, Feature, Endpoint).


⚡ 15-Minute Setup

# Using uv (recommended)
uv add llm-meter

# or pip
pip install llm-meter

1. Initialize & Instrument

from fastapi import FastAPI
from llm_meter import LLMMeter, FastAPIMiddleware
from openai import OpenAI

# 1. Initialize SDK
meter = LLMMeter(
    storage_url="sqlite+aiosqlite:///llm_usage.db",
    providers={"openai": {"api_key": "YOUR_KEY"}}
)

app = FastAPI()

# 2. Add Middleton for automatic attribution
app.add_middleware(FastAPIMiddleware, meter=meter)

# 3. Wrap your client
client = meter.wrap_client(OpenAI())

@app.post("/generate")
async def generate(prompt: str):
    # This call is automatically tracked and attributed to "/generate"
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    return {"text": response.choices[0].message.content}

2. Inspect via CLI

# Get a high-level summary
llm-meter usage summary

# See which endpoint costs the most
llm-meter usage by-endpoint

🎯 Key Features

  • Accounting, not Observability: Focuses on cost attribution and usage tracking, not heavy traces or prompt logging.
  • FastAPI Native: Middleware handles request_id and context propagation automatically.
  • Async-Safe: Powered by contextvars to ensure usage is correctly attributed even in complex async workflows.
  • Proxy-Free: Works via SDK-level instrumentation (no network interception or latency overhead).
  • Self-Hosted: You own your data. Supports SQLite (default) and PostgreSQL.

⚠️ v1 Limitations

  • Supports OpenAI and Azure OpenAI.
  • Batch token tracking (Streaming support coming in v1.1).
  • No web UI (everything is available via CLI or SQL).

🧪 Testing, Linting & Type Checks

To run all tests and check coverage:

uv run pytest --cov=llm_meter --cov-report=term-missing

To run linting and formatting checks with Ruff:

uv run ruff check . --fix
uv run ruff format .

To run type checking with Pyright:

uv run pyright

To run all pre-commit hooks manually:

uv run pre-commit run --all-files

These commands help ensure code quality, style, and type safety before committing changes.

🛠 Contributing

We love contributions! Please see CONTRIBUTING.md for details on how to get started.


📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for llm-meter 0.3.4

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

Source distribution (sdist)

Source distribution for llm-meter 0.3.4
File Size Uploaded Provenance
llm_meter-0.3.4.tar.gz 140.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-meter 0.3.4
File Interpreter ABI Platform
llm_meter-0.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 170.2 kB

Release files / llm_meter-0.3.4.tar.gz

Download URL llm_meter-0.3.4.tar.gz
Size 140.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fa13a724b6ec27504138f8ee0b46736e596e326a2471b2bebca1e9236217393e
BLAKE2b-256 checksum
How to use checksums
e45a06e391c26ca20d23fcf0d3cc3dd563efef8972ace8eabc9decd7b1967f2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / llm_meter-0.3.4-py3-none-any.whl

Download URL llm_meter-0.3.4-py3-none-any.whl
Size 30.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26ac2dd7c101cc6579e668dd537b6be6b20f74127a79b10e93d42fc2046d8737
BLAKE2b-256 checksum
How to use checksums
b647c19ce14ba66c83249fdb5671ddce296a9e6cfb410b2d76186f2514118365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for llm-meter 0.3.4

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.

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