contextbudget

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT 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 contextbudget provides explicit, local history budgeting helpers. It never changes the agent loop or calls a provider on its own.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConfig   = errors.New("context budget: invalid configuration")
	ErrLimit    = errors.New("context budget: resource limit exceeded")
	ErrEstimate = errors.New("context budget: counting or estimation failed")
	ErrBudget   = errors.New("context budget: required history does not fit")
	ErrNative   = errors.New("context budget: native history selection unsupported")
)

Functions

This section is empty.

Types

type Budget

type Budget struct {
	MaxTokens     int
	ReserveTokens int
	MinimumTurns  int
}

Budget is an explicit estimated total capacity. ReserveTokens is budgeted for the new prompt, tool schemas, output and provider framing not already counted. MinimumTurns defaults to one, clamped to the available turns.

type Counter

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

Counter counts ordinary text using an explicit embedded vocabulary. It does not infer model names, fetch dictionaries or encode special tokens.

func NewCounter

func NewCounter(enc Encoding) (*Counter, error)

func (*Counter) Count

func (c *Counter) Count(ctx context.Context, text string) (int, error)

Count reports text tokens, not the complete provider request cost. Inputs must be valid UTF-8 and at most 64 KiB. Counting is synchronous; cancellation is checked before and after it, not a hard CPU deadline.

func (*Counter) Estimator

func (c *Counter) Estimator(perMessage, perToolCall int) (EstimateFunc, error)

Estimator constructs a text-based estimate with caller-supplied framing overhead. The estimate sums field token counts and overhead; it is not an exact provider wire count. Reserve space for prompt, tools and output.

type Encoding

type Encoding string
const (
	Cl100kBase Encoding = "cl100k_base"
	O200kBase  Encoding = "o200k_base"
)

type EstimateFunc

type EstimateFunc func(context.Context, agent.Message) (int, error)

EstimateFunc estimates the additive cost of one complete message. A caller implementation must honor context and return a nonnegative bounded cost. Select supplies a copy, and does not expose arbitrary estimator errors.

type Selection

type Selection struct {
	Messages               []agent.Message
	KeptTurns              int
	DroppedTurns           int
	EstimatedHistoryTokens int
	ReservedTokens         int
}

func Select

func Select(ctx context.Context, history []agent.Message, b Budget, estimate EstimateFunc) (Selection, error)

Select retains the optional system message and the longest recent contiguous suffix of complete user turns that fits the supplied additive estimate. It validates the whole original history, never splits a tool group and never runs a model or a tool. It rejects native history in this version.

Jump to

Keyboard shortcuts

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