agent_metering
Language-agnostic LLM cost metering for B2B SaaS — point any OpenAI / Anthropic client at the HTTP proxy (Node, Go, Java, PHP, Python, curl, …).
MIT open source — CONTRIBUTING · SECURITY · Code of Conduct
Install
pip install llm-agent-metering
# optional extras:
# pip install "llm-agent-metering[dashboard]"
# pip install "llm-agent-metering[example]"
From GitHub (latest main):
pip install "git+https://github.com/prantakhandaker/agent_metering.git"
Any language (recommended)
Run the proxy once, then set your SDK base URL to it. No SDK install in the app language required.
pip install llm-agent-metering
python -m uvicorn agent_metering.proxy:app --host 0.0.0.0 --port 8787
| Provider | Base URL / env |
|---|---|
| OpenAI | http://127.0.0.1:8787/proxy/openai/v1 → OPENAI_BASE_URL |
| Anthropic | http://127.0.0.1:8787/proxy/anthropic → ANTHROPIC_BASE_URL |
| Azure | .../proxy/azure/v1 → AZURE_OPENAI_BASE_URL |
Optional per-user / feature headers (stripped before upstream):
X-User-Id(preferred) orX-Customer-IdX-Feature- Or OpenAI body field
user/ Anthropicmetadata.user_id
Defaults: env AGENT_METERING_CUSTOMER_ID / AGENT_METERING_FEATURE, else default.
Spend → local SQLite agent_metering.db. Dashboard:
python -m streamlit run examples/dashboard.py # pip install "llm-agent-metering[dashboard]"
Node
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: "http://127.0.0.1:8787/proxy/openai/v1",
defaultHeaders: { "X-User-Id": "user_42" },
});
await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hi" }],
});
Full script: examples/proxy_node_example.mjs.
curl
curl http://127.0.0.1:8787/proxy/openai/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-H "X-User-Id: user_42" \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hi"}]}'
Go
client := openai.NewClient(
option.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
option.WithBaseURL("http://127.0.0.1:8787/proxy/openai/v1"),
option.WithHeader("X-User-Id", "user_42"),
)
Docker sidecar
docker compose -f examples/docker-compose.sidecar.yml up --build
App containers only need OPENAI_BASE_URL / ANTHROPIC_BASE_URL pointing at http://metering-proxy:8787/proxy/....
Or wrap a local process:
python -m agent_metering run --start-proxy -- python your_app.py
Python-only shortcut (optional)
Same venv install auto-patches OpenAI / Anthropic SDKs (no base URL change):
pip install llm-agent-metering
# run your Python app — no import required
Opt out: AGENT_METERING_AUTO=0. Demo: python examples/auto_instrument_example.py.
Also detects FastAPI/Flask/Django request users and OpenAI user= when present.
Who this is for / not for
For: Any stack that can set an LLM HTTP base URL (or env) and needs per-customer / per-user / per-feature spend.
Not for: Full tracing/evals (Langfuse), or replacing multi-provider gateways you already run (LiteLLM / Portkey) unless you put this proxy in front.
Why
Flat API rate limits do not protect margin. Agent workloads are open-ended: tool loops and long contexts can burn tokens quietly. Metering per customer/feature surfaces that before margin disappears.
Project layout
Primary (any language): proxy.py, providers/, cli.py
Python convenience: autoload.py (.pth), instrument.py, user_detect.py, frameworks.py
Shared: config.py, core.py, storage.py, context.py
Tests
pytest
Releasing
Maintainers publish to PyPI via GitHub Actions Trusted Publishing (no API token in secrets).
- One-time on pypi.org: Publishing → Pending publisher
- Project:
llm-agent-metering - Owner:
prantakhandaker - Repository:
agent_metering - Workflow:
publish.yml - Environment: leave empty
- Project:
- Bump
versioninpyproject.tomlto match the release tag. - Tag and release:
# version in pyproject.toml must match the tag
git tag v0.3.1
git push origin v0.3.1
# then GitHub → Releases → Draft release from that tag → Publish
Publishing workflow: .github/workflows/publish.yml.
License
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_agent_metering-0.3.1.tar.gz.
File metadata
- Download URL: llm_agent_metering-0.3.1.tar.gz
- Upload date:
- Size: 35.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7942b41a2a706d1ce09ea977f148c0ee20e1bedbd6fbff64056f45d42ab29a88
|
|
| MD5 |
cccd117a332ace1e38cd9b3f99d31c88
|
|
| BLAKE2b-256 |
d7c2ec47d7e509439a0ac176e800bd9393c32e1bc620f5130314771e55e3a492
|
Provenance
The following attestation bundles were made for llm_agent_metering-0.3.1.tar.gz:
Publisher:
publish.yml on prantakhandaker/agent_metering
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_agent_metering-0.3.1.tar.gz -
Subject digest:
7942b41a2a706d1ce09ea977f148c0ee20e1bedbd6fbff64056f45d42ab29a88 - Sigstore transparency entry: 2749933313
- Sigstore integration time:
-
Permalink:
prantakhandaker/agent_metering@855b21f0fd7738e25f8dada3b88e00bbe0930067 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/prantakhandaker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@855b21f0fd7738e25f8dada3b88e00bbe0930067 -
Trigger Event:
release
-
Statement type:
File details
Details for the file llm_agent_metering-0.3.1-py3-none-any.whl.
File metadata
- Download URL: llm_agent_metering-0.3.1-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0be9a011ef11f510d483deec8fc78ef1cd74ba33b300ad6419696ddd92f0050
|
|
| MD5 |
611b51cbe6227988a46a4de52effc7aa
|
|
| BLAKE2b-256 |
e7334ca2b010f74d5a2ee24677dee3774f9f212527cdcd6e19596783bf8a5288
|
Provenance
The following attestation bundles were made for llm_agent_metering-0.3.1-py3-none-any.whl:
Publisher:
publish.yml on prantakhandaker/agent_metering
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_agent_metering-0.3.1-py3-none-any.whl -
Subject digest:
a0be9a011ef11f510d483deec8fc78ef1cd74ba33b300ad6419696ddd92f0050 - Sigstore transparency entry: 2749933364
- Sigstore integration time:
-
Permalink:
prantakhandaker/agent_metering@855b21f0fd7738e25f8dada3b88e00bbe0930067 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/prantakhandaker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@855b21f0fd7738e25f8dada3b88e00bbe0930067 -
Trigger Event:
release
-
Statement type: