AgentSkillsCN

context-optimizer

优化上下文加载,以实现更高效的 Token 使用。适用于处理大型代码库、受限上下文环境,或当用户提及“上下文”、“Token”、“优化”、“总结”等词汇,或希望缩减上下文规模时使用。

SKILL.md
--- frontmatter
name: context-optimizer
description: Optimize context loading for efficient token usage. Use when working with large codebases, context limits, or when the user mentions "context", "token", "optimize", "summarize", or asks to reduce context size.

Context Optimizer

Optimize AI context loading for efficient token usage and focused development sessions.

When to Use

  • Working with large codebases (>50 files)
  • Context window approaching limits
  • User mentions "context", "token", "optimize"
  • Starting a new development session

Context Scoring

FactorWeightDescription
Direct relevance40%Directly related to current task
Dependency chain25%Required by relevant files
Recent access20%Recently read or modified
Reference frequency15%Often referenced in codebase

Optimization Strategies

StrategyLoad ScopeToken Savings
A: Essential OnlyModified files + type defs + immediate deps60-80%
B: Focused ContextWorking files + 1-level deps + docs + config40-60%
C: SummarizedFull working files + summaries + index30-50%

Document Priority Loading

PriorityFilesWhen
1 (Always)docs/CONTEXT.md, docs/PROGRESS.mdEvery session
2 (Phase)docs/phases/phase-N/SPEC.md, TASKS.mdPhase work
3 (On-Demand)docs/PRD.md, docs/TECH-SPEC.md, src/**/*As needed

Token Budget Guidelines

Session TypeBudgetLoading Strategy
Quick check~2KCONTEXT.md + PROGRESS.md
Standard dev~10KCONTEXT + PROGRESS + active files
Deep dive~30KAll docs + relevant source
Full context~50K+Complete project load

Incremental Context Protocol

Turn-by-turn progressive loading to minimize initial token cost.

Loading Sequence

TurnLoadTokensPurpose
1AGENTS.md (lean) + MANIFEST.md + CONTEXT.md~1.1KUser intent detection
2Task-specific files (TASKS.md row + source)+2-3KIntent-scoped work
3+On-demand (SPEC, PRD, etc.)+variesAs referenced

Note: auto_load_phase_docs deferred until Turn 2.

Expansion Triggers

User IntentLoad TargetBudget Impact
Phase 작업+phase docs (SPEC, TASKS)+2-3K
아키텍처 논의+TECH-SPEC + adjacent phase SPECs+5-8K
코드 리뷰+PRD (requirements) + CHECKLIST+3-5K
전체 현황+PROGRESS + all phase summaries+5-10K

settings.json Configuration

json
{
  "context-optimizer": {
    "loading_strategy": "incremental",
    "initial_budget": 800,
    "expansion_triggers": {
      "phase_work": "+phase_docs",
      "architecture": "+tech_spec+adjacent_phases",
      "review": "+prd+checklist",
      "full_status": "+progress+all_phase_summaries"
    }
  }
}

Task-Scoped Context Boundary

LevelScopeTokensContents
1: Task-Active (default)Single task~3-6KCONTEXT.md + Task row + source + direct deps
2: Phase-FullFull phase~8-10KLevel 1 + SPEC.md + CHECKLIST.md
3: Cross-PhaseArchitecture~12-15KLevel 2 + adjacent SPEC.md + TECH-SPEC sections

Task Context Format

markdown
## Task T2-03 Context
Source: docs/phases/phase-2/TASKS.md (row 3 only)

| ID | Task | Status | Priority | Est |
|----|------|--------|----------|-----|
| T2-03 | API endpoint 구현 | 🔄 | P0 | 3h |

Related files: server/src/routes/api.ts, server/src/models/schema.ts
Dependencies: T2-01 (DB schema), T2-02 (auth middleware)

Session Checkpoint Protocol

Auto-save on context threshold (>80% budget) for seamless recovery.

Checkpoint Template

markdown
# Session Checkpoint
Date: {{TIMESTAMP}}
Phase: {{CURRENT_PHASE}} | Task: {{TASK_ID}} | Progress: {{PHASE_PROGRESS}}%

## State
- Working on: {{TASK_DESCRIPTION}}
- Modified: {{FILE_LIST}}

## Decisions / Next / Resume Command

Recovery cost: AGENTS.md (300) + checkpoint (1.6K) = ~2K for instant recovery.

AGENTS.md Lean Template

Location: .codex/templates/AGENTS.lean.md

VariableSourceExample
{{PROJECT_NAME}}DISCOVERY.md"Resumely"
{{TECH_STACK}}DISCOVERY.md"Next.js+Supabase"
{{CURRENT_PHASE}}PROGRESS.md"2"
{{PHASE_PROGRESS}}TASKS.md calc"60"
FormatTokensSavings
Standard AGENTS.md~1,700+baseline
Lean AGENTS.md~300~82%

Agent MANIFEST Pattern

Location: .codex/agents/MANIFEST.md

code
1. MANIFEST.md loaded (~500 tokens, 25 agents × 1 row)
2. Match user intent → keyword lookup
3. Load only matched agent file (~1-3K tokens)
Total: ~1.5-3.5K per invocation vs. ~38K if all loaded

Best Practices

#Practice
1Start Lean: Ultra-lean AGENTS.md + MANIFEST only
2Task-First: Scope to current Task, not entire Phase
3Expand Incrementally: Add files only when referenced
4Structured > Prose: Use tables/key-value over paragraphs
5Checkpoint Often: Auto-save before context exhaustion
6Phase-Scoped Loading: Load current Phase docs by default
7Summarize Adjacents: Only SPEC.md from neighboring Phases
8Budget Awareness: Match loading strategy to session type