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 ¶
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 ¶
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 (*Counter) Count ¶
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 EstimateFunc ¶
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.