budget

package
v0.4.21 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package budget provides iteration budget management for long-running tasks. Inspired by Hermes Agent's IterationBudget, this package prevents infinite loops and provides fine-grained control over agent execution iterations.

Index

Constants

View Source
const (
	// DefaultMaxIterations for parent agents
	DefaultMaxIterations = 90
	// DefaultSubagentMaxIterations for subagents
	DefaultSubagentMaxIterations = 50
	// DefaultSimpleTaskMaxIterations for simple tasks
	DefaultSimpleTaskMaxIterations = 30
	// DefaultComplexTaskMaxIterations for complex tasks
	DefaultComplexTaskMaxIterations = 150
)

Default budget configurations

Variables

This section is empty.

Functions

This section is empty.

Types

type Budget

type Budget struct {
	// contains filtered or unexported fields
}

Budget tracks iteration consumption for an agent or subagent. Each agent gets its own Budget instance to prevent resource exhaustion.

func New

func New(maxTotal int) *Budget

New creates a new Budget with the specified maximum iterations. Parent agents typically use maxIterations (default 90), subagents use delegation.maxIterations (default 50).

func Preset

func Preset(presetType string) *Budget

Preset creates a budget with preset configurations.

func (*Budget) Consume

func (b *Budget) Consume() bool

Consume attempts to consume one iteration. Returns true if the iteration was allowed, false if budget is exhausted.

func (*Budget) ConsumeN

func (b *Budget) ConsumeN(n int) int

ConsumeN attempts to consume N iterations atomically. Returns the number of iterations actually consumed (may be less than N if budget runs out).

func (*Budget) GetStats

func (b *Budget) GetStats() Stats

GetStats returns comprehensive budget statistics.

func (*Budget) IsExhausted

func (b *Budget) IsExhausted() bool

IsExhausted returns true if the budget is exhausted.

func (*Budget) MaxTotal

func (b *Budget) MaxTotal() int

MaxTotal returns the maximum allowed iterations.

func (*Budget) Refund

func (b *Budget) Refund()

Refund returns one iteration to the budget. Useful for execute_code iterations that shouldn't count against the budget.

func (*Budget) RefundN

func (b *Budget) RefundN(n int)

RefundN returns N iterations to the budget.

func (*Budget) Remaining

func (b *Budget) Remaining() int

Remaining returns the number of iterations remaining.

func (*Budget) UsagePercent

func (b *Budget) UsagePercent() float64

UsagePercent returns the percentage of budget used (0.0 - 100.0).

func (*Budget) Used

func (b *Budget) Used() int

Used returns the number of consumed iterations.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages budgets for multiple agents/tasks.

func NewManager

func NewManager() *Manager

NewManager creates a new budget manager.

func (*Manager) Create

func (m *Manager) Create(id string, maxTotal int) *Budget

Create creates a new budget for a task/agent.

func (*Manager) Delete

func (m *Manager) Delete(id string)

Delete removes a budget.

func (*Manager) Get

func (m *Manager) Get(id string) (*Budget, bool)

Get retrieves a budget by ID.

func (*Manager) GetGlobalStats

func (m *Manager) GetGlobalStats() (total, used, remaining int)

GetGlobalStats returns aggregated statistics across all budgets.

func (*Manager) List

func (m *Manager) List() []string

List returns all budget IDs.

type Stats

type Stats struct {
	MaxTotal     int
	Used         int
	Remaining    int
	Refunded     int
	UsagePercent float64
	IsExhausted  bool
	CreatedAt    time.Time
	LastUsedAt   time.Time
	Duration     time.Duration // Time since creation
}

Stats returns comprehensive budget statistics.

Jump to

Keyboard shortcuts

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