Skip to main content

Python client for cost-monitor: report LLM events and read back a project's events feed and spend metrics.

Project description

cost-monitor (Python)

Python client for cost-monitor — report LLM events and read back a project's events feed and spend metrics. A full-parity port of the TypeScript @cost-monitor/sdk: sync and async, batching with retry, and auto-instrumentation for OpenAI and Anthropic.

pip install cost-monitor
# with auto-instrumentation for your provider:
pip install "cost-monitor[openai]"      # or [anthropic]

Auto-tracking in 3 lines

Wrap your provider client; keep calling it exactly as before — every completion is metered to cost-monitor. The response (and stream) you get back is untouched; the server is the source of truth for cost.

from openai import OpenAI
from cost_monitor import wrap_openai

client = wrap_openai(
    OpenAI(),
    api_key="cm_live_...",                 # a cost-monitor PROJECT key
    base_url="https://your-cost-monitor",  # the API origin (host of /v1)
    default_feature="chat",                # optional default attribution
)

client.chat.completions.create(model="gpt-4o-mini", messages=[...])  # tracked

wrap_anthropic works the same for an Anthropic / AsyncAnthropic client.

Per-call attribution

Attribute a single call to a customer / feature / user with the reserved cost_monitor= keyword. It is always stripped before the provider call, so the provider never sees it. Per-call values override the wrapper defaults.

client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[...],
    cost_monitor={"customer_id": "cust_123", "feature": "summarize"},
)

Typing note: cost_monitor= is runtime-safe but is not part of the provider SDK's static signature, so a type checker will flag it as an unexpected keyword. For strictly-typed codebases, set attribution via the wrapper defaults (default_feature / default_customer_id / default_user_id), or report the call with a manual CostMonitorClient.track(...) instead.

Async

from openai import AsyncOpenAI
from cost_monitor import wrap_openai

client = wrap_openai(AsyncOpenAI(), api_key="cm_live_...", base_url="https://...")
await client.chat.completions.create(model="gpt-4o-mini", messages=[...])

Flushing before exit

A wrapper with an SDK-owned buffer exposes a control handle. Flush/close it on shutdown so the last batch is delivered (the buffer also flushes periodically):

client.cost_monitor.close()          # sync: final flush + stop the timer
await client.cost_monitor.aclose()   # async

Or pass your own buffer and own its lifecycle (mirrors the TS { sink } form):

from cost_monitor import CostMonitorClient, EventBuffer, wrap_openai

buffer = EventBuffer(CostMonitorClient(api_key="cm_live_...", base_url="https://..."))
client = wrap_openai(OpenAI(), sink=buffer, default_feature="chat")
# ... at shutdown:
buffer.close()

Pass exactly one of sink= or (api_key + base_url).

Manual reporting

No provider wrapper needed — report any event yourself (works for any LLM / raw HTTP):

from cost_monitor import CostMonitorClient

cm = CostMonitorClient(api_key="cm_live_...", base_url="https://...")
cm.track(
    provider="openai", model="gpt-4o-mini",
    input_tokens=130, output_tokens=39, status="success",
    feature="chat", customer_id="cust_123",
)

Read back a project's data: cm.get_events(), cm.get_metrics(period="30d"), cm.get_event(id), cm.get_margin_series(), and the export_*_csv() methods.

Reliability

  • Telemetry never breaks your app. Wrapper/buffer failures are swallowed and routed to an on_error callback; the provider call always completes.
  • Batching + retry. Events are sent in batches (default 50, or every 5s); transient failures (5xx / 429 / network) retry with exponential backoff; a 413 or 4xx is dropped, not retried.
  • Idempotent. Each event gets a stable client_event_id (uuid4) so a retry dedups server-side.

Requires Python 3.9+. openai (>=1.0.0) and anthropic (>=0.30.0) are optional extras — install only the one you use.

Released under the MIT license.

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

cost_monitor-0.1.0.tar.gz (90.3 kB view details)

Uploaded Source

Built Distribution

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

cost_monitor-0.1.0-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file cost_monitor-0.1.0.tar.gz.

File metadata

  • Download URL: cost_monitor-0.1.0.tar.gz
  • Upload date:
  • Size: 90.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for cost_monitor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 392a3039bd4bd7c76cbeab586f4cc2666955f29e8936033ae683a171935bdd5f
MD5 3ddfdb3877e5fb787e832908814bf0bd
BLAKE2b-256 7529f59ece197f126ef0911e6555818899b331cd6a85fcbc64497d48d0b5cb61

See more details on using hashes here.

File details

Details for the file cost_monitor-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cost_monitor-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for cost_monitor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7763e134ccb5c64b6e83af7f9e7f18bcc5518c72a4c2b62ae78b0edd283fc81b
MD5 92165954df55740a996a8b73020a19e2
BLAKE2b-256 de00d3d80038dd026e4e3aca61a72182b346911701e1596fea6789984d7c699a

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