Token Budget System

March 1, 2026 ยท View on GitHub

The token budget system controls how many tokens of learned behavior content are injected into agent system prompts. It ensures floop stays within LLM context limits while preserving the most important behaviors.

How Tiering Works

Every behavior is assigned one of four injection tiers based on its activation level:

TierActivation ThresholdContent
Full>= 0.7Complete canonical content
Summary>= 0.3One-line summary (or truncated canonical)
Name Only>= 0.1`name` [kind] #tags
Omitted< 0.1Not included

Constraints receive special protection: they are never demoted below Summary tier, regardless of activation level. This ensures safety-critical behaviors remain visible.

Budget Demotion

When the total token cost of all tiered behaviors exceeds the budget:

  1. Sort behaviors by activation (ascending)
  2. Demote the lowest-activation behavior one tier (Full -> Summary -> Name Only -> Omitted)
  3. Recalculate total tokens
  4. Repeat until within budget

Constraints are skipped during demotion (they stay at their assigned tier or the constraint minimum tier, whichever is higher).

Configuration

Configure token budgets in ~/.floop/config.yaml:

token_budget:
  # Budget for MCP resource handlers and CLI default (init, upgrade, prompt)
  default: 2000

  # Budget for hook-triggered activate calls (dynamic context injection)
  dynamic_context: 500

Environment Variable Overrides

VariableOverridesExample
FLOOP_TOKEN_BUDGETtoken_budget.defaultFLOOP_TOKEN_BUDGET=3000
FLOOP_TOKEN_BUDGET_DYNAMICtoken_budget.dynamic_contextFLOOP_TOKEN_BUDGET_DYNAMIC=800

Environment variables take precedence over config file values.

CLI Flags

The --token-budget flag is available on several commands and always overrides both config file and environment variable values:

CommandFlag Default SourceDescription
floop inittoken_budget.default (2000)Budget written into hook scripts
floop upgradetoken_budget.default (2000)Budget for upgraded hook scripts
floop activatetoken_budget.dynamic_context (500)Per-injection budget
floop prompt --tiered--token-budget flag (0 = unlimited)Budget for tiered prompt compilation

Precedence: CLI flag > environment variable > config file > built-in default

Session Tracking

The session state system provides additional budget controls for hook-triggered injections:

ParameterDefaultDescription
MaxTokenBudget3000Total tokens for the entire session
MaxPerInjection500Maximum tokens per single injection
BackoffMultiplier5Exponential backoff between re-injections

After each injection, the session tracks tokens consumed. Once the session-wide budget is exhausted, no more injections occur. The backoff multiplier spaces out re-injections: 1st is immediate, 2nd waits 5 prompts, 3rd waits 10, etc.

Architecture

Context (file, task, env)
         |
         v
  Spreading Activation Engine
         |
         v
  Results: [{behaviorID, activation}, ...]
         |
         v
  ActivationTierMapper
    - Map activation -> tier (thresholds: 0.7/0.3/0.1)
    - Enforce constraint minimum tier
    - Budget demotion (lowest activation first)
         |
         v
  InjectionPlan
    - FullBehaviors, SummarizedBehaviors, NameOnlyBehaviors, OmittedBehaviors
    - TotalTokens, TokenBudget
         |
         v
  Assembly Compiler
    - Compile tiered prompt text
    - Sections: Constraints, Directives, Procedures (full + summary + name-only)
         |
         v
  Prompt text injected into agent system prompt

Token Estimation

Token counts are estimated using the heuristic 1 token ~ 4 characters ((len(text) + 3) / 4). This is a rough approximation for English text. The canonical implementation lives in internal/tokens/estimate.go.

Key Files

FileRole
internal/tokens/estimate.goCentralized token estimation
internal/config/config.goTokenBudgetConfig (default + dynamic_context)
internal/tiering/activation_tiers.goActivationTierMapper (canonical tiering)
internal/tiering/bridge.goConvert scored behaviors to activation results
internal/assembly/compile.goTiered prompt compilation
internal/session/state.goSession-wide budget tracking and backoff
internal/mcp/handlers.goMCP resource handler (uses config budget)
cmd/floop/cmd_activate.goCLI activate (uses dynamic_context budget)