Skip to main content

marginal-sdk

Marginal SDK for Python — send AI cost events and see where your AI spend goes, sliced by customer, feature, environment, model, or any field you define.

pip install marginal-sdk

Python 3.9+, stdlib only (zero dependencies).

Quickstart

One call per LLM request: name the provider, paste the response's model and usage — Marginal detects the provider's usage shape and computes the cost server-side against its model price catalog.

import os
from marginal import Marginal

marginal = Marginal(api_key=os.environ["MARGINAL_API_KEY"])

response = client.chat.completions.create(...)

marginal.track(
    provider="openai",
    model=response.model,
    usage=response.usage.model_dump(),
    fields={"customer": "acme-corp", "feature": "support-bot"},
)

For non-LLM spend (voice, images, pre-computed costs), send dollars directly:

marginal.track(cost=0.05, fields={"customer": "acme-corp"})

track() is synchronous and never raises: events are buffered and flushed by a daemon worker thread (every 5 s or at 100 events), with retries on network errors, 429s, and 5xx. Errors are reported to an on_error callback (a warning print by default). Buffered events are flushed automatically at interpreter exit; call marginal.flush() to force one earlier. Fork-safe: the worker restarts in the child after os.fork().

Options

Marginal(
    api_key="mgl_...",         # required — server-side only
    on_error=lambda err: ...,  # delivery/validation errors (never raised)
    base_url="https://api.marginalhq.com",  # default
)

Docs

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marginal_sdk-0.1.1.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

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

marginal_sdk-0.1.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file marginal_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: marginal_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for marginal_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1cf6b340dc46c17ec16ec947134c4765c25413feffc00aa913bf269208983626
MD5 ae337b80799492fdb3b7d5f5366c6678
BLAKE2b-256 b646d62afb3a03a8d824502b0f6574ffa4fd91ae92b81b75f2a76310b8215185

See more details on using hashes here.

File details

Details for the file marginal_sdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: marginal_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for marginal_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2b00c21e178b5a8948e23001bc9cc3b4df51eb776d4c86af62e55ba6db97514d
MD5 c3d524c00f2308a0d90d9c15e2c65adf
BLAKE2b-256 919aabc1d266889fe3ed9d5618e6325d9ddacf8fa95cfcf5e437f39c652d751d

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 Sentry Error logging StatusPage Status page