The platform for continuously improving AI agents.
import logging
from decimal import Decimal
import httpx
from celery import shared_task
logger = logging.getLogger(__name__)
class NotPricedError(Exception):
"""The provider has not priced the generation yet."""
@shared_task(bind=True, name="overbae.tasks.billing.price_generation", max_retries=12)
def price_generation(self, cause, generation, model, cost, usage):
"""Charge an OpenRouter generation at its cost, looked up once the provider priced it."""
# Lazy: services import tasks.
from overbae.services import model_meter
from overbae.services.billing_ledger import Cause
try:
priced = Decimal(cost) if cost is not None else model_meter.generation_cost(generation)
if priced is None:
raise NotPricedError(generation)
except (NotPricedError, httpx.HTTPError) as exc:
if self.request.retries >= self.max_retries:
logger.error("OpenRouter generation was never priced: %s", generation)
return
raise self.retry(exc=exc, countdown=min(30 * 2**self.request.retries, 600)) from exc
model_meter.charge_generation(Cause(**cause), generation, priced, model, usage)
@shared_task(name="overbae.tasks.billing.collect_container_meters")
def collect_container_meters():
"""Charge the Modal containers that did customers' work since the last collection."""
# Lazy: services import tasks.
from overbae.services import container_meter
return {"charged": container_meter.collect()}
@shared_task(name="overbae.tasks.billing.check_provider_bills")
def check_provider_bills():
"""Compare the ledger with what Modal and OpenRouter billed this environment; read-only."""
# Lazy: services import tasks.
from overbae.services import provider_check
return {"results": provider_check.check()}