JuliusBrussee/caveman · error

token budget must be positive, zero (default), or…

Error message

token budget must be positive, zero (default), or UnlimitedTokenBudget

What it means

Recall validates opts.TokenBudget before ranking results: only positive values, zero (meaning DefaultTokenBudget), or the UnlimitedTokenBudget sentinel are accepted. Any other negative value is rejected because the budget would make the selection logic behave incorrectly. This is a pure input-validation error, not a storage failure.

Solutions

  1. Use 0 for the default budget or mem.UnlimitedTokenBudget for unbounded recall.
  2. If the intent was unlimited, replace the negative literal with the UnlimitedTokenBudget constant.
  3. Clamp or validate any computed budget to >= 0 before constructing RecallOptions.
  4. Fix the upstream calculation producing a negative budget (e.g. max(0, limit - used)).

Example fix

// before
hits, err := store.Recall(ctx, q, mem.RecallOptions{TokenBudget: -1}) // 'unlimited' attempt
// after
hits, err := store.Recall(ctx, q, mem.RecallOptions{TokenBudget: mem.UnlimitedTokenBudget})
Defensive patterns

Strategy: validation

Validate before calling

b := opts.TokenBudget
if b < 0 && b != mem.UnlimitedTokenBudget {
    return errors.New("token budget must be >= 0 or UnlimitedTokenBudget")
}

Prevention

When it happens

Trigger: Calling Store.Recall(ctx, query, RecallOptions{TokenBudget: -N}) for any negative N other than mem.UnlimitedTokenBudget — e.g. TokenBudget: -1 or -5000.

Common situations: Developers use -1 hoping to mean 'unlimited' (a common sentinel convention elsewhere); it must be mem.UnlimitedTokenBudget instead. Others compute budgets from a subtraction that underflows below zero.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/8482ccfe5aed5e22. Report an issue: GitHub.

Appendix: source

Thrown at mem/store.go:425

// Recall returns the memories most relevant to query, ranked by BM25, filtered
// by the threshold, and compressed for injection. It fails toward no recall: a
// query with no term overlap (or that clears nothing above the threshold)
// returns an empty slice, never a guess.
func (s *Store) Recall(query string, opts RecallOptions) ([]Hit, error) {
	limit := opts.Limit
	if limit <= 0 {
		limit = DefaultLimit
	}
	threshold := opts.Threshold
	if threshold <= 0 {
		threshold = DefaultThreshold
	}
	budget := opts.TokenBudget
	unlimited := budget == UnlimitedTokenBudget
	if budget == 0 {
		budget = DefaultTokenBudget
	} else if budget < 0 && !unlimited {
		return nil, fmt.Errorf("token budget must be positive, zero (default), or UnlimitedTokenBudget")
	}

	all, err := s.all()
	if err != nil {
		return nil, err
	}
	scores := bm25Scores(query, all)

	type scored struct {
		mem   Memory
		score float64
	}
	ranked := make([]scored, 0, len(all))
	for _, m := range all {
		if sc := scores[m.ID]; sc >= threshold {
			ranked = append(ranked, scored{mem: m, score: sc})
		}
	}

View on GitHub (pinned to 3ee70a1026)