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.0.tar.gz (7.6 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.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for marginal_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7c2490469ebd2f965d7c58e6aba204bf63f63dee5e59247acaaa461fec6188b0
MD5 5e444517e328fb23b49bb900a3201775
BLAKE2b-256 b14a590d5f0f6cc5bd3282713130985c69be5d3a7daf869bc57174e2690d5c13

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for marginal_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 125b885866a4cc49d5168622533df14b19e86f59508a6666ba53e9564e972784
MD5 cd7fd147450f4cd46c9e466bd20b2306
BLAKE2b-256 1434ab3c9bf504a5fd8de5d7d6f2606aedab77b114ee17d8d2a0222a9d546641

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