A tiny Python library for tracking LLM token usage, latency, and estimated cost.
Project description
llm-cost-profiler
A lightweight, framework-agnostic library for tracking LLM token usage, cost, and latency across OpenAI, LangChain, FastAPI, batch jobs, and any custom Python workflow.
It helps teams bring visibility, observability, and cost analytics into any LLM-powered system with almost zero code changes.
Overview
llm-cost-profiler is a small, dependency-light Python library for tracking LLM usage, latency and estimated cost across different model providers. It provides simple instrumentation primitives (manual recording and a decorator), JSONL logging for audit and analysis, and optional dynamic pricing lookups.
Perfect for developers building RAG systems, agents, batch inference jobs, Streamlit apps, FastAPI services, and more.
Features
- Token usage (prompt & completion) tracking
- Model-specific cost estimation
- Latency & response time tracking
- JSONL logging sink for audits & dashboards
- Simple decorator to profile function calls
- Dynamic pricing (optional) with safe fallbacks
- Minimal dependencies
- Zero dependencies on AI frameworks
- Works with OpenAI, LangChain, custom clients, batch jobs, APIs
- Built for production observability (text logs → data lakes)
Installation
Install from PyPI:
pip install llm-cost-profiler
## QUICK START
from llm_cost_profiler import CostProfiler, profile_llm_call
from openai import OpenAI
client = OpenAI(api_key="YOUR_KEY")
prof = CostProfiler("local_costs.jsonl")
@profile_llm_call(
prof,
model_getter=lambda a,k: k.get("model", "gpt-4o"),
token_getter=lambda r: (r["usage"]["prompt_tokens"], r["usage"]["completion_tokens"])
)
def ask(prompt, model="gpt-4o"):
return client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
ask("Explain LLM observability like I'm 5")
print(prof.report_summary())
## EXAMPLES
All runnable examples are inside the examples/ directory:
##Core Examples
example_basic_profiler.py – minimal fake call + cost
example_multiple_calls.py – tags and multiple calls
example_jsonl_logging.py – JSONL logs + reading back
example_model_price_override.py – override pricing table
## Integrations
openai_integration.py – real OpenAI client usage
langchain_integration.py – decorator wrapper over LangChain
fastapi_server.py – API endpoint that tracks cost per request
batch_job.py – batch summarization worker example
## Run any example with:
python examples/example_basic_profiler.py
## Development
Clone the repo and use a virtualenv:
python -m venv .venv
source .venv/bin/activate
pip install -e .[dev]
pytest -q
## How the decorator works
@profile_llm_call(
prof,
model_getter=lambda args, kwargs: kwargs.get("model"),
token_getter=lambda resp: (
resp["usage"]["prompt_tokens"],
resp["usage"]["completion_tokens"]
)
)
def llm_call(...):
...
##The decorator automatically captures:
# model name
# prompt & completion tokens
# execution time
# estimated cost
# any custom tags you pass
# and logs it as JSONL.
## Configuration / Env
Create a .env (never commit it). Example .env.example:
OPENAI_API_KEY=sk-REPLACE_ME
PRICING_API_KEY=replace-me-if-needed
##Contributing
Contributions welcome — open issues and PRs. Please follow the project's code style and tests.
##License
MIT © Bon
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_cost_profiler-0.1.3.tar.gz.
File metadata
- Download URL: llm_cost_profiler-0.1.3.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
758246a8dce46b31a5358922e762891b2c56e5975f3ec71e2c8e56e1a6e4e651
|
|
| MD5 |
33cdf0ea944c536920525743d3fa6a2c
|
|
| BLAKE2b-256 |
fae792198fcf0225ec45c45c2a2290a7746ae083f64d347ac6d2616df7a51e04
|
File details
Details for the file llm_cost_profiler-0.1.3-py3-none-any.whl.
File metadata
- Download URL: llm_cost_profiler-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c66168296f8abd35049973d775b355fdffda4c3282d67590189a377316e1c7c1
|
|
| MD5 |
a0b9321e6c792a7ea2dc034a52b8a04a
|
|
| BLAKE2b-256 |
6fd1419c4864de6ca6d127b42a85146bd86e261171590d34509d77b95cb7fecd
|