Budget¶
Cost tracking, budget enforcement, auto-downgrade, quota management, and CFO optimization.
Config¶
config
¶
Budget configuration models.
Implements the Cost Controls section of the Operations design page: alert thresholds, per-task and per-agent limits, automatic model downgrade, and risk budget configuration.
BudgetAlertConfig
pydantic-model
¶
Bases: BaseModel
Alert threshold configuration for budget monitoring.
Thresholds are expressed as percentages of the total monthly budget.
They must be strictly ordered: warn_at < critical_at < hard_stop_at.
Attributes:
| Name | Type | Description |
|---|---|---|
warn_at |
int
|
Percentage of budget that triggers a warning alert. |
critical_at |
int
|
Percentage of budget that triggers a critical alert. |
hard_stop_at |
int
|
Percentage of budget that triggers a hard stop. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
warn_at(int) -
critical_at(int) -
hard_stop_at(int)
Validators:
-
_validate_threshold_ordering
AutoDowngradeConfig
pydantic-model
¶
Bases: BaseModel
Automatic model downgrade configuration.
When enabled, models are downgraded to cheaper alternatives once
budget usage exceeds threshold percent. The downgrade_map is
stored as a tuple of (source_alias, target_alias) pairs to
maintain immutability.
Attributes:
| Name | Type | Description |
|---|---|---|
enabled |
bool
|
Whether auto-downgrade is active. |
threshold |
int
|
Budget percent that triggers downgrade. |
downgrade_map |
tuple[tuple[NotBlankStr, NotBlankStr], ...]
|
Ordered pairs of (from_alias, to_alias). |
boundary |
Literal['task_assignment']
|
When to apply downgrade (task_assignment only, never mid-execution per the Operations design page). |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
enabled(bool) -
threshold(int) -
downgrade_map(tuple[tuple[NotBlankStr, NotBlankStr], ...]) -
boundary(Literal['task_assignment'])
Validators:
-
_normalize_downgrade_map -
_validate_downgrade_map
boundary
pydantic-field
¶
When to apply downgrade (task_assignment only, never mid-execution)
BudgetConfig
pydantic-model
¶
Bases: BaseModel
Top-level budget configuration for a company.
Defines the overall monthly budget, alert thresholds, per-task and per-agent spending limits, and automatic model downgrade settings.
Attributes:
| Name | Type | Description |
|---|---|---|
total_monthly |
float
|
Monthly budget limit. |
alerts |
BudgetAlertConfig
|
Alert threshold configuration. |
per_task_limit |
float
|
Maximum cost per task. |
per_agent_daily_limit |
float
|
Maximum cost per agent per day. |
auto_downgrade |
AutoDowngradeConfig
|
Automatic model downgrade configuration. |
reset_day |
int
|
Day of month when budget resets (1-28, avoids month-length issues). |
currency |
str
|
ISO 4217 currency code for display formatting. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
total_monthly(float) -
alerts(BudgetAlertConfig) -
per_task_limit(float) -
per_agent_daily_limit(float) -
auto_downgrade(AutoDowngradeConfig) -
reset_day(int) -
currency(str) -
risk_budget(RiskBudgetConfig)
Validators:
-
_validate_per_task_limit_within_monthly -
_validate_per_agent_daily_limit_within_monthly
reset_day
pydantic-field
¶
Day of month when budget resets (1-28, avoids month-length issues)
Cost Record¶
cost_record
¶
Cost record model for per-API-call tracking.
Implements the Cost Tracking section of the Operations design page: every API call is tracked as an immutable cost record (append-only pattern).
CostRecord
pydantic-model
¶
Bases: BaseModel
Immutable record of a single API call's cost.
Once created, a CostRecord cannot be modified (frozen model).
This enforces the append-only pattern: new records are created for
each API call; existing records are never updated.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
NotBlankStr
|
Agent identifier (string reference). |
task_id |
NotBlankStr
|
Task identifier (string reference). |
provider |
NotBlankStr
|
LLM provider name. |
model |
NotBlankStr
|
Model identifier. |
input_tokens |
int
|
Input token count. |
output_tokens |
int
|
Output token count. |
cost_usd |
float
|
Cost in USD (base currency). |
timestamp |
AwareDatetime
|
Timezone-aware timestamp of the API call. |
call_category |
LLMCallCategory | None
|
Optional LLM call category for coordination metrics (productive, coordination, system). |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
agent_id(NotBlankStr) -
task_id(NotBlankStr) -
provider(NotBlankStr) -
model(NotBlankStr) -
input_tokens(int) -
output_tokens(int) -
cost_usd(float) -
timestamp(AwareDatetime) -
call_category(LLMCallCategory | None)
Validators:
-
_validate_token_consistency
call_category
pydantic-field
¶
LLM call category (productive, coordination, system)
Spending Summary¶
spending_summary
¶
Spending summary models for aggregated cost reporting.
Provides the aggregation data structures used by
:class:~synthorg.budget.tracker.CostTracker for cost reporting and
designed for consumption by the CFO agent (see Operations design page).
Views of :class:~synthorg.budget.cost_record.CostRecord data are
aggregated by agent, department, and time period.
PeriodSpending
pydantic-model
¶
Bases: _SpendingTotals
Spending aggregation for a specific time period.
Attributes:
| Name | Type | Description |
|---|---|---|
start |
datetime
|
Period start (inclusive). |
end |
datetime
|
Period end (exclusive). |
Fields:
-
total_cost_usd(float) -
total_input_tokens(int) -
total_output_tokens(int) -
record_count(int) -
start(datetime) -
end(datetime)
Validators:
-
_validate_period_ordering
AgentSpending
pydantic-model
¶
Bases: _SpendingTotals
Spending aggregation for a single agent.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
NotBlankStr
|
Agent identifier. |
Fields:
-
total_cost_usd(float) -
total_input_tokens(int) -
total_output_tokens(int) -
record_count(int) -
agent_id(NotBlankStr)
DepartmentSpending
pydantic-model
¶
Bases: _SpendingTotals
Spending aggregation for a department.
Attributes:
| Name | Type | Description |
|---|---|---|
department_name |
NotBlankStr
|
Department name. |
Fields:
-
total_cost_usd(float) -
total_input_tokens(int) -
total_output_tokens(int) -
record_count(int) -
department_name(NotBlankStr)
SpendingSummary
pydantic-model
¶
Bases: BaseModel
Top-level spending summary combining all aggregation dimensions.
Provides a snapshot of spending broken down by time period, agent, and department, along with budget utilization context.
Attributes:
| Name | Type | Description |
|---|---|---|
period |
PeriodSpending
|
Time-period aggregation. |
by_agent |
tuple[AgentSpending, ...]
|
Per-agent spending breakdown. |
by_department |
tuple[DepartmentSpending, ...]
|
Per-department spending breakdown. |
budget_total_monthly |
float
|
Monthly budget for context. |
budget_used_percent |
float
|
Percent of budget consumed. |
alert_level |
BudgetAlertLevel
|
Current budget alert level. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
period(PeriodSpending) -
by_agent(tuple[AgentSpending, ...]) -
by_department(tuple[DepartmentSpending, ...]) -
budget_total_monthly(float) -
budget_used_percent(float) -
alert_level(BudgetAlertLevel)
Validators:
-
_validate_unique_agent_ids -
_validate_unique_department_names
Cost Tiers¶
cost_tiers
¶
Cost tier definitions and classification.
Provides configurable metadata for cost tiers: price ranges, display
properties, and model-to-tier classification. The built-in CostTier
enum (synthorg.core.enums) defines the tier values; this module adds
a configurable layer on top.
CostTierDefinition
pydantic-model
¶
Bases: BaseModel
Metadata for a single cost tier.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
NotBlankStr
|
Unique tier identifier (e.g. |
display_name |
NotBlankStr
|
Human-readable name. |
description |
str
|
What this tier represents. |
price_range_min |
float
|
Minimum cost_per_1k_total for models in this tier. |
price_range_max |
float | None
|
Maximum cost_per_1k_total; |
color |
str
|
Hex color for UI rendering. |
icon |
str
|
Icon identifier for UI rendering. |
sort_order |
int
|
Display ordering (lower = cheaper). |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
id(NotBlankStr) -
display_name(NotBlankStr) -
description(str) -
price_range_min(float) -
price_range_max(float | None) -
color(str) -
icon(str) -
sort_order(int)
Validators:
-
_validate_price_range
CostTiersConfig
pydantic-model
¶
Bases: BaseModel
Configuration for cost tier definitions.
Attributes:
| Name | Type | Description |
|---|---|---|
tiers |
tuple[CostTierDefinition, ...]
|
User-defined tier overrides/additions. |
include_builtin |
bool
|
Whether to merge built-in default tiers. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
tiers(tuple[CostTierDefinition, ...]) -
include_builtin(bool)
Validators:
-
_validate_unique_ids
resolve_tiers
¶
Merge built-in and user-defined tiers, sorted by sort_order.
User tiers override built-in tiers with the same ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
CostTiersConfig
|
Cost tiers configuration. |
required |
Returns:
| Type | Description |
|---|---|
tuple[CostTierDefinition, ...]
|
Merged and sorted tuple of tier definitions. |
Source code in src/synthorg/budget/cost_tiers.py
classify_model_tier
¶
Classify a model into a cost tier based on total cost per 1k tokens.
Matches the first tier whose price range contains the given cost.
Range check: min <= cost < max (or min <= cost if max is
None). If tiers have overlapping ranges, the first match in
iteration order wins -- callers should ensure tiers are sorted by
sort_order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_per_1k_total
|
float
|
Combined |
required |
tiers
|
tuple[CostTierDefinition, ...]
|
Resolved tier definitions (should be sorted by sort_order). |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Tier ID of the matching tier, or |
Source code in src/synthorg/budget/cost_tiers.py
Hierarchy¶
hierarchy
¶
Budget hierarchy models.
Implements the Budget Hierarchy section of the Operations design page: Company to Department to Team, with percentage-based allocation at each level.
TeamBudget
pydantic-model
¶
Bases: BaseModel
Budget allocation for a single team within a department.
Attributes:
| Name | Type | Description |
|---|---|---|
team_name |
NotBlankStr
|
Team name (string reference). |
budget_percent |
float
|
Percent of department budget allocated to this team. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
team_name(NotBlankStr) -
budget_percent(float)
DepartmentBudget
pydantic-model
¶
Bases: BaseModel
Budget allocation for a department with nested team allocations.
Team budget percentages may sum to less than 100% to allow for an unallocated reserve within the department.
Attributes:
| Name | Type | Description |
|---|---|---|
department_name |
NotBlankStr
|
Department name (string reference). |
budget_percent |
float
|
Percent of company budget allocated to this department. |
teams |
tuple[TeamBudget, ...]
|
Team budget allocations within this department. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
department_name(NotBlankStr) -
budget_percent(float) -
teams(tuple[TeamBudget, ...])
Validators:
-
_validate_unique_team_names -
_validate_team_budget_sum
BudgetHierarchy
pydantic-model
¶
Bases: BaseModel
Company-wide budget hierarchy.
Maps the Company -> Department -> Team nesting from a budget allocation perspective (see Operations design page). Department budget percentages may sum to less than 100% to allow for an unallocated reserve at the company level.
Attributes:
| Name | Type | Description |
|---|---|---|
total_monthly |
float
|
Total company monthly budget in USD (base currency). |
departments |
tuple[DepartmentBudget, ...]
|
Department budget allocations. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
total_monthly(float) -
departments(tuple[DepartmentBudget, ...])
Validators:
-
_validate_unique_department_names -
_validate_department_budget_sum
Tracker¶
tracker
¶
Real-time cost tracking service.
Provides an in-memory store with TTL-based eviction for
:class:CostRecord entries and aggregation queries consumed by the CFO
agent and budget monitoring.
Service layer for the cost tracking schema defined in the Operations design page. The current implementation is purely in-memory; persistence integration is planned.
ProviderUsageSummary
¶
Bases: NamedTuple
Per-provider usage totals for a time window.
CostTracker
¶
CostTracker(
*,
budget_config=None,
department_resolver=None,
auto_prune_threshold=_AUTO_PRUNE_THRESHOLD,
)
In-memory cost tracking service with TTL-based eviction.
Records :class:CostRecord entries from LLM API calls and provides
aggregation queries for budget monitoring. Memory is bounded by a
soft TTL-based auto-prune: when the record count exceeds
auto_prune_threshold, records older than 168 hours (7 days)
are removed on the next query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
budget_config
|
BudgetConfig | None
|
Optional budget configuration for alert level
computation. When |
None
|
department_resolver
|
Callable[[str], str | None] | None
|
Optional callable mapping |
None
|
auto_prune_threshold
|
int
|
Maximum record count before auto-pruning is triggered on snapshot. Defaults to 100,000. |
_AUTO_PRUNE_THRESHOLD
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If auto_prune_threshold < 1. |
Source code in src/synthorg/budget/tracker.py
budget_config
property
¶
The optional budget configuration.
Returns:
| Type | Description |
|---|---|
BudgetConfig | None
|
Budget config if set, else |
record
async
¶
Append a cost record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_record
|
CostRecord
|
Immutable cost record to store. |
required |
Source code in src/synthorg/budget/tracker.py
prune_expired
async
¶
Remove records older than the 168-hour (7-day) cost window.
Call periodically from long-running services to bound memory growth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
now
|
datetime | None
|
Reference time. Defaults to current UTC time. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of records removed. |
Source code in src/synthorg/budget/tracker.py
get_total_cost
async
¶
Sum of cost_usd across all records, optionally filtered by time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime | None
|
Inclusive lower bound on |
None
|
end
|
datetime | None
|
Exclusive upper bound on |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Rounded total cost in USD (base currency). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both start and end are given and
|
Source code in src/synthorg/budget/tracker.py
get_agent_cost
async
¶
Sum of cost_usd for a single agent, optionally filtered by time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent identifier to filter by. |
required |
start
|
datetime | None
|
Inclusive lower bound on |
None
|
end
|
datetime | None
|
Exclusive upper bound on |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Rounded total cost in USD (base currency) for the agent. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both start and end are given and
|
Source code in src/synthorg/budget/tracker.py
get_record_count
async
¶
Total number of recorded cost entries.
Returns:
| Type | Description |
|---|---|
int
|
Number of cost records. |
get_records
async
¶
Return filtered cost records.
Returns an immutable snapshot of records matching the filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str | None
|
Filter by agent. |
None
|
task_id
|
str | None
|
Filter by task. |
None
|
provider
|
NotBlankStr | None
|
Filter by provider name. |
None
|
start
|
datetime | None
|
Inclusive lower bound on |
None
|
end
|
datetime | None
|
Exclusive upper bound on |
None
|
Returns:
| Type | Description |
|---|---|
tuple[CostRecord, ...]
|
Immutable tuple of matching cost records. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both start and end are given and
|
Source code in src/synthorg/budget/tracker.py
get_provider_usage
async
¶
Return aggregated token and cost totals for a provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
NotBlankStr
|
Provider to aggregate usage for. |
required |
start
|
datetime | None
|
Inclusive lower bound on |
None
|
end
|
datetime | None
|
Exclusive upper bound on |
None
|
Returns:
| Type | Description |
|---|---|
ProviderUsageSummary
|
Total tokens (input + output) and total cost. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both start and end are given and
|
Source code in src/synthorg/budget/tracker.py
build_summary
async
¶
Build a spending summary for the given period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime
|
Inclusive period start. |
required |
end
|
datetime
|
Exclusive period end. |
required |
Returns:
| Type | Description |
|---|---|
SpendingSummary
|
Aggregated spending summary with breakdowns and alert level. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/tracker.py
get_category_breakdown
async
¶
Build a per-category cost breakdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str | None
|
Filter by agent. |
None
|
task_id
|
str | None
|
Filter by task. |
None
|
start
|
datetime | None
|
Inclusive lower bound on timestamp. |
None
|
end
|
datetime | None
|
Exclusive upper bound on timestamp. |
None
|
Returns:
| Type | Description |
|---|---|
CategoryBreakdown
|
Category breakdown of cost, tokens, and call counts. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/tracker.py
get_orchestration_ratio
async
¶
Compute the orchestration overhead ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str | None
|
Filter by agent. |
None
|
task_id
|
str | None
|
Filter by task. |
None
|
start
|
datetime | None
|
Inclusive lower bound on timestamp. |
None
|
end
|
datetime | None
|
Exclusive upper bound on timestamp. |
None
|
thresholds
|
OrchestrationAlertThresholds | None
|
Optional custom alert thresholds. |
None
|
Returns:
| Type | Description |
|---|---|
OrchestrationRatio
|
Orchestration ratio with alert level. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/tracker.py
Enforcer¶
enforcer
¶
Budget enforcement service.
Composes :class:~synthorg.budget.tracker.CostTracker and
:class:~synthorg.budget.config.BudgetConfig to provide pre-flight
checks, in-flight budget checking, and task-boundary auto-downgrade
as described in the Cost Controls section of the Operations design page.
BudgetEnforcer
¶
BudgetEnforcer(
*,
budget_config,
cost_tracker,
model_resolver=None,
quota_tracker=None,
degradation_configs=None,
risk_tracker=None,
risk_scorer=None,
)
Budget enforcement: pre-flight, in-flight, and auto-downgrade.
Concurrency-safe via CostTracker's asyncio.Lock. Pre-flight checks are best-effort under concurrency (TOCTOU); the in-flight checker is the true safety net.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
budget_config
|
BudgetConfig
|
Limits and thresholds. |
required |
cost_tracker
|
CostTracker
|
Spend queries. |
required |
model_resolver
|
ModelResolver | None
|
Auto-downgrade alias lookup. |
None
|
quota_tracker
|
QuotaTracker | None
|
Provider-level quota enforcement. |
None
|
degradation_configs
|
Mapping[str, DegradationConfig] | None
|
Per-provider degradation strategies. |
None
|
risk_tracker
|
RiskTracker | None
|
Optional risk tracking service. |
None
|
risk_scorer
|
RiskScorer | None
|
Optional risk scoring implementation. |
None
|
Source code in src/synthorg/budget/enforcer.py
get_budget_utilization_pct
async
¶
Return monthly budget utilization as a percentage (0--100+).
Returns None when disabled (total_monthly <= 0) or
when the cost query fails (graceful degradation).
Source code in src/synthorg/budget/enforcer.py
check_can_execute
async
¶
Pre-flight: verify monthly + daily + quota limits allow execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent requesting execution. |
required |
provider_name
|
str | None
|
Optional provider name for quota checks.
When |
None
|
estimated_tokens
|
int
|
Estimated tokens for the upcoming request. Forwarded to the quota tracker for token-based checks. |
0
|
Returns:
| Type | Description |
|---|---|
PreFlightResult
|
Pre-flight result with effective provider info and |
PreFlightResult
|
degradation details when applicable. |
Raises:
| Type | Description |
|---|---|
BudgetExhaustedError
|
Monthly hard stop exceeded. |
DailyLimitExceededError
|
Agent daily limit exceeded
(subclass of |
QuotaExhaustedError
|
Provider quota exhausted and degradation could not resolve. |
Source code in src/synthorg/budget/enforcer.py
check_quota
async
¶
Check provider quota via QuotaTracker.
Returns always-allowed when no quota tracker is configured.
Note
Unlike check_can_execute, this method does not
catch unexpected exceptions from the underlying
QuotaTracker. check_can_execute wraps quota
checks in a try/except that falls back to allowing
execution on unexpected errors (graceful degradation),
but direct callers of check_quota are responsible
for their own error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Provider to check. |
required |
estimated_tokens
|
int
|
Estimated tokens for the request. |
0
|
Returns:
| Type | Description |
|---|---|
QuotaCheckResult
|
Quota check result. |
Source code in src/synthorg/budget/enforcer.py
resolve_model
async
¶
Apply auto-downgrade at task boundary if threshold exceeded.
Returns identity unchanged when downgrade is disabled, not
applicable, or lookup fails. Returns new AgentIdentity
with downgraded ModelConfig otherwise.
Source code in src/synthorg/budget/enforcer.py
make_budget_checker
async
¶
Create a sync BudgetChecker with pre-computed baselines.
Checks task limit, monthly total, and agent daily limit.
Baselines are snapshot-in-time (TOCTOU acceptable).
Returns None when all limits are disabled.
Source code in src/synthorg/budget/enforcer.py
check_risk_budget
async
¶
Pre-flight risk budget check.
Checks per-task, per-agent daily, and total daily risk limits including the projected risk of the pending action.
Pre-flight checks are best-effort under concurrency (TOCTOU). See class docstring.
Raises:
| Type | Description |
|---|---|
RiskBudgetExhaustedError
|
When a risk limit is exceeded and enforcement is active. |
Source code in src/synthorg/budget/enforcer.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 | |
record_risk
async
¶
Score and record a risk entry for the given action.
Returns None when risk budgets are disabled, no tracker
is configured, or no scorer is available.
Source code in src/synthorg/budget/enforcer.py
Quota¶
quota
¶
Quota and subscription models for provider cost tracking.
Defines quota windows, subscription configurations, degradation strategies, and quota check result models for providers that operate under subscription plans, local deployments, or pay-as-you-go billing.
QuotaWindow
¶
Bases: StrEnum
Time window for quota enforcement.
QuotaLimit
pydantic-model
¶
Bases: BaseModel
A single quota limit for a time window.
Attributes:
| Name | Type | Description |
|---|---|---|
window |
QuotaWindow
|
Time window for this limit. |
max_requests |
int
|
Maximum requests in the window (0 = unlimited). |
max_tokens |
int
|
Maximum tokens in the window (0 = unlimited). |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
window(QuotaWindow) -
max_requests(int) -
max_tokens(int)
Validators:
-
_at_least_one_limit
ProviderCostModel
¶
Bases: StrEnum
How a provider charges for usage.
Members
PER_TOKEN: Standard pay-as-you-go; cost computed from cost_per_1k_input/output. SUBSCRIPTION: Monthly flat fee; individual calls are pre-paid. LOCAL: Zero monetary cost; only hardware constraints.
SubscriptionConfig
pydantic-model
¶
Bases: BaseModel
Subscription and quota configuration for a provider.
Attributes:
| Name | Type | Description |
|---|---|---|
plan_name |
NotBlankStr
|
Name of the subscription plan. |
cost_model |
ProviderCostModel
|
How the provider charges for usage. |
monthly_cost |
float
|
Fixed monthly subscription fee in USD (base currency). |
quotas |
tuple[QuotaLimit, ...]
|
Rate/token/request limits per time window. |
hardware_limits |
str | None
|
Free-text hardware constraints for local models. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
plan_name(NotBlankStr) -
cost_model(ProviderCostModel) -
monthly_cost(float) -
quotas(tuple[QuotaLimit, ...]) -
hardware_limits(str | None)
Validators:
-
_validate_quotas_unique_windows -
_validate_cost_model_constraints
DegradationAction
¶
Bases: StrEnum
Action to take when a provider's quota is exhausted.
Members
FALLBACK: Route to a fallback provider. QUEUE: Wait for quota window reset, then retry. ALERT: Raise error and alert user.
DegradationConfig
pydantic-model
¶
Bases: BaseModel
Configuration for graceful degradation when quota is exhausted.
Attributes:
| Name | Type | Description |
|---|---|---|
strategy |
DegradationAction
|
What to do when quota is exhausted. |
fallback_providers |
tuple[NotBlankStr, ...]
|
Ordered fallback provider names. |
queue_max_wait_seconds |
int
|
Max seconds to wait when queueing. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
strategy(DegradationAction) -
fallback_providers(tuple[NotBlankStr, ...]) -
queue_max_wait_seconds(int)
Validators:
-
_validate_fallback_providers
QuotaSnapshot
pydantic-model
¶
Bases: BaseModel
Point-in-time snapshot of quota usage for a provider window.
Attributes:
| Name | Type | Description |
|---|---|---|
provider_name |
NotBlankStr
|
Provider this snapshot belongs to. |
window |
QuotaWindow
|
Time window for this snapshot. |
requests_used |
int
|
Requests consumed in this window. |
requests_limit |
int
|
Maximum requests allowed (0 = unlimited). |
tokens_used |
int
|
Tokens consumed in this window. |
tokens_limit |
int
|
Maximum tokens allowed (0 = unlimited). |
window_resets_at |
datetime | None
|
When the current window resets. |
captured_at |
datetime
|
When this snapshot was captured. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
provider_name(NotBlankStr) -
window(QuotaWindow) -
requests_used(int) -
requests_limit(int) -
tokens_used(int) -
tokens_limit(int) -
window_resets_at(datetime | None) -
captured_at(datetime)
requests_remaining
property
¶
Remaining requests in this window.
Returns None when the limit is not enforced (unlimited).
Returns 0 when fully consumed.
tokens_remaining
property
¶
Remaining tokens in this window.
Returns None when the limit is not enforced (unlimited).
Returns 0 when fully consumed.
QuotaCheckResult
pydantic-model
¶
Bases: BaseModel
Result of a pre-flight quota check.
Attributes:
| Name | Type | Description |
|---|---|---|
allowed |
bool
|
Whether the request is allowed. |
provider_name |
NotBlankStr
|
Provider that was checked. |
reason |
str
|
Human-readable reason (set when denied). |
exhausted_windows |
tuple[QuotaWindow, ...]
|
Which windows are exhausted (if any). |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
allowed(bool) -
provider_name(NotBlankStr) -
reason(str) -
exhausted_windows(tuple[QuotaWindow, ...])
Validators:
-
_validate_denied_has_reason
always_allowed_result
¶
Build an always-allowed QuotaCheckResult.
window_start
¶
Compute the UTC-aware start of the current quota window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window
|
QuotaWindow
|
Which time window to compute. |
required |
now
|
datetime | None
|
Reference timestamp. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
datetime
|
UTC-aware datetime at the start of the current window. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If now is a naive (timezone-unaware) datetime. |
Source code in src/synthorg/budget/quota.py
effective_cost_per_1k
¶
Compute effective cost per 1k tokens based on cost model.
Returns 0.0 for SUBSCRIPTION and LOCAL models (pre-paid / free).
Returns cost_per_1k_input + cost_per_1k_output for PER_TOKEN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_per_1k_input
|
float
|
Cost per 1k input tokens. |
required |
cost_per_1k_output
|
float
|
Cost per 1k output tokens. |
required |
cost_model
|
ProviderCostModel
|
The provider's cost model. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Effective cost per 1k tokens. |
Source code in src/synthorg/budget/quota.py
quota_tracker
¶
Quota tracking service.
Tracks per-provider request and token usage against configured quota windows. Window-based counters are rotated automatically when a window boundary is crossed.
Concurrency-safe via asyncio.Lock (same pattern as
:class:~synthorg.budget.tracker.CostTracker).
QuotaTracker
¶
Tracks per-provider quota usage across configured time windows.
Providers without a subscription config are silently ignored (no-op on record, always allowed on check).
Note
check_quota followed by record_usage is subject to a
TOCTOU gap: another coroutine could record usage between the
check and the record, pushing the counter past the limit.
This is by-design for the single-loop asyncio concurrency
model -- the gap only exists across await points and is
acceptable for quota enforcement (the in-flight budget checker
is the true safety net).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subscriptions
|
Mapping[str, SubscriptionConfig]
|
Mapping of provider name to subscription config. |
required |
Source code in src/synthorg/budget/quota_tracker.py
record_usage
async
¶
Record usage against all configured windows for a provider.
Rotates window counters if a window boundary has been crossed. Providers with no subscription config are skipped with a DEBUG log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Provider to record usage for. |
required |
requests
|
int
|
Number of requests to record (must be >= 0). |
1
|
tokens
|
int
|
Number of tokens to record (must be >= 0). |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If requests or tokens is negative. |
Source code in src/synthorg/budget/quota_tracker.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
check_quota
async
¶
Pre-flight check: can this provider handle a request?
Providers with no subscription config always return allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Provider to check. |
required |
estimated_tokens
|
int
|
Estimated tokens for the request (must be >= 0). |
0
|
Returns:
| Type | Description |
|---|---|
QuotaCheckResult
|
Check result with allowed status and reason. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If estimated_tokens is negative. |
Source code in src/synthorg/budget/quota_tracker.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
get_snapshot
async
¶
Get current usage snapshots for a provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Provider to query. |
required |
window
|
QuotaWindow | None
|
Optional specific window to query. If |
None
|
Returns:
| Type | Description |
|---|---|
tuple[QuotaSnapshot, ...]
|
Tuple of quota snapshots. |
Source code in src/synthorg/budget/quota_tracker.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
get_all_snapshots
async
¶
Get usage snapshots for all tracked providers.
Note
Snapshots are collected per-provider with separate lock acquisitions, so cross-provider consistency is not guaranteed under concurrent writes.
Returns:
| Type | Description |
|---|---|
dict[str, tuple[QuotaSnapshot, ...]]
|
Dict mapping provider name to tuple of snapshots. |
Source code in src/synthorg/budget/quota_tracker.py
peek_quota_available
¶
Synchronous snapshot of per-provider quota availability.
Reads cached counters without acquiring the lock. This is
safe when called synchronously from the asyncio event loop
thread (no concurrent mutations during synchronous execution)
and acceptable for heuristic selection decisions where TOCTOU
is tolerated. Do not call from a thread-pool executor or
from outside the event loop -- that would race with
record_usage.
Providers without configured quotas are excluded from the result (they are always allowed and do not need selection guidance).
Returns:
| Type | Description |
|---|---|
dict[str, bool]
|
Dict mapping provider name to availability status. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called from a different event loop than the one used by this tracker's async methods, or from outside an event loop when async methods have already been called (indicating a thread-pool executor context). |
Source code in src/synthorg/budget/quota_tracker.py
Billing¶
billing
¶
Billing period computation utilities.
Pure functions for determining billing period boundaries based on a
configurable reset day. Used by :class:~synthorg.budget.enforcer.BudgetEnforcer
to scope cost queries to the current billing cycle.
billing_period_start
¶
Compute the UTC-aware start of the current billing period.
If now.day >= reset_day, returns current month's reset_day
at 00:00 UTC. Otherwise, returns previous month's reset_day
at 00:00 UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reset_day
|
int
|
Day of month when the billing period resets (1-28). |
required |
now
|
datetime | None
|
Reference timestamp. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
datetime
|
UTC-aware datetime at midnight on the billing period start day. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/billing.py
daily_period_start
¶
Compute the UTC-aware start of today (midnight UTC).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
now
|
datetime | None
|
Reference timestamp. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
datetime
|
UTC-aware datetime at midnight of the current day. |
Source code in src/synthorg/budget/billing.py
Optimizer¶
optimizer
¶
CFO cost optimization service.
Provides spending anomaly detection, cost efficiency analysis, model
downgrade recommendations, routing optimization suggestions, and
operation approval decisions. Composes
:class:~synthorg.budget.tracker.CostTracker and
:class:~synthorg.budget.config.BudgetConfig for read-only analytical
queries -- the advisory complement to
:class:~synthorg.budget.enforcer.BudgetEnforcer.
Service layer backing the CFO role (see Operations design page).
CostOptimizer
¶
CFO analytical service for cost optimization.
Composes CostTracker and BudgetConfig for read-only analysis: anomaly detection, efficiency analysis, downgrade recommendations, routing optimization suggestions, and operation approval evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_tracker
|
CostTracker
|
Cost tracking service for querying spend. |
required |
budget_config
|
BudgetConfig
|
Budget configuration for limits and thresholds. |
required |
config
|
CostOptimizerConfig | None
|
Optimizer-specific configuration. Defaults to
|
None
|
model_resolver
|
ModelResolver | None
|
Optional model resolver for downgrade and routing optimization recommendations. |
None
|
Source code in src/synthorg/budget/optimizer.py
detect_anomalies
async
¶
Detect spending anomalies in the given period.
Divides [start, end) into window_count equal windows,
groups records by agent, and flags agents whose last-window
spending deviates significantly from their historical mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime
|
Inclusive period start. |
required |
end
|
datetime
|
Exclusive period end. |
required |
window_count
|
int
|
Number of time windows to divide the period into. Must be >= 2 and <= 1000. |
5
|
Returns:
| Type | Description |
|---|---|
AnomalyDetectionResult
|
Anomaly detection result with any detected anomalies. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/optimizer.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
analyze_efficiency
async
¶
Analyze cost efficiency of all agents in the period.
Computes cost-per-1k-tokens for each agent and rates them relative to the global average.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime
|
Inclusive period start. |
required |
end
|
datetime
|
Exclusive period end. |
required |
Returns:
| Type | Description |
|---|---|
EfficiencyAnalysis
|
Efficiency analysis with per-agent ratings. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/optimizer.py
recommend_downgrades
async
¶
Recommend model downgrades for inefficient agents.
Runs efficiency analysis and uses the model resolver and downgrade map to find cheaper alternatives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime
|
Inclusive period start. |
required |
end
|
datetime
|
Exclusive period end. |
required |
Returns:
| Type | Description |
|---|---|
DowngradeAnalysis
|
Downgrade analysis with recommendations. Empty when no |
DowngradeAnalysis
|
model_resolver is configured. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/optimizer.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
suggest_routing_optimizations
async
¶
Suggest routing optimizations based on actual usage patterns.
Analyzes each agent's most-used model and suggests cheaper alternatives available through the model resolver, comparing by cost and context window size.
Unlike recommend_downgrades which only targets INEFFICIENT
agents, this method analyzes all agents and suggests cheaper
alternatives regardless of efficiency rating -- any agent that
could use a cheaper model is a candidate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime
|
Inclusive period start. |
required |
end
|
datetime
|
Exclusive period end. |
required |
Returns:
| Type | Description |
|---|---|
RoutingOptimizationAnalysis
|
Routing optimization analysis with per-agent suggestions. |
RoutingOptimizationAnalysis
|
Empty when no model_resolver is configured. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/optimizer.py
evaluate_operation
async
¶
Evaluate whether an operation should proceed.
Evaluates three criteria in order:
- Rejects negative
estimated_cost_usdimmediately. - Denies if the projected alert level (after adding the estimated cost) meets or exceeds the auto-deny threshold.
- Denies if the projected cost would exceed the hard-stop limit.
- Approves with optional warning conditions for high-cost operations or elevated alert levels.
When total_monthly <= 0 budget enforcement is disabled and
the operation is always approved with no conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Agent requesting the operation. |
required |
estimated_cost_usd
|
float
|
Estimated cost of the operation. Must be >= 0. |
required |
now
|
datetime | None
|
Reference timestamp for billing period computation.
Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
ApprovalDecision
|
Approval decision with reasoning. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/optimizer.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | |
optimizer_models
¶
CFO / CostOptimizer domain models.
Frozen Pydantic models for anomaly detection, cost efficiency analysis,
downgrade recommendations, and approval decisions. Used by
:class:~synthorg.budget.optimizer.CostOptimizer and
:class:~synthorg.budget.reports.ReportGenerator.
AnomalyType
¶
Bases: StrEnum
Type of spending anomaly detected.
SUSTAINED_HIGH and RATE_INCREASE are reserved for future
detection algorithms; only SPIKE is currently produced.
AnomalySeverity
¶
Bases: StrEnum
Severity of a detected spending anomaly.
EfficiencyRating
¶
Bases: StrEnum
Cost efficiency rating for an agent.
SpendingAnomaly
pydantic-model
¶
Bases: BaseModel
A detected spending anomaly for a single agent.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
NotBlankStr
|
Agent exhibiting the anomaly. |
anomaly_type |
AnomalyType
|
Classification of the anomaly. |
severity |
AnomalySeverity
|
Severity level of the anomaly. |
description |
NotBlankStr
|
Human-readable explanation. |
current_value |
float
|
Spending in the most recent window. |
baseline_value |
float
|
Mean spending across historical windows. |
deviation_factor |
float
|
How many standard deviations above baseline. Set to 0.0 when the baseline is zero (no historical spending). |
detected_at |
datetime
|
Timestamp when the anomaly was detected. |
period_start |
datetime
|
Start of the window that triggered the anomaly. |
period_end |
datetime
|
End of the window that triggered the anomaly. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
agent_id(NotBlankStr) -
anomaly_type(AnomalyType) -
severity(AnomalySeverity) -
description(NotBlankStr) -
current_value(float) -
baseline_value(float) -
deviation_factor(float) -
detected_at(datetime) -
period_start(datetime) -
period_end(datetime)
Validators:
-
_validate_period_ordering
AnomalyDetectionResult
pydantic-model
¶
Bases: BaseModel
Result of an anomaly detection scan.
Attributes:
| Name | Type | Description |
|---|---|---|
anomalies |
tuple[SpendingAnomaly, ...]
|
Detected anomalies (may be empty). |
scan_period_start |
datetime
|
Start of the scanned period. |
scan_period_end |
datetime
|
End of the scanned period. |
agents_scanned |
int
|
Number of unique agents in the data. |
scan_timestamp |
datetime
|
When the scan was performed. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
anomalies(tuple[SpendingAnomaly, ...]) -
scan_period_start(datetime) -
scan_period_end(datetime) -
agents_scanned(int) -
scan_timestamp(datetime)
Validators:
-
_validate_period_ordering
AgentEfficiency
pydantic-model
¶
Bases: BaseModel
Cost efficiency metrics for a single agent.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
NotBlankStr
|
Agent identifier. |
total_cost_usd |
float
|
Total cost in the analysis period. |
total_tokens |
int
|
Total tokens consumed (input + output). |
cost_per_1k_tokens |
float
|
Cost per 1000 tokens (computed). |
record_count |
int
|
Number of cost records. |
efficiency_rating |
EfficiencyRating
|
Efficiency classification. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
agent_id(NotBlankStr) -
total_cost_usd(float) -
total_tokens(int) -
record_count(int) -
efficiency_rating(EfficiencyRating)
cost_per_1k_tokens
property
¶
Cost per 1000 tokens, derived from total_cost and total_tokens.
EfficiencyAnalysis
pydantic-model
¶
Bases: BaseModel
Result of a cost efficiency analysis.
Attributes:
| Name | Type | Description |
|---|---|---|
agents |
tuple[AgentEfficiency, ...]
|
Per-agent efficiency metrics (sorted by cost_per_1k desc). |
global_avg_cost_per_1k |
float
|
Global average cost per 1000 tokens. |
analysis_period_start |
datetime
|
Start of the analysis period. |
analysis_period_end |
datetime
|
End of the analysis period. |
inefficient_agent_count |
int
|
Number of agents rated INEFFICIENT (computed). |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
agents(tuple[AgentEfficiency, ...]) -
global_avg_cost_per_1k(float) -
analysis_period_start(datetime) -
analysis_period_end(datetime)
Validators:
-
_validate_period_ordering -
_validate_agents_sort_order
DowngradeRecommendation
pydantic-model
¶
Bases: BaseModel
A model downgrade recommendation for a single agent.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
NotBlankStr
|
Agent identifier. |
current_model |
NotBlankStr
|
Currently used model identifier. |
recommended_model |
NotBlankStr
|
Recommended cheaper model. |
estimated_savings_per_1k |
float
|
Estimated savings per 1000 tokens. |
reason |
NotBlankStr
|
Human-readable explanation. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
agent_id(NotBlankStr) -
current_model(NotBlankStr) -
recommended_model(NotBlankStr) -
estimated_savings_per_1k(float) -
reason(NotBlankStr)
Validators:
-
_validate_different_models
estimated_savings_per_1k
pydantic-field
¶
Estimated savings per 1000 tokens
DowngradeAnalysis
pydantic-model
¶
Bases: BaseModel
Result of a downgrade recommendation analysis.
Attributes:
| Name | Type | Description |
|---|---|---|
recommendations |
tuple[DowngradeRecommendation, ...]
|
Per-agent downgrade recommendations. |
total_estimated_savings_per_1k |
float
|
Aggregate estimated savings per 1000 tokens across all recommendations (computed). |
budget_pressure_percent |
float
|
Current budget utilization percentage. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
recommendations(tuple[DowngradeRecommendation, ...]) -
budget_pressure_percent(float)
ApprovalDecision
pydantic-model
¶
Bases: BaseModel
Result of evaluating whether an operation should proceed.
Attributes:
| Name | Type | Description |
|---|---|---|
approved |
bool
|
Whether the operation is approved. |
reason |
NotBlankStr
|
Explanation for the decision. |
budget_remaining_usd |
float
|
Remaining budget in USD (base currency) (may be negative if over budget). |
budget_used_percent |
float
|
Percentage of budget consumed. |
alert_level |
BudgetAlertLevel
|
Current budget alert level. |
conditions |
tuple[NotBlankStr, ...]
|
Any conditions attached to approval. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
approved(bool) -
reason(NotBlankStr) -
budget_remaining_usd(float) -
budget_used_percent(float) -
alert_level(BudgetAlertLevel) -
conditions(tuple[NotBlankStr, ...])
budget_remaining_usd
pydantic-field
¶
Remaining budget in USD (base currency) (negative when over budget)
CostOptimizerConfig
pydantic-model
¶
Bases: BaseModel
Configuration for the CostOptimizer service.
Attributes:
| Name | Type | Description |
|---|---|---|
anomaly_sigma_threshold |
float
|
Number of standard deviations above mean to flag as anomalous. |
anomaly_spike_factor |
float
|
Multiplier above mean to flag as spike (independent of stddev). |
inefficiency_threshold_factor |
float
|
Factor above global average cost_per_1k to flag as inefficient. |
approval_auto_deny_alert_level |
BudgetAlertLevel
|
Alert level at or above which operations are automatically denied. |
approval_warn_threshold_usd |
float
|
Cost threshold for adding a
warning condition to approval. When set to |
min_anomaly_windows |
int
|
Minimum number of historical windows required before anomaly detection activates. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
anomaly_sigma_threshold(float) -
anomaly_spike_factor(float) -
inefficiency_threshold_factor(float) -
approval_auto_deny_alert_level(BudgetAlertLevel) -
approval_warn_threshold_usd(float) -
min_anomaly_windows(int)
anomaly_sigma_threshold
pydantic-field
¶
Sigma threshold for anomaly detection
inefficiency_threshold_factor
pydantic-field
¶
Factor above global avg for inefficiency
approval_auto_deny_alert_level
pydantic-field
¶
Alert level triggering auto-deny
approval_warn_threshold_usd
pydantic-field
¶
Cost threshold for warning condition
min_anomaly_windows
pydantic-field
¶
Minimum historical windows for anomaly detection
RoutingSuggestion
pydantic-model
¶
Bases: BaseModel
A routing optimization suggestion for a single agent.
Suggests switching an agent's most-used model to a cheaper alternative that provides sufficient context window size.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
NotBlankStr
|
Agent identifier. |
current_model |
NotBlankStr
|
Currently most-used model identifier. |
suggested_model |
NotBlankStr
|
Suggested cheaper alternative. |
current_cost_per_1k |
float
|
Current model's total cost per 1k tokens. |
suggested_cost_per_1k |
float
|
Suggested model's total cost per 1k tokens. |
estimated_savings_per_1k |
float
|
Estimated savings per 1k tokens (computed). |
reason |
NotBlankStr
|
Human-readable explanation. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
agent_id(NotBlankStr) -
current_model(NotBlankStr) -
suggested_model(NotBlankStr) -
current_cost_per_1k(float) -
suggested_cost_per_1k(float) -
reason(NotBlankStr)
Validators:
-
_validate_different_models -
_validate_savings_positive
suggested_cost_per_1k
pydantic-field
¶
Suggested model total cost per 1k tokens
RoutingOptimizationAnalysis
pydantic-model
¶
Bases: BaseModel
Result of a routing optimization analysis.
Attributes:
| Name | Type | Description |
|---|---|---|
suggestions |
tuple[RoutingSuggestion, ...]
|
Per-agent routing optimization suggestions. |
total_estimated_savings_per_1k |
float
|
Aggregate estimated savings per 1k tokens across all suggestions (computed). |
analysis_period_start |
datetime
|
Start of the analysis period. |
analysis_period_end |
datetime
|
End of the analysis period. |
agents_analyzed |
int
|
Number of agents analyzed. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
suggestions(tuple[RoutingSuggestion, ...]) -
analysis_period_start(datetime) -
analysis_period_end(datetime) -
agents_analyzed(int)
Validators:
-
_validate_period_ordering
total_estimated_savings_per_1k
property
¶
Aggregate estimated savings per 1k tokens.
Reports¶
reports
¶
CFO spending report generation.
Provides multi-dimensional spending reports with breakdowns by task,
provider, model, and time-period comparison. Composes
:class:~synthorg.budget.tracker.CostTracker and
:class:~synthorg.budget.config.BudgetConfig.
Service layer backing CFO reporting (see Operations design page).
TaskSpending
pydantic-model
¶
Bases: BaseModel
Spending aggregation for a single task.
Attributes:
| Name | Type | Description |
|---|---|---|
task_id |
NotBlankStr
|
Task identifier. |
total_cost_usd |
float
|
Total cost for the task. |
total_tokens |
int
|
Total tokens consumed (input + output). |
record_count |
int
|
Number of cost records. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
task_id(NotBlankStr) -
total_cost_usd(float) -
total_tokens(int) -
record_count(int)
ProviderDistribution
pydantic-model
¶
Bases: BaseModel
Cost distribution for a single provider.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
NotBlankStr
|
Provider name. |
total_cost_usd |
float
|
Total cost for the provider. |
record_count |
int
|
Number of cost records. |
percentage_of_total |
float
|
Percentage of total spending. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
provider(NotBlankStr) -
total_cost_usd(float) -
record_count(int) -
percentage_of_total(float)
ModelDistribution
pydantic-model
¶
Bases: BaseModel
Cost distribution for a single model.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
NotBlankStr
|
Model identifier. |
provider |
NotBlankStr
|
Provider name. |
total_cost_usd |
float
|
Total cost for the model. |
record_count |
int
|
Number of cost records. |
percentage_of_total |
float
|
Percentage of total spending. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
model(NotBlankStr) -
provider(NotBlankStr) -
total_cost_usd(float) -
record_count(int) -
percentage_of_total(float)
PeriodComparison
pydantic-model
¶
Bases: BaseModel
Comparison of spending between two consecutive periods.
Attributes:
| Name | Type | Description |
|---|---|---|
current_period_cost |
float
|
Cost in the current period. |
previous_period_cost |
float
|
Cost in the previous period. |
cost_change_usd |
float
|
Absolute change in cost (computed). |
cost_change_percent |
float | None
|
Percentage change in cost (computed). None when previous period cost is zero. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
current_period_cost(float) -
previous_period_cost(float)
cost_change_percent
property
¶
Percentage cost change. None when previous period cost is zero.
SpendingReport
pydantic-model
¶
Bases: BaseModel
Multi-dimensional spending report.
Attributes:
| Name | Type | Description |
|---|---|---|
summary |
SpendingSummary
|
Overall spending summary for the period. |
by_task |
tuple[TaskSpending, ...]
|
Per-task spending breakdown. |
by_provider |
tuple[ProviderDistribution, ...]
|
Per-provider cost distribution. |
by_model |
tuple[ModelDistribution, ...]
|
Per-model cost distribution. |
period_comparison |
PeriodComparison | None
|
Comparison with previous period (optional). |
top_agents_by_cost |
tuple[tuple[NotBlankStr, float], ...]
|
Top agents by cost (sorted descending). |
top_tasks_by_cost |
tuple[tuple[NotBlankStr, float], ...]
|
Top tasks by cost (sorted descending). |
generated_at |
datetime
|
When the report was generated. |
Config:
frozen:Trueallow_inf_nan:False
Fields:
-
summary(SpendingSummary) -
by_task(tuple[TaskSpending, ...]) -
by_provider(tuple[ProviderDistribution, ...]) -
by_model(tuple[ModelDistribution, ...]) -
period_comparison(PeriodComparison | None) -
top_agents_by_cost(tuple[tuple[NotBlankStr, float], ...]) -
top_tasks_by_cost(tuple[tuple[NotBlankStr, float], ...]) -
generated_at(datetime)
Validators:
-
_validate_agent_ranking_order -
_validate_task_ranking_order
ReportGenerator
¶
Generates multi-dimensional spending reports.
Composes CostTracker and BudgetConfig to produce reports with breakdowns by task, provider, model, and period comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_tracker
|
CostTracker
|
Cost tracking service for querying spend. |
required |
budget_config
|
BudgetConfig
|
Budget configuration for context. |
required |
Source code in src/synthorg/budget/reports.py
generate_report
async
¶
Generate a spending report for the given period.
Fetches records and summary concurrently; derives total_cost
from the records snapshot for consistent distribution
percentages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
datetime
|
Inclusive period start. |
required |
end
|
datetime
|
Exclusive period end. |
required |
top_n
|
int
|
Maximum number of top agents/tasks to include. |
10
|
include_period_comparison
|
bool
|
Whether to compute a comparison with the previous period of the same duration. |
True
|
Returns:
| Type | Description |
|---|---|
SpendingReport
|
Multi-dimensional spending report. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/synthorg/budget/reports.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
Enums¶
enums
¶
Budget-specific enumerations.
BudgetAlertLevel
¶
Bases: StrEnum
Alert severity levels for budget thresholds.
Used by :class:~synthorg.budget.spending_summary.SpendingSummary
to indicate the current budget state relative to configured thresholds.
Errors¶
errors
¶
Budget-layer error hierarchy.
Defines budget-specific exceptions in a leaf module with minimal intra-project imports, preventing circular dependency chains when these exceptions are needed by both the budget enforcer and the engine layer.
BudgetExhaustedError
¶
Bases: Exception
Budget exhaustion signal.
Used in two contexts:
- Raised directly by :meth:
BudgetEnforcer.check_can_executewhen pre-flight budget checks fail (e.g., monthly hard stop, daily limit, or provider quota exceeded). - Caught by the engine layer (
AgentEngine.run) and used to build anAgentRunResultwithTerminationReason.BUDGET_EXHAUSTED.
DailyLimitExceededError
¶
Bases: BudgetExhaustedError
Per-agent daily spending limit exceeded.
RiskBudgetExhaustedError
¶
RiskBudgetExhaustedError(
msg, *, agent_id=None, task_id=None, risk_units_used=0.0, risk_limit=0.0
)
Bases: BudgetExhaustedError
Raised when cumulative risk budget is exhausted.
Subclass of BudgetExhaustedError so existing engine-level
catch handlers cover it transparently.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
The agent that exceeded the limit, or |
|
task_id |
The task during which the limit was exceeded, or |
|
risk_units_used |
Cumulative risk units consumed. |
|
risk_limit |
The limit that was exceeded. |
Source code in src/synthorg/budget/errors.py
QuotaExhaustedError
¶
Bases: BudgetExhaustedError
Raised when provider quota is exhausted and unresolvable.
Covers all terminal degradation outcomes: ALERT strategy (intentional immediate raise), failed FALLBACK (no providers available or all exhausted), and failed QUEUE (wait exceeded or still exhausted after waiting).
Attributes:
| Name | Type | Description |
|---|---|---|
provider_name |
The provider whose quota was exhausted,
or |
|
degradation_action |
The degradation strategy that was
attempted, or |