budget

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 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.

type Limits added in v0.0.5

type Limits struct {
	// DailyUSD and DailyTokens are the project's daily caps, each the lower of
	// its own setting and its percentage of the global limit.
	DailyUSD    float64
	DailyTokens int
	// GlobalDailyUSD and GlobalDailyTokens are the host-wide caps, across
	// every project.
	GlobalDailyUSD    float64
	GlobalDailyTokens int
}

Limits are the spend ceilings Enforce holds a run to. Zero means none.

budget.monthly_usd is not among them: it is reported by `cloop cost report` and enforced nowhere, which is worth knowing before relying on it.

func EffectiveLimits added in v0.0.5

func EffectiveLimits(cfg config.BudgetConfig, global globalbudget.GlobalBudgetConfig) Limits

EffectiveLimits computes the ceilings Enforce applies for a project with cfg on a host with global. Exported so `cloop hub doctor` reports the limits a run is held to, rather than reading the budget block and counting every field in it as one (Task 20387).

func (Limits) Bounded added in v0.0.5

func (l Limits) Bounded() bool

Bounded reports whether any of these ceilings is set. (The Claude Code subscription caps and block_extra_usage are enforced elsewhere, and are not among them.)

Jump to

Keyboard shortcuts

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