Skip to main content

Python API

ducto for Python is published as the ducto package on PyPI. Requires Python 3.11+.

Installation

pip install ducto

# With store backends
pip install "ducto[supabase]"
pip install "ducto[postgres]"
pip install "ducto[test]" # dev + testing

Core Classes

ClassDescription
PricingEnginePure calculation layer — evaluates expressions against usage metrics
CreditManagerOrchestrates calculate → reserve → deduct lifecycle
MemoryStoreIn-memory store for testing
HttpxSupabaseStoreSupabase-backed store (fetch-based)
PostgresStoreDirect PostgreSQL store
UsageMetricsData class for usage dimensions
CostBreakdownStructured calculation result

Quick Example

from ducto import CreditManager, UsageMetrics
from ducto.interface.supabase import HttpxSupabaseStore

store = HttpxSupabaseStore(url="https://project.supabase.co", key="service_role_key")
manager = CreditManager(store=store)
manager.load_pricing_from_store()

manager.add_credits("user_abc", 1000)

result = manager.deduct(
user_id="user_abc",
metrics=UsageMetrics(model="gpt-4", input_tokens=500, output_tokens=200),
idempotency_key="tx_001",
)
print(result.balance_after) # remaining credits