context-budget-guard

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Context Budget Guard

上下文预算防护

State file:
~/.openclaw/skill-state/context-budget-guard/state.yaml
Don't wait for the model to go incoherent. Act at 70%, not 95%.
状态文件:
~/.openclaw/skill-state/context-budget-guard/state.yaml
不要等到模型输出变得语无伦次再处理,在用量达到70%时就行动,而非等到95%。

When to Use

适用场景

  • At the start of any long-running session (>30 min expected)
  • Before each major task step in a multi-hour workflow
  • When
    long-running-task-management
    advances to a new stage
  • 任何预计运行时长超过30分钟的长会话开始时
  • 数小时工作流中每个主要任务步骤执行前
  • long-running-task-management
    推进到新阶段时

The Process

操作流程

Step 1: Initialize (Session Start)

步骤1:初始化(会话开始)

Write to state:
session_start
timestamp,
compaction_count: 0
,
status: monitoring
,
threshold_pct: 70
.
向状态文件写入:
session_start
时间戳、
compaction_count: 0
status: monitoring
threshold_pct: 70

Step 2: Check Budget (Before Each Major Step)

步骤2:检查预算(每个主要步骤执行前)

Estimate current context usage as a rough percentage:
  • Low (<50%) — continue normally
  • Medium (50–70%) — note in state, proceed with caution (avoid loading large files)
  • High (>70%) — trigger
    context-window-management
    NOW, before continuing
To estimate: count approximate tokens from recent messages, loaded file contents, and active task context.
估算当前上下文使用量的大致占比:
  • 低(<50%) — 正常继续操作
  • 中(50–70%) — 在状态中标记,谨慎推进(避免加载大文件)
  • 高(>70%) — 立即触发
    context-window-management
    ,再继续后续操作
估算方法:统计近期消息、已加载文件内容、活跃任务上下文的大致token数量。

Step 3: After Compaction

步骤3:上下文压缩后

  • Increment
    compaction_count
    in state
  • Write
    last_compacted_at
    ,
    strategy_used
    (passed from
    context-window-management
    )
  • Reset mental estimate to ~20% (post-compaction baseline)
  • 状态中的
    compaction_count
    计数+1
  • 写入
    last_compacted_at
    strategy_used
    (从
    context-window-management
    传入)
  • 将预估用量重置为约20%(压缩后基线值)

Step 4: Session End

步骤4:会话结束

Update state:
status: idle
,
session_end
timestamp.
更新状态:
status: idle
session_end
时间戳。

Key Principles

核心原则

  • The 70% threshold is the trigger — earlier is always better than later
  • Check budget BEFORE loading large files or running subagents, not after
  • If in doubt: compact. A clean context costs less than a confused one.
  • Works best paired with
    long-running-task-management
    — check budget at every checkpoint
  • 70%为固定触发阈值,提前行动永远优于延后处理
  • 请在加载大文件或运行subagent前检查预算,而非操作完成后再检查
  • 存疑时直接压缩:干净上下文的成本远低于混乱上下文带来的损失
  • long-running-task-management
    搭配使用效果最佳,在每个检查点都需检查预算