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.3.tar.gz (91.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.3-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cost_monitor-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e76ffc3f1e76109bd01c8f7d83eac76a443ad735cf64bbc15dd5c3f20158524b
MD5 6f9682f40ebf581d8d8cb46b4a479b11
BLAKE2b-256 f1343ffff43d133c05091d57baa9c189a818055f78d6a78956b0c240fb792558

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cost_monitor-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e2d109e609ddb3a33b15c1b8947c7b07e8e39e3594cf7686486bdf3cd6e64c10
MD5 35fc85846f38723dcac3d0244189801a
BLAKE2b-256 e96a1899901b7cc97c6ba2e6eb2488691775c22d8f4749b9d12627f978fc040b

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