Create a Balance
Copy
from limitry import Limitry
client = Limitry()
balance = client.balances.create(
customer_id="cust_123",
name="AI Credits",
unit="credits",
initial_balance=10000,
minimum_balance=0 # Can't go below 0
)
print(f"Created balance: {balance.id}")
print(f"Available: {balance.available_balance} {balance.unit}")
Balance Options
Minimum Balance
Copy
# No overdraft allowed (default)
balance = client.balances.create(
customer_id="cust_123",
name="Credits",
unit="credits",
initial_balance=1000,
minimum_balance=0
)
# available_balance = 1000
# Reserve some credits
balance = client.balances.create(
customer_id="cust_123",
name="Credits",
unit="credits",
initial_balance=1000,
minimum_balance=100 # Keep 100 in reserve
)
# available_balance = 900
# Allow overdraft
balance = client.balances.create(
customer_id="cust_123",
name="Credits",
unit="credits",
initial_balance=1000,
minimum_balance=-500 # Allow 500 debt
)
# available_balance = 1500
Check Balance Sufficiency
Check if a customer has sufficient balance before an operation:Copy
result = client.balances.check_sufficiency(
customer_id="cust_123",
name="credits",
amount=500
)
print(f"Sufficient: {result.sufficient}")
print(f"Available: {result.available_balance}")
print(f"Current: {result.current_balance}")
if not result.sufficient:
shortfall = result.requested_amount - result.available_balance
print(f"Need {shortfall} more credits")
Credit (Add Funds)
Copy
result = client.balances.credit(
customer_id="cust_123",
name="credits",
amount=5000,
description="Monthly top-up",
reference="stripe_pi_123" # Optional: link to payment
)
print(f"Added: {result.transaction.amount}")
print(f"New balance: {result.balance.current_balance}")
Debit (Spend Credits)
Copy
result = client.balances.debit(
customer_id="cust_123",
name="credits",
amount=100,
description="GPT-4 completion",
reference="evt_xyz789" # Optional: link to event
)
if result.success:
print(f"Spent: {result.transaction.amount}")
print(f"Remaining: {result.balance.current_balance}")
else:
print(f"Failed: {result.error}")
Debits are atomic — if there aren’t enough credits, the entire operation fails. No partial debits.
Get a Balance
Copy
balance = client.balances.retrieve("bal_abc123")
print(f"Current balance: {balance.current_balance}")
print(f"Minimum balance: {balance.minimum_balance}")
print(f"Unit: {balance.unit}")
List Balances
Copy
# List all balances for a customer
balances = client.balances.list(customer_id="cust_123")
for bal in balances.data:
print(f"{bal.name}: {bal.available_balance} {bal.unit}")
# List all balances
balances = client.balances.list()
# Pagination
balances = client.balances.list(limit=50)
if balances.has_more:
next_page = client.balances.list(cursor=balances.next_cursor)
Transaction History
Copy
transactions = client.balances.list_transactions(
balance_id="bal_abc123",
limit=50
)
for txn in transactions.data:
sign = "+" if txn.type == "credit" else "-"
print(f"{txn.created_at}: {sign}{txn.amount} {txn.description}")
print(f" Balance after: {txn.balance_after}")
if txn.reference:
print(f" Reference: {txn.reference}")
# Filter by type
credits_only = client.balances.list_transactions(
balance_id="bal_abc123",
type="credit"
)
debits_only = client.balances.list_transactions(
balance_id="bal_abc123",
type="debit"
)
Update a Balance
Copy
balance = client.balances.update(
"bal_abc123",
name="Updated balance name",
minimum_balance=50 # Change reserve amount
)
Delete a Balance
Copy
result = client.balances.delete("bal_abc123")
if result.success:
print("Balance deleted")
Deleting a balance is permanent. All transaction history will be lost.
Example: Credit-Based Usage
Copy
def process_request(customer_id: str, balance_id: str, prompt: str):
# 1. Check balance
balance = client.balances.retrieve(balance_id)
estimated_cost = len(prompt) // 4 # Rough token estimate
if balance.available_balance < estimated_cost:
raise Exception("Insufficient credits")
# 2. Make the LLM call
response = openai.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
# 3. Calculate actual cost
actual_cost = response.usage.total_tokens
# 4. Debit the balance
client.balances.debit(
customer_id=customer_id,
name="credits",
amount=actual_cost,
description=f"GPT-4: {actual_cost} tokens"
)
# 5. Record event for analytics
client.events.record(
customer_id=customer_id,
event_type="llm.completion",
values={"tokens": actual_cost},
dimensions={"model": "gpt-4"}
)
return response.choices[0].message.content