budget

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package budget provides daily token and cost budget enforcement for cloop. It reads from the cost ledger (.cloop/costs.jsonl) to calculate today's usage and enforces configured limits before task execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enforce

func Enforce(workDir string, cfg config.BudgetConfig, notifyCfg config.NotifyConfig) error

Enforce checks the daily budget and returns a non-nil error if any limit is exceeded, with a clear human-readable message. Use this before starting task execution to abort early rather than spending tokens on a blocked run.

Enforce also evaluates global (cross-project) budget limits and per-project percentage caps derived from the global limits.

Types

type CheckResult

type CheckResult struct {
	// Stats is today's aggregated usage.
	Stats DailyStats

	// USDExceeded is true when DailyUSDLimit > 0 and today's USD spend >= limit.
	USDExceeded bool
	// TokensExceeded is true when DailyTokenLimit > 0 and today's tokens >= limit.
	TokensExceeded bool

	// USDRemaining is the remaining USD budget (negative when exceeded). 0 when no limit.
	USDRemaining float64
	// TokensRemaining is the remaining token budget (negative when exceeded). 0 when no limit.
	TokensRemaining int

	// ThresholdAlertUSD fires when USD spend crosses AlertThresholdPct but is not yet exceeded.
	ThresholdAlertUSD bool
	// ThresholdAlertTokens fires when token count crosses AlertThresholdPct but is not yet exceeded.
	ThresholdAlertTokens bool
}

CheckResult is returned by Check, summarising the budget state.

func Check

func Check(workDir string, cfg config.BudgetConfig, notifyCfg config.NotifyConfig) (CheckResult, error)

Check evaluates today's usage against the configured daily limits. It fires desktop/webhook alerts when the alert threshold is crossed. notifyCfg controls which notification channels are active (may be zero value to skip).

func (CheckResult) Exceeded

func (r CheckResult) Exceeded() bool

Exceeded reports whether any budget limit has been exceeded.

type DailyStats

type DailyStats struct {
	TotalTokens int
	TotalUSD    float64
	EntryCount  int
}

DailyStats holds today's aggregated usage figures.

func DailyUsage

func DailyUsage(workDir string) (DailyStats, error)

DailyUsage reads the cost ledger and returns today's (UTC) aggregated usage. Returns an empty DailyStats (not an error) when no records exist.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL