budget

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package budget enforces the hard caps of a run: tokens, spend, wall time, sandbox runs and agent turns. Caps are mandatory. There is no "unlimited": an agent loop with no ceiling is how a run costs more than the operator meant to spend.

Index

Constants

This section is empty.

Variables

View Source
var ErrExhausted = errors.New("budget exhausted")

ErrExhausted is returned (wrapped, naming the cap) when a cap is reached.

Functions

This section is empty.

Types

type Budget

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

Budget tracks spend against caps. It is safe for concurrent use.

func New

func New(caps Caps, clock func() time.Time) (*Budget, error)

New starts a budget. clock may be nil for time.Now.

func (*Budget) CanAfford

func (b *Budget) CanAfford(estTokens int, estUSD float64) error

CanAfford reports whether a call estimated at the given size fits in what is left. It lets the engine skip a task it could not finish instead of starting it and cutting it off.

func (*Budget) Caps

func (b *Budget) Caps() Caps

Caps returns the configured caps.

func (*Budget) Charge

func (b *Budget) Charge(u llm.Usage)

Charge records the usage of one model call.

func (*Budget) Check

func (b *Budget) Check() error

Check returns nil while the run may make another model call, and an error wrapping ErrExhausted that names the cap otherwise.

func (*Budget) Remaining

func (b *Budget) Remaining() time.Duration

Remaining returns how long the run may still take.

func (*Budget) Spent

func (b *Budget) Spent() Spent

Spent returns a snapshot of what has been used.

func (*Budget) UseSandbox

func (b *Budget) UseSandbox() error

UseSandbox reserves one sandbox run, or reports that the cap is reached.

type Caps

type Caps struct {
	Tokens      int
	USD         float64
	Wall        time.Duration
	SandboxRuns int
}

Caps are the limits of one run. Every field must be positive.

type Spent

type Spent struct {
	InputTokens  int
	OutputTokens int
	USD          float64
	SandboxRuns  int
	ModelCalls   int
	Elapsed      time.Duration
	// CostUnknown is true when any call used a model with no known price,
	// which means the USD cap could not be enforced for that call.
	CostUnknown bool
}

Spent is how much of the budget has been used.

func (Spent) Tokens

func (s Spent) Tokens() int

Tokens is the total of input and output tokens.

type Stagnation

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

Stagnation detects an agent that has stopped making progress: it keeps issuing tool calls it has already made. It is deliberately simple and counts exact repeats, which is how a stuck model actually behaves.

func NewStagnation

func NewStagnation(limit int) *Stagnation

NewStagnation returns a detector that trips after limit repeated calls.

func (*Stagnation) Observe

func (s *Stagnation) Observe(name, args string) (repeat bool)

Observe records a tool call (name plus raw arguments) and reports whether it repeats an earlier one.

func (*Stagnation) Stuck

func (s *Stagnation) Stuck() bool

Stuck reports whether the repeat limit has been reached.

Jump to

Keyboard shortcuts

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