This repository was archived by the owner on Jun 3, 2026. It is now read-only.
Repository navigation
Add modular billing credit ledger #212
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f930d01
Add modular billing credit ledger
ishaanxgupta 2dcc9fe
Address billing review findings
ishaanxgupta 0aaa7a2
Fix billing imports for CI
ishaanxgupta f22c59f
Harden billing webhook and reservation idempotency
ishaanxgupta bfc5254
Remove unused billing memory alias
ishaanxgupta fd1acfd
Avoid releasing reused billing reservations
ishaanxgupta 5ddb4b1
Release retry billing reservation on start failure
ishaanxgupta 620bbcb
Make billing grants and debits atomic
ishaanxgupta 70e10af
Make billing reserve and release atomic
ishaanxgupta 1beb1e6
Mark jobs cancelled before billing release
ishaanxgupta 68dc4fe
Keep billing debit claim transactional
ishaanxgupta c4b90b7
Release retry reservation on payload update failure
ishaanxgupta 800a82c
Close remaining Greptile billing gaps
ishaanxgupta 3a04784
Stabilize billing verification route test
ishaanxgupta 6744051
Fix cancel billing regression test store patch
ishaanxgupta 2a49af5
Avoid webhook grants without payment id
ishaanxgupta fb3237c
Keep billing route test lightweight
ishaanxgupta 894795e
Keep billing reserved if cancel status write fails
ishaanxgupta c85e9a1
Reject grantable webhooks missing payment ids
ishaanxgupta 3ead8fa
Handle grant webhook and retry failure edges
ishaanxgupta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.