算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
manage-token-budget【设计与创意】Monitor, cap, and recover from context accumulation in agentic systems. Covers per-cycle cost tracking, context window auditing, budget caps with enforcement policies, emergency pruning when approaching limits, and progressive disclosure integration to minimize token spend on routing. Use when running long-lived agent loops (heartbeats, polling, auto…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Control cost + ctx footprint of agentic systems → track tokens/cycle, audit ctx, enforce caps, prune low-value, route via metadata before full procs. Core: every token earns place. Inform decisions = stay; occupy space without influence = prune.
Community evidence: 37hr autonomous session = $13.74 from 30min heartbeat + verbose system instructions + unchecked ctx accumulation. Fix: 4hr intervals + notification-only + no feed browsing. This skill codifies prevention patterns.
Instrument loop → log tokens at every boundary.
Per cycle capture:
Store structured (JSONL, CSV, DB) — NOT in ctx window:
{"cycle": 47, "ts": "2026-03-12T14:30:00Z", "trigger": "heartbeat",
"input_tokens": 18420, "output_tokens": 2105, "cost_usd": 0.0891,
"cumulative_cost_usd": 3.42}
No instrumentation → estimate from billing:
→ Per-cycle log w/ tokens + costs, granular enough to find expensive cycles. Log lives outside ctx.
If err: no token counts (some APIs lack metadata) → billing → averages. Coarse tracking (daily $/daily cycles) reveals trends. None possible → Step 2 + estimate from ctx size.
Measure occupants, rank by size.
Decompose:
Budget table:
Context Budget Audit:
+------------------------+--------+------+-----------------------------------+
| Component | Tokens | % | Notes |
+------------------------+--------+------+-----------------------------------+
| System prompt | 4,200 | 21% | Includes CLAUDE.md chain |
| Memory (auto-loaded) | 3,800 | 19% | MEMORY.md + 4 topic files |
| Tool schemas | 2,600 | 13% | 3 MCP servers, 47 tools |
| Active skill procedure | 1,900 | 9% | Full SKILL.md loaded |
| Conversation history | 5,100 | 25% | 12 prior turns |
| Current cycle content | 2,400 | 12% | Tool outputs from this cycle |
+------------------------+--------+------+-----------------------------------+
| TOTAL | 20,000 | 100% | Model limit: 200,000 |
| Remaining headroom |180,000 | | |
+------------------------+--------+------+-----------------------------------+
Flag oversize vs. decision value. 4k-token memory unused this task = pure overhead.
→ Ranked table per consumer + size + %. ≥1 component stands out (usually history or verbose tool outs).
If err: hard to count → char/4 for English, char/3 for JSON/YAML. Goal = relative rank, not exact.
Hard + soft limits, action per limit.
Soft limit (warn): 60-75% of hard. Hit:
Hard limit (stop): absolute max. Hit:
Per-cycle cap: max per single cycle. Stops runaway:
Doc in workflow config:
token_budget:
soft_limit_usd: 5.00 # warn and begin pruning
hard_limit_usd: 10.00 # halt and alert
per_cycle_cap_usd: 0.50 # max per individual cycle
soft_limit_pct: 70 # % of context window triggering pruning
hard_limit_pct: 90 # % of context window triggering halt
enforcement: strict # strict = halt on hard limit; advisory = log only
alert_channel: notification # how to notify the operator
→ Caps doc'd at 3 levels (soft/hard/per-cycle) + explicit actions. Policy answers "what at limit?" before limit.
If err: $ premature → ctx-% only (soft 70%, hard 90%) + add $ after 24-48hr data. Advisory mode (log no halt) OK during calibration.
At limits, drop low-value systematically.
Priority (drop lowest first):
Per pruned item, tombstone:
[PRUNED: 2,400 tokens of npm audit output from cycle 12 — 3 vulnerabilities found, all patched]
~20 tokens but preserves conclusion.
→ Ctx drops below soft after prune. Each pruned item has tombstone. No decision-critical info lost.
If err: prune to lvl 4 + still over → workflow too ctx-heavy for cycle freq. Escalate: "N% post-prune. (a) longer interval, (b) reduce scope, (c) split workflows, (d) accept higher cost."
Route via registry metadata before loading full procs → spend tokens on routing not reading.
Pattern:
_registry.yml ~3-5 lines, ~50 tokensSame pattern for other large payloads:
Without progressive disclosure:
Load 5 candidate skills → 5 × 1,500 tokens = 7,500 tokens → use 1 skill
With progressive disclosure:
Route through 5 registry entries → 5 × 50 tokens = 250 tokens
Load 1 matched skill → 1 × 1,500 tokens = 1,500 tokens
Total: 1,750 tokens (77% reduction)
→ Skill loading = 2-phase: lightweight routing → full only on match. Same for memory, schemas, files.
If err: registry insufficient (vague descs, missing tags) → improve registry not abandon. Fix = better metadata, not more loading.
Set intervals from cost data, not arbitrary schedules.
Cost/hour at current interval:
cost_per_hour = avg_cost_per_cycle × cycles_per_hourVs. budget:
hours_until_hard_limit = (hard_limit - cumulative_cost) / cost_per_hourMin effective interval:
Apply:
Before: 30-minute heartbeat, verbose processing
→ 48 cycles/day × $0.09/cycle = $4.32/day
After: 4-hour heartbeat, notification-only
→ 6 cycles/day × $0.04/cycle = $0.24/day
→ 94% cost reduction
→ Interval justified by cost data + matches refresh rate. Tradeoff doc'd for future baseline.
If err: low-latency required, can't extend → reduce per-cycle cost (smaller prompts, fewer schemas, summarized history). 2 levers: frequency + cost/cycle.
Confirm all working + within budget.
Budget Validation Report:
+-----------------------+----------+--------+
| Check | Expected | Actual |
+-----------------------+----------+--------+
| Per-cycle logging | Present | |
| Soft limit warning | Fires | |
| Hard limit halt | Halts | |
| Per-cycle cap | Truncates| |
| Progressive disclosure| Routes | |
| Daily cost projection | < $X.XX | |
+-----------------------+----------+--------+
→ All 5 controls verified. Projection within budget.
If err: not firing → check enforcement wired into actual loop, not just doc'd. Config w/o enforcement = plan, not control. Projection over → Step 6, adjust interval/cost.
assess-context — eval reasoning ctx structural health; complements Step 2 auditmetal — extract conceptual essence; progressive disclosure applies in prospect phasechrysopoeia — value extraction + dead weight elim; same value-per-token at code levelmanage-memory — organize + prune persistent memory; reduces memory ctx component