AgentSkillsCN

context-engineering

Token 优化与上下文窗口管理策略。适用于长时间会话、复杂任务,或接近上下文限制的场景。

SKILL.md
--- frontmatter
name: context-engineering
description: Token optimization and context window management strategies. Load for long sessions, complex tasks, or when approaching context limits.

Context Engineering

PRINCIPLE: Quality > Quantity. High-signal tokens beat exhaustive content.

When to Activate

  • Session approaching 70-80% context utilization
  • Multi-agent orchestration tasks
  • Large codebase exploration
  • Complex implementation spanning many files

Core Strategies

1. Progressive Disclosure

Load information just-in-time, not all-at-once.

code
❌ BAD: Read 20 files before starting work
✅ GOOD: Read files as you need them during implementation
ScopeStrategy
CodebaseUse grep/glob first, read only what's needed
DocumentationSummarize, don't dump entire docs
ResearchDelegate to researcher agent, get concise results

2. U-Shaped Attention

Critical information belongs at the beginning OR end of context.

LLMs have strongest attention at:

  • First ~10% of context (recency bias)
  • Last ~10% of context (position bias)
  • Middle content gets "lost"
code
✅ GOOD: Plan at top of session, current task at bottom
❌ BAD: Critical requirements buried in middle of conversation

3. Observation Masking

Replace verbose tool output with compact references.

code
❌ BAD: Full 500-line file content in every message
✅ GOOD: "See file X (read previously)" with key excerpts only

When reading files:

  • Quote only the relevant section
  • Reference line numbers for context
  • Don't repeat what's already in context

Compaction Safety

Triggers for Optimization

SignalAction
70% context usedStart summarizing older exchanges
80% context usedDelegate read-only work to sub-agents
90% context usedConsider session fork or compaction

During Compaction

The system may compress your history. Protect critical context:

  1. Use the plan - plan_save persists across compaction
  2. Use notepads - notepad_write for learnings/decisions
  3. Use delegations - Background tasks persist their results

What Survives Compaction

PersistsLost
Plan (plan.md)Old tool outputs
DelegationsConversation history
Notepad entriesFile contents read
System promptIntermediate reasoning

Multi-Agent Context Isolation

The Partition Principle

Keep main agent context clean by delegating "context-heavy" work.

typescript
// ❌ BAD: Read 50 files in main session
for (const file of files) {
  read(file) // pollutes main context
}

// ✅ GOOD: Delegate to explorer
task(agent="explore", prompt="Find all usages of X and summarize")
// Returns: concise summary, main context stays clean

Agent Responsibilities

AgentContext Strategy
exploreAbsorbs codebase reads, returns summary
researcherAbsorbs external docs, returns synthesis
coderFocused on single implementation unit
oracleReceives synthesized context, not raw data

Delegation Pattern

code
1. Main agent identifies need for broad search
2. Delegate to appropriate agent with specific question
3. Agent does heavy lifting (many reads/searches)
4. Returns concise, actionable summary
5. Main agent continues with minimal context impact

Tool Optimization

Efficient Tool Descriptions

Every tool in context should have clear:

ElementWhy
WhatSingle sentence purpose
WhenTrigger conditions
InputsRequired parameters
ReturnsExpected output format

Batch Operations

code
✅ GOOD: Single glob("**/*.ts") → process results
❌ BAD: Multiple sequential reads of guessed paths

Anti-Patterns

Anti-PatternCost
Reading entire files when you need 10 linesToken waste
Repeating context already in conversationToken waste
Critical info in middle of long promptAttention loss
Single agent for parallelizable workContext bloat
Dumping docs without summarizationSignal dilution

Checklist

When context is getting heavy:

  • Are you loading info just-in-time or all-at-once?
  • Is critical context at the start or end of messages?
  • Can any work be delegated to sub-agents?
  • Is the plan persisted for compaction safety?
  • Are you quoting only relevant excerpts from files?