Skip to main content

Accurate LLM usage & cost tracking for Python backends (FastAPI-native)

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_meter-0.3.4.tar.gz (140.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_meter-0.3.4-py3-none-any.whl (30.0 kB view details)

Uploaded Python 3

File details

Details for the file llm_meter-0.3.4.tar.gz.

File metadata

  • Download URL: llm_meter-0.3.4.tar.gz
  • Upload date:
  • Size: 140.3 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for llm_meter-0.3.4.tar.gz
Algorithm Hash digest
SHA256 fa13a724b6ec27504138f8ee0b46736e596e326a2471b2bebca1e9236217393e
MD5 11d984bf170d49bc85783e2f8912be8c
BLAKE2b-256 e45a06e391c26ca20d23fcf0d3cc3dd563efef8972ace8eabc9decd7b1967f2a

See more details on using hashes here.

File details

Details for the file llm_meter-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: llm_meter-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 30.0 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for llm_meter-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 26ac2dd7c101cc6579e668dd537b6be6b20f74127a79b10e93d42fc2046d8737
MD5 0348b18f770778d74e83819f5d27a0d3
BLAKE2b-256 b647c19ce14ba66c83249fdb5671ddce296a9e6cfb410b2d76186f2514118365

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page