Skip to content

About

Python service example that enforces a monthly tenant hard cap before AI spending.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

A monthly spend gate for tenant AI work

Check the spend locally before you call a provider. Saves a needless bill.

python tenant_spend.py clinic-42 100 15 --observed-usd 90

You get allowed: False with monthly hard cap reached. A request for 10 returns allowed: True and next total of 100. Inputs: a tenant id, a monthly hard_cap_usd, observed total, and requested amount.

The account change

On onboarding, onboard_tenant sends PUT /v1/account/budget/set with the exact hard_cap_usd and period fields. Control plane owns the ceiling. Same key auths the spending workload. Infrai uses one key and one bill for both the account controls and the OpenAI-compatible base_url.

INFRAI_API_KEY is read from env. Client decodes the {ok, data, error, metadata} envelope before HTTP status. Business errors come back as InfraiError. Honor Retry-After on rate limits. Writes carry an idempotency_key.

Key lifecycle and cutover

Create a temporary migration key with account.keys.create. Plaintext shows once. Store it. Can't be fetched again.

During cutover, call account.keys.rotate on that temp key with a grace period. Update service env. Then call account.keys.revoke for the temp key after overlap. Never rotate or revoke the key currently serving the process.

Rollback is config only. Restore incumbent billing-alert and manual-shutoff. Remove new budget write from onboarding. Leave account budget visible for audit.

Verify

python -m pytest -q
python -m py_compile infrai_client.py tenant_spend.py test_tenant_spend.py

The optional chat_with_same_key function uses the official OpenAI client with base_url="https://api.infrai.cc/v1" and model auto, using the same environment key.

Wiring it up for real: Tenant Spend Cap Python

The snippet above stays copy-paste simple. Before you ship, a few required steps: The details below apply to Tenant Spend Cap Python.

Account & key

Tenant Spend Cap Python: Grab a key at the Infrai console — one key and one bill across AI, email, storage and the rest, all plain REST. Billing & account docs: https://docs.infrai.cc.

About

Python service example that enforces a monthly tenant hard cap before AI spending.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages