Skip to main content

costlyinfra-meter

The optional metering hook for Meter — a thin, fail-safe wrapper that reports per-call LLM usage so spend can be attributed per feature. Stdlib-only, no dependencies. Cost is computed server-side from Meter's pricing tables — the SDK never sees prices, and it never sends prompt or response content, only token counts and a feature_id.

It is fail-safe: recording appends to an in-memory queue and returns. A single background worker batches and posts; nothing on your call path blocks, raises, or touches the network. If Meter is down, misconfigured, or asleep, your application is unaffected. With no ingest URL/token configured, every call is a no-op.

It is also bounded: one worker thread per meter whatever your traffic, and a capped queue (10,000 events). If the queue fills — a stalled endpoint, a burst — the oldest events are dropped and counted on meter.dropped. Metering degrades; your application does not.

Delivery and flush()

Events are sent when a batch fills (50) or after flush_interval seconds (5), whichever comes first. An atexit hook flushes on normal shutdown with a short deadline, so a script that exits immediately still delivers.

Call meter.flush() explicitly where the worker may not get scheduled — a serverless handler that freezes between invocations, or before a hard exit:

meter.flush()          # returns True if the queue drained, False on timeout
meter.flush(timeout=1) # never waits longer than you allow

Retries

A failed batch is retried (3 attempts, backing off ~1s / 4s / 15s with jitter) — enough to ride out a restart, a blip, or a sleeping instance waking up. A 4xx is not retried: a bad token or a malformed event fails the same way forever, so retrying only hammers the endpoint. 429 is retried, since it explicitly asks you to come back.

Retrying is safe because every attempt carries the same batch_id. The server applies a batch once and recognises re-deliveries, so a retry after a timeout you could not distinguish from a failure cannot double-charge a feature. Events only count as lost after the final attempt, and then meter.dropped says so.

Tunable per meter: batch_size, flush_interval, queue_max, timeout, max_attempts, retry_backoff.

Install

Into the virtualenv your application runs in:

python3 -m pip install "costlyinfra-meter>=1.0"

Or add costlyinfra-meter>=1.0 to requirements.txt / pyproject.toml, which is the version that survives a rebuild.

If pip answers error: externally-managed-environment, you are outside a virtualenv — activate your application's environment and run it again.

Use

Recommended — wrap the client once (no per-call code):

from costlyinfra_meter import wrap

client = wrap(anthropic_client, feature_id="feature-threat-triage")  # reads ENV

resp = client.messages.create(model="claude-sonnet-4-6", ...)   # metered automatically

Provider is auto-detected; each call is recorded with its latency. Pass an optional metadata={…} (e.g. environment, customer_id) for extra attribution. Streaming/async calls use the explicit form below.

Explicit — one line per call:

from costlyinfra_meter import Meter

meter = Meter(feature_id="feature-threat-triage")
resp = anthropic_client.messages.create(model="claude-sonnet-4-6", ...)
meter.record_anthropic(resp)   # <- the whole hook

Helpers: record_anthropic, record_openai, record_gemini, record_openai_compatible, or the generic record(provider=…, model=…, tokens_in=…, tokens_out=…, feature_id=…).

Optimize mode (opt-in)

Meter(..., optimize=True) additionally emits privacy-safe signals — salted hashes and counts, never prompt text — so Meter can surface measured optimization opportunities (duplicate calls, uncached repeated prefixes). Off by default; all work is off the call path, memory-bounded, and fail-safe. The SDK fetches a per-tenant salt once (GET /api/hook/salt, same ingest token).

Config

Env var What it is
METER_INGEST_URL e.g. https://app.example.com/api/hook/events
METER_INGEST_TOKEN the per-workspace ingest token from the dashboard

License

Apache-2.0. (The Meter server is AGPL-3.0; this client SDK is permissive so you can embed it in a proprietary app.)

Metadata

Release files for costlyinfra-meter 1.0.0

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

Source distribution (sdist)

Source distribution for costlyinfra-meter 1.0.0
File Size Uploaded Provenance Actions
costlyinfra_meter-1.0.0.tar.gz 23.9 kB With provenance Details for costlyinfra_meter-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for costlyinfra-meter 1.0.0
File Interpreter ABI Platform Actions
costlyinfra_meter-1.0.0-py3-none-any.whl Python 3 none any Details for costlyinfra_meter-1.0.0-py3-none-any.whl

Total release size: 41.4 kB

Release files / costlyinfra_meter-1.0.0.tar.gz

Download URL costlyinfra_meter-1.0.0.tar.gz
Size 23.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7f5eed03945f32529ff80bebb6841dad5147c9e3355d7e7a3d51d01aded1bfd1
BLAKE2b-256 checksum
How to use checksums
5badaa41e29a7aea2c2db68f56e4cf3296d6287b004716f4b298d7eb12f3865a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Release files / costlyinfra_meter-1.0.0-py3-none-any.whl

Download URL costlyinfra_meter-1.0.0-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8caa14abeeb72328c96cdac9fee88c78e2867373fbabd6df0dc8a54fc5ba8457
BLAKE2b-256 checksum
How to use checksums
f459341a6765b1a81ca19303209e2a410fbfe3fc838e5658d63d53141afbf4f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Security for costlyinfra-meter 1.0.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for costlyinfra_meter-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

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