Context Budget Allocation Deep Dive
Analysis of how OpenClaw's 200k token context budget is allocated across different components per LLM call.
Total Budget
Budget Breakdown
1. System Prompt (2,000-8,000 tokens)
Built by buildAgentSystemPrompt() from src/agents/system-prompt.ts:
Conditional sections (~20 components):
- Base identity and capabilities
- Workspace awareness
- Tool usage guidelines
- Channel-specific instructions (Telegram, Discord, Slack, etc.)
- Session management instructions
- Error handling protocols
- Multi-turn conversation guidelines
- Owner display secret instructions (if configured)
- Custom system prompt additions
Token range: 2,000-8,000 tokens depending on active features
2. Skills Catalog (1,500-3,000 tokens)
From src/agents/skills/workspace.ts:
Calculation:
- Max 150 skills
- Max 30,000 characters
- ~7-8k tokens at worst case (char/4 heuristic)
- Typical: 1,500-3,000 tokens
3. Tool Definitions (2,400-3,600 tokens)
24 core tools from Pi Agent SDK:
- Each tool definition: ~100-150 tokens
- Total: 24 × ~100-150 = 2,400-3,600 tokens
Core tools include:
- Bash, Read, Write, Edit
- Glob, Grep
- WebFetch
- NotebookEdit
- TodoWrite
- Skill
- And 14+ others
4. Memory (RAG) Injection (500-2,000 tokens)
From src/memory/manager.ts:
Per snippet:
- Max 700 characters
- ~175 tokens per snippet (char/4)
Typical injection:
- 3-10 snippets
- Total: 500-2,000 tokens
5. Compaction Reserve (20,000 tokens)
Purpose:
- Buffer for compaction operations
- Ensures enough space for summarization prompts
- Includes
SUMMARIZATION_OVERHEAD_TOKENS = 4,096
Trigger point: Compaction starts when context reaches ~180k (200k - 20k)
6. Session History (Remaining: ~163,000-174,000 tokens)
Calculation:
Typical range: 163,000-174,000 tokens (60-80% of total budget)
This is the largest consumer of context budget.
Budget Allocation by Use Case
Minimal Session (Empty History)
Typical Active Session
Near-Compaction Session
Compaction Trigger Logic
Optimization Strategies
1. Reduce System Prompt Size
- Strip unnecessary channel-specific instructions
- Use conditional prompting
- Savings: 1,000-2,000 tokens
2. Limit Skills Catalog
- Reduce
MAX_SKILLS_IN_PROMPTto 100 - Filter irrelevant skills per session
- Savings: 500-1,000 tokens
3. Lazy-Load Memory Snippets
- Only inject memory when explicitly needed
- Use relevance scoring
- Savings: 500-1,500 tokens
4. Aggressive History Pruning
- Reduce
maxHistorySharefrom 0.5 to 0.4 - Earlier compaction trigger
- Trade-off: Less context retention