Context Budget Allocation Deep Dive

Analysis of how OpenClaw's 200k token context budget is allocated across different components per LLM call.

Total Budget

// From src/agents/defaults.ts
export const DEFAULT_CONTEXT_TOKENS = 200_000;

// From src/agents/pi-settings.ts
export const DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR = 20_000;

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:

const DEFAULT_MAX_SKILLS_IN_PROMPT = 150;
const DEFAULT_MAX_SKILLS_PROMPT_CHARS = 30_000;

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:

const SNIPPET_MAX_CHARS = 700;

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)

// From src/agents/pi-settings.ts
export const DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR = 20_000;

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:

200,000 (total)
  - 8,000 (system prompt, high estimate)
  - 3,000 (skills catalog, high estimate)
  - 3,600 (tool definitions, high estimate)
  - 2,000 (memory injection, high estimate)
  - 20,000 (compaction reserve)
  - 1,000 (other overhead)
---------
= 162,400 tokens available for session history

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)

System Prompt:     2,000 tokens   (1.0%)
Skills:            1,500 tokens   (0.75%)
Tools:             2,400 tokens   (1.2%)
Memory:              500 tokens   (0.25%)
Reserve:          20,000 tokens   (10%)
History:               0 tokens   (0%)
Available:       173,600 tokens   (86.8%)

Typical Active Session

System Prompt:     5,000 tokens   (2.5%)
Skills:            2,500 tokens   (1.25%)
Tools:             3,000 tokens   (1.5%)
Memory:            1,500 tokens   (0.75%)
Reserve:          20,000 tokens   (10%)
History:         120,000 tokens   (60%)
Used:            152,000 tokens   (76%)
Available:        48,000 tokens   (24%)

Near-Compaction Session

System Prompt:     6,000 tokens   (3%)
Skills:            3,000 tokens   (1.5%)
Tools:             3,600 tokens   (1.8%)
Memory:            2,000 tokens   (1%)
Reserve:          20,000 tokens   (10%)
History:         165,400 tokens   (82.7%)
Used:            200,000 tokens   (100%)
Compaction triggered!

Compaction Trigger Logic

// Compaction starts when approaching reserve floor
const effectiveLimit = DEFAULT_CONTEXT_TOKENS - DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR;
// effectiveLimit = 200,000 - 20,000 = 180,000

if (estimatedTokens > effectiveLimit) {
  // Trigger compaction
}

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_PROMPT to 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 maxHistoryShare from 0.5 to 0.4
  • Earlier compaction trigger
  • Trade-off: Less context retention

Cross-References