Skip to main content

Bursar Python SDK for AI credits and usage billing

PyPI PyPI downloads

Bursar logo

The Python SDK for Bursar. It meters usage, prices operations, and manages balances against the shared canonical PostgreSQL schema and the same versioned configuration document as the JavaScript SDK. Python 3.12 through 3.15 are supported.

Installation

python -m pip install "bursar[postgres]"

Extras: postgres (default recommended), stripe or dodo for that payment provider, s3 (optional billing archive), google-adk (model-call admission and settlement plugin), and test (dev/test tooling).

Apply the SQL baseline before starting an application:

export BURSAR_MIGRATION_DATABASE_URL=postgresql://bursar_migrator@db.example.com/bursar
bursar migrate

bursar migrate applies the ordered SQL files, records checksums, and is safe to re-run. Use a dedicated migration principal; applications connect with a separate least-privilege runtime principal. See the CLI guide for the separate migration, operator, and application credentials.

Usage

from decimal import Decimal

from bursar import Bursar, PostgresStore

store = PostgresStore(
    database_url,
    tenant_id=tenant_id,
    provider_environment="test",
)
bursar = Bursar(credit_store=store)

grant = bursar.credits.add_credits(
    user_id,
    Decimal("500"),
    entry_type="purchase",
    idempotency_key="checkout:42",
)
charge = bursar.credits.deduct_credits(
    user_id,
    Decimal("20"),
    idempotency_key="job:42",
)
refund = bursar.credits.refund_credits(charge.entry_id, idempotency_key="refund:job:42")

page = bursar.credits.list_ledger_entries(user_id, limit=25)
while page.next_cursor is not None:
    page = bursar.credits.list_ledger_entries(user_id, limit=25, cursor=page.next_cursor)

LedgerEntry, LedgerCursor, and LedgerPage are available from bursar.credits.types; pagination is cursor-only. PostgresStore is the production, tenant-scoped store; CreditStore is the abstract base for custom implementations.

Publish one versioned configuration document through the facade — billing and auto-recharge read the same active document:

bursar.catalog.publish_and_activate(config)

Optional S3 and ClickHouse storage

PostgreSQL remains authoritative. S3 and ClickHouse are optional delivery targets, managed by create_bursar_runtime from bursar.storage:

from bursar.storage import BursarRuntimeOptions, create_bursar_runtime

runtime = create_bursar_runtime(
    BursarRuntimeOptions(
        postgres=os.environ["DATABASE_URL"],
        operator_postgres=os.environ["BURSAR_OPERATOR_DATABASE_URL"],
        tenant_id=os.environ["BURSAR_TENANT_ID"],
        provider_environment="test",
    )
)
runtime.start()
bursar = runtime.bursar

With no S3/ClickHouse configuration the runtime creates no background worker and analytics query PostgreSQL directly. See the storage guide for the full S3 and ClickHouse setup.

Development

cd python
uv sync --group dev        # runtime + dev/test deps
uv run pytest              # full suite; integration tests need Postgres
uv run ruff check src/ tests/ scripts/
uv run pyright src/ --warnings

Real-Postgres tests resolve DATABASE_URL when BURSAR_ALLOW_DATABASE_RESET=1, else spin up a disposable PostgreSQL 17 + pg_partman 5 + pg_jsonschema 0.3 testcontainer. See CONTRIBUTING.md.

License

AGPL-3.0.

Metadata

Release files for bursar 2.1.1

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

Source distribution (sdist)

Source distribution for bursar 2.1.1
File Size Uploaded Provenance Actions
bursar-2.1.1.tar.gz 595.7 kB Without provenance Details for bursar-2.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bursar 2.1.1
File Interpreter ABI Platform Actions
bursar-2.1.1-py3-none-any.whl Python 3 none any Details for bursar-2.1.1-py3-none-any.whl

Total release size: 1.1 MB

Release files / bursar-2.1.1.tar.gz

Download URL bursar-2.1.1.tar.gz
Size 595.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1a01456d2a390eb744fac81b0edfd701808cb7b49556c7386022680e694bf9de
BLAKE2b-256 checksum
How to use checksums
55e23cbae20cf29acbddc78acba037b9b3c093ec9fed5e2786253b1f947a81e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / bursar-2.1.1-py3-none-any.whl

Download URL bursar-2.1.1-py3-none-any.whl
Size 484.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5eb116892d7cc4d66719b46a289a8658751a410d50db6515f31361b19848981b
BLAKE2b-256 checksum
How to use checksums
3fbcb5b30bfbbc7e612fabd3acfbe7998288a96201668dee1b92b2250329a75f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for bursar 2.1.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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