budget_controller¶
budget_controller ¶
Budget control and enforcement for LLM costs.
Implements cost monitoring with threshold warnings and hard limits.
BudgetExceededError ¶
Bases: Exception
Raised when budget limit is exceeded.
BudgetController ¶
BudgetController(max_budget: Decimal | None = None, warn_at_75: bool = True, warn_at_90: bool = True, fail_on_exceed: bool = True)
Controls and enforces budget limits during execution.
Follows Single Responsibility: only handles budget management.
Initialize budget controller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_budget
|
Decimal | None
|
Maximum allowed budget in USD |
None
|
warn_at_75
|
bool
|
Warn at 75% of budget |
True
|
warn_at_90
|
bool
|
Warn at 90% of budget |
True
|
fail_on_exceed
|
bool
|
Raise error if budget exceeded |
True
|
Source code in ondine/utils/budget_controller.py
check_budget ¶
Check if budget is within limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_cost
|
Decimal
|
Current accumulated cost |
required |
Raises:
| Type | Description |
|---|---|
BudgetExceededError
|
If budget exceeded and fail_on_exceed=True |
Source code in ondine/utils/budget_controller.py
get_remaining ¶
Get remaining budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_cost
|
Decimal
|
Current accumulated cost |
required |
Returns:
| Type | Description |
|---|---|
Decimal | None
|
Remaining budget or None if no limit |
Source code in ondine/utils/budget_controller.py
get_usage_percentage ¶
Get budget usage as percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_cost
|
Decimal
|
Current accumulated cost |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
Usage percentage or None if no limit |
Source code in ondine/utils/budget_controller.py
can_afford ¶
Check if estimated additional cost is within budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimated_cost
|
Decimal
|
Estimated cost for next operation |
required |
current_cost
|
Decimal
|
Current accumulated cost |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if within budget |