Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
610 changes: 249 additions & 361 deletions src/api/routes/billing.py

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/api/routes/v2/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from src.api.dependencies import get_ingest_pipeline
from src.api.routes import memory as memory_v1
from src.billing.service import commit_job_billing, release_job_billing
from src.jobs.durable import get_default_job_store

try: # pragma: no cover - no-op fallback keeps imports working without SDK.
Expand Down Expand Up @@ -50,15 +51,22 @@ async def mark_job_progress_activity(payload: Dict[str, Any]) -> None:

@activity.defn
async def mark_job_succeeded_activity(payload: Dict[str, Any]) -> None:
job = await asyncio.to_thread(get_default_job_store().get, payload["job_id"])
result = payload.get("result") or {}
if job:
result = await asyncio.to_thread(commit_job_billing, job, result)
await asyncio.to_thread(
get_default_job_store().mark_succeeded,
payload["job_id"],
payload.get("result") or {},
result,
)


@activity.defn
async def mark_job_dead_letter_activity(payload: Dict[str, Any]) -> None:
job = await asyncio.to_thread(get_default_job_store().get, payload["job_id"])
if job:
await asyncio.to_thread(release_job_billing, job, "dead_letter")
await asyncio.to_thread(
get_default_job_store().mark_dead_letter,
payload["job_id"],
Expand Down
20 changes: 20 additions & 0 deletions src/api/routes/v2/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from src.api.routes.v2.temporal_client import cancel_job_workflow, start_job_workflow
from src.api.schemas import APIResponse
from src.billing.service import InsufficientCredits, get_default_billing_service, release_job_billing
from src.jobs.durable import DEAD_LETTER, QUEUED, RUNNING, get_default_job_store

router = APIRouter(
Expand Down Expand Up @@ -112,6 +113,24 @@ async def retry_job(job_id: str, request: Request, user: dict = Depends(require_
if job.get("status") not in {"failed", "dead_letter", "cancelled"}:
return _error(request, "Only failed, dead-lettered, or cancelled jobs can be retried.", 409, elapsed_ms(start))

payload = job.get("payload") if isinstance(job.get("payload"), dict) else {}
billing_account_id = payload.get("billing_account_id")
if billing_account_id:
billing_service = get_default_billing_service()
try:
estimate = billing_service.estimate_required_credits(job.get("job_type") or "", payload)
reservation = await asyncio.to_thread(
billing_service.reserve_credits,
billing_account_id,
job_id,
estimate.reserved_credits,
)
payload["billing_reservation_id"] = reservation.reservation_id
payload["billing_estimate"] = estimate.model_dump()
await asyncio.to_thread(get_default_job_store().update_payload, job_id, payload)
except InsufficientCredits as exc:
return _error(request, str(exc), 402, elapsed_ms(start))

await asyncio.to_thread(get_default_job_store().reset_for_retry, job_id, True)
job = await asyncio.to_thread(get_default_job_store().get, job_id)
try:
Expand All @@ -138,6 +157,7 @@ async def cancel_job(job_id: str, request: Request, user: dict = Depends(require
except Exception as exc:
error = str(exc) or exc.__class__.__name__
return _error(request, f"Cancel failed to reach workflow: {error}", 503, elapsed_ms(start))
await asyncio.to_thread(release_job_billing, job, "cancelled")
await asyncio.to_thread(get_default_job_store().mark_cancelled, job_id)
await asyncio.to_thread(_mark_scanner_job_cancelled, job)
job = await asyncio.to_thread(get_default_job_store().get, job_id)
Expand Down
91 changes: 75 additions & 16 deletions src/api/routes/v2/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
)
from src.api.routes.v2.temporal_client import start_job_workflow
from src.api.schemas import APIResponse, BatchIngestRequest, IngestRequest, ScrapeRequest, StatusEnum
from src.billing.service import InsufficientCredits, get_default_billing_service
from src.config import settings
from src.jobs.durable import QUEUED, get_default_job_store, new_attempt_id, stable_hash
from src.jobs.durable import QUEUED, get_default_job_store, idempotency_key, new_attempt_id, stable_hash

router = APIRouter(
prefix="/v2/memory",
Expand All @@ -39,6 +40,10 @@ def _content_hash(payload: Dict[str, Any]) -> str:
return stable_hash(payload)


def _durable_job_id(job_type: str, fields: Dict[str, Any]) -> str:
return f"{job_type}:{idempotency_key(job_type, fields)}"


class WorkflowStartFailed(RuntimeError):
def __init__(self, job: Dict[str, Any], error: str) -> None:
super().__init__(error)
Expand Down Expand Up @@ -117,22 +122,35 @@ async def ingest_memory_v2(req: IngestRequest, request: Request, user: dict = De
payload = req.model_dump()
payload["user_id"] = user_id
payload["timeout_seconds"] = float(settings.memory_ingest_timeout_seconds)
idempotency_fields = {
"user_id": user_id,
"org_id": payload.get("org_id", "default"),
"content_hash": _content_hash({
"user_query": req.user_query,
"agent_response": req.agent_response or "",
"session_datetime": req.session_datetime,
"image_url": req.image_url,
"effort_level": req.effort_level,
}),
}
job_id = _durable_job_id("memory_ingest", idempotency_fields)
billing_service = get_default_billing_service()

try:
account, estimate, reservation = await asyncio.to_thread(
billing_service.reserve_job_credits,
user=user,
job_id=job_id,
job_type="memory_ingest",
payload=payload,
)
payload["billing_account_id"] = account["id"]
payload["billing_reservation_id"] = reservation.reservation_id
payload["billing_estimate"] = estimate.model_dump()
job, created = await _enqueue_and_start(
job_type="memory_ingest",
payload=payload,
idempotency_fields={
"user_id": user_id,
"org_id": payload.get("org_id", "default"),
"content_hash": _content_hash({
"user_query": req.user_query,
"agent_response": req.agent_response or "",
"session_datetime": req.session_datetime,
"image_url": req.image_url,
"effort_level": req.effort_level,
}),
},
idempotency_fields=idempotency_fields,
user_id=job_user_id,
timeout_seconds=float(settings.memory_ingest_timeout_seconds),
max_attempts=3,
Expand All @@ -145,14 +163,28 @@ async def ingest_memory_v2(req: IngestRequest, request: Request, user: dict = De
elapsed_ms(start),
)
except WorkflowStartFailed as exc:
if payload.get("billing_account_id"):
await asyncio.to_thread(
billing_service.release_job_reservation,
payload["billing_account_id"],
job_id,
)
return _workflow_start_error(
request,
exc.job,
str(exc),
f"/v2/memory/ingest/{exc.job['job_id']}/status",
elapsed_ms(start),
)
except InsufficientCredits as exc:
return _error(request, str(exc), 402, elapsed_ms(start))
except Exception as exc:
if payload.get("billing_account_id"):
await asyncio.to_thread(
billing_service.release_job_reservation,
payload["billing_account_id"],
job_id,
)
return _error(request, str(exc), 500, elapsed_ms(start))
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved


Expand Down Expand Up @@ -183,15 +215,28 @@ async def batch_ingest_memory_v2(req: BatchIngestRequest, request: Request, user
min(len(req.items) * float(settings.memory_ingest_timeout_seconds), 3600.0),
),
}
idempotency_fields = {
"user_id": user_id,
"content_hash": _content_hash({"items": items}),
}
job_id = _durable_job_id("memory_batch_ingest", idempotency_fields)
billing_service = get_default_billing_service()

try:
account, estimate, reservation = await asyncio.to_thread(
billing_service.reserve_job_credits,
user=user,
job_id=job_id,
job_type="memory_batch_ingest",
payload=payload,
)
payload["billing_account_id"] = account["id"]
payload["billing_reservation_id"] = reservation.reservation_id
payload["billing_estimate"] = estimate.model_dump()
job, created = await _enqueue_and_start(
job_type="memory_batch_ingest",
payload=payload,
idempotency_fields={
"user_id": user_id,
"content_hash": _content_hash({"items": items}),
},
idempotency_fields=idempotency_fields,
user_id=user_id,
timeout_seconds=payload["timeout_seconds"],
max_attempts=3,
Expand All @@ -204,14 +249,28 @@ async def batch_ingest_memory_v2(req: BatchIngestRequest, request: Request, user
elapsed_ms(start),
)
except WorkflowStartFailed as exc:
if payload.get("billing_account_id"):
await asyncio.to_thread(
billing_service.release_job_reservation,
payload["billing_account_id"],
job_id,
)
return _workflow_start_error(
request,
exc.job,
str(exc),
f"/v2/memory/jobs/{exc.job['job_id']}/status",
elapsed_ms(start),
)
except InsufficientCredits as exc:
return _error(request, str(exc), 402, elapsed_ms(start))
except Exception as exc:
if payload.get("billing_account_id"):
await asyncio.to_thread(
billing_service.release_job_reservation,
payload["billing_account_id"],
job_id,
)
return _error(request, str(exc), 500, elapsed_ms(start))
Comment thread
ishaanxgupta marked this conversation as resolved.
Resolved


Expand Down
31 changes: 31 additions & 0 deletions src/billing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Billing and credit ledger package."""

from .service import (
InsufficientCredits,
commit_job_billing,
commit_job_debit,
ensure_billing_account,
estimate_required_credits,
get_billing_summary,
get_default_billing_service,
record_usage_event,
release_job_billing,
release_job_reservation,
reserve_credits,
reserve_job_credits,
)

__all__ = [
"InsufficientCredits",
"commit_job_billing",
"commit_job_debit",
"ensure_billing_account",
"estimate_required_credits",
"get_billing_summary",
"get_default_billing_service",
"record_usage_event",
"release_job_billing",
"release_job_reservation",
"reserve_credits",
"reserve_job_credits",
]
49 changes: 49 additions & 0 deletions src/billing/metering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

import math
from typing import Any, Mapping

from src.billing.types import CreditEstimate
from src.utils import billing as billing_config


def _ingest_text(payload: Mapping[str, Any]) -> str:
return "\n".join(
str(payload.get(key) or "")
for key in ("user_query", "agent_response")
if payload.get(key)
)


def content_tokens_for_job(job_type: str, payload: Mapping[str, Any]) -> int:
if job_type == "memory_batch_ingest":
return sum(
content_tokens_for_job("memory_ingest", item)
for item in list(payload.get("items") or [])
if isinstance(item, Mapping)
)
if job_type == "memory_ingest":
return billing_config.estimate_tokens(_ingest_text(payload))
if job_type == "memory_retrieve":
return billing_config.estimate_tokens(str(payload.get("query") or ""))
return billing_config.estimate_tokens(str(payload))


def estimate_required_credits(
job_type: str,
payload: Mapping[str, Any],
*,
include_reservation_buffer: bool = True,
) -> CreditEstimate:
tokens = content_tokens_for_job(job_type, payload)
multiplier = billing_config.workflow_multiplier(job_type, payload)
billable = max(1, math.ceil(tokens * multiplier))
buffer = billing_config.RESERVATION_BUFFER_MULTIPLIER if include_reservation_buffer else 1.0
reserved = max(billable, math.ceil(billable * buffer))
return CreditEstimate(
job_type=job_type,
content_tokens=tokens,
multiplier=multiplier,
billable_credits=billable,
reserved_credits=reserved,
)
Loading
Loading