contextbudget

package
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 2 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 contextbudget is a leaf package (standard library only) holding the live context-window budget of one run: how many tokens the conversation has used against the model's window, whether a low-budget reminder has been delivered, and whether the model has asked to continue in a fresh window.

The agent loop owns the state's lifecycle: it creates one per run (or adopts the session-scoped one the driver passes in), publishes it into the run context, and observes the conversation size as the run progresses. The context-sensitive tools (get_context_remaining, new_context) and the budget reminder provider read the same object back from the context, so every surface sees one consistent figure without per-driver wiring — the same pattern as the judge's message snapshot.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithState

func WithState(ctx context.Context, s *State) context.Context

WithState returns a child context carrying the run's budget state.

Types

type Notice

type Notice struct {
	Tier      Tier
	Remaining int
	Used      int
	Window    int
}

Notice is one due reminder: the tier plus the figures it should carry.

type State

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

State is the concurrency-safe context budget of one run. Use New; the zero value is not usable.

func FromContext

func FromContext(ctx context.Context) *State

FromContext returns the budget state carried by ctx, or nil when the caller runs outside a budget-aware loop.

func New

func New() *State

New returns an empty budget state.

func (*State) Observe

func (s *State) Observe(used int)

Observe records the current conversation size in tokens.

func (*State) Remaining

func (s *State) Remaining() (int, bool)

Remaining returns the tokens left in the window, or ok=false while unknown.

func (*State) RequestRollover

func (s *State) RequestRollover()

RequestRollover asks the loop to start a fresh context window at the next turn boundary. It is unconditional; callers check RolloverEnabled first to explain why the request cannot be honored.

func (*State) RolloverEnabled

func (s *State) RolloverEnabled() bool

RolloverEnabled reports whether new_context can be honored.

func (*State) SetRolloverEnabled

func (s *State) SetRolloverEnabled(enabled bool)

SetRolloverEnabled records whether new_context can be honored this run.

func (*State) SetWindow

func (s *State) SetWindow(window int)

SetWindow records the model's context window in tokens (<=0 means unknown).

func (*State) TakeNotice

func (s *State) TakeNotice() (Notice, bool)

TakeNotice returns the next due low-budget reminder. A reminder fires once per tier per crossing; the escalation from notice to critical fires once more, and the ladder resets after the budget recovers (e.g. after compaction), so a later crossing reminds again.

func (*State) TakeRolloverRequest

func (s *State) TakeRolloverRequest() bool

TakeRolloverRequest consumes and reports a pending rollover request.

func (*State) Usage

func (s *State) Usage() (used, window int, ok bool)

Usage returns the last observed usage and whether a figure exists yet.

type Tier

type Tier int

Tier identifies which low-budget reminder fired.

const (
	// TierNone means no reminder is due.
	TierNone Tier = 0
	// TierNotice is the first low-budget reminder.
	TierNotice Tier = 1
	// TierCritical is the escalated reminder as the budget nears exhaustion.
	TierCritical Tier = 2
)

Jump to

Keyboard shortcuts

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