context-budget
Token optimization, context budgeting, and memory management for AI agents. Includes thresholds, warnings, and compaction strategies.
- License 许可
- MIT
- 最近更新
- 2026-08-26
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- context-budget
- description
- Token optimization, context budgeting, and memory management for AI agents. Includes thresholds, warnings, and compaction strategies.
02 资源文件 5
- 参考文档.skill-meta.json371 B
- 资产文件assets/01-template.md30 B
- 参考文档README.md431 B
- 参考文档references/01-overview.md32 B
- 参考文档references/02-usage.md24 B
03 SKILL.md 正文
Context Budget
Token optimization, context budgeting, and memory management. Keeps AI agents efficient within context limits.
Context Limits
| Model | Context | Warning | Critical |
|---|---|---|---|
| Haiku | 200K | 160K | 180K |
| Sonnet | 200K | 160K | 180K |
| Opus | 200K | 160K | 180K |
| GPT-4 | 128K | 100K | 115K |
| Claude 3.5 | 200K | 160K | 180K |
Budget Strategy
Conservative (Minimal)
- System prompt: < 5K tokens
- Context: < 50K tokens
- Use for: Simple tasks, single-file changes
Balanced (Standard)
- System prompt: < 10K tokens
- Context: < 80K tokens
- Use for: Most tasks, multi-file changes
Aggressive (Full)
- System prompt: < 15K tokens
- Context: < 150K tokens
- Use for: Complex tasks, architectural decisions
Workflow
Phase 1 — Measure
# Count tokens (approximate)
echo "$CONTEXT" | wc -c | awk '{print $1/4}'
# Or use API
opencode --token-count
Phase 2 — Budget
If context > warning threshold:
- Enable throttling
- Use tail sampling (last N messages)
- Trigger compaction warning
If context > critical threshold:
- Force compaction
- Disable non-essential hooks
- Warn user
Phase 3 — Optimize
# Slim system prompt
/context-budget --slim
# Compact context
/context-budget --compact
# Archive old messages
/context-budget --archive
Token Optimization Techniques
1. Slim Prompts
Before (bloated):
You are a senior software architect with 15+ years experience.
You have worked at Google, Meta, Amazon. You specialize in
clean architecture, testing, and best practices. You're
helpful but also challenging when needed...
After (concise):
Senior Architect. 15+ yrs exp. Clean architecture, testing.
Helpful, challenging when needed.
2. Lazy Loading
Only load what's needed:
- Load skills on-demand
- Use skill resolver
- Don't pre-load everything
3. Short References
Before:
As defined in our architecture decision record at /docs/adr/001.md:
- We use hexagonal architecture
- Ports and adapters pattern
- Domain driven design
After:
(Ref: /docs/adr/001.md - hexagonal architecture, ports/adapters, DDD)
4. Compact History
Tail sampling (keep last N messages):
# Keep last 20 messages
tail -n 20 conversation.json > compacted.json
Warning Levels
| Level | Threshold | Action |
|---|---|---|
| Green | < 70% | Normal |
| Yellow | 70-80% | Enable throttling |
| Orange | 80-90% | Suggest compaction |
| Red | > 90% | Force compaction |
Usage
# Check budget
/context-budget --check
# Slim prompts
/context-budget --slim
# Compact context
/context-budget --compact
# Set threshold
/context-budget --threshold=100000
Output Format
## Context Budget
### Current: 85,000 / 200,000 tokens (43%)
### Breakdown
- System prompt: 12,000 tokens (6%)
- Conversation: 65,000 tokens (33%)
- Tools output: 8,000 tokens (4%)
### Warning Level: GREEN ✅
### Recommendations
1. No action needed
2. Context is well within limits
Compaction Strategies
1. Summarize Old Messages
Replace old messages with summary.
2. Archive
Move old context to external storage.
3. Prune Tool Outputs
Keep only relevant tool outputs.
4. Inline Small Files
For small files, inline directly instead of tool references.
Best Practices
- Monitor continuously - Check every N messages
- Slim prompts - Keep system prompt minimal
- Lazy load - Only load what's needed
- Tail sampling - Keep recent, archive old
- Compact early - Don't wait for critical
04 同仓库更多 Skills
zustand-5
Zustand 5 state management patterns. Trigger: When managing React state with Zustand.
zod-4
Zod 4 schema validation patterns. Trigger: When using Zod for validation - breaking changes from v3.
x-bookmark-quote-posts
Workflow para revisar bookmarks de X/Twitter y convertirlos en drafts de quote posts. Accede a la sección de bookmarks vía browser headless, extrae posts recientes, y genera drafts con voz personal del usuario. No publica ni muta X — solo genera contenido para revisión. Trigger: X bookmarks, Twitter bookmarks, quote posts, bookmark automation, crear quote posts, revisar bookmarks, growth content X.
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
writing-plans
Use when you have a spec or requirements for a multi-step task, before touching code
work-unit-commits
Plan commits as reviewable work units. Trigger: implementation, commit splitting, chained PRs, or keeping tests and docs with code.