AgentSkillsCN

context-management

在长对话中,制定管理上下文/Token预算的策略。 当您处理大型代码库、进行深度分析,或执行多步骤任务时,可使用此技能。 触发条件:长期任务、大文件、多次搜索、“上下文即将耗尽”、复杂探索场景。

SKILL.md
--- frontmatter
name: context-management
description: |
  Strategies for managing context/token budget in long conversations.
  Use when working on large codebases, extensive analysis, or multi-step tasks.
  Triggers: long tasks, large files, multiple searches, "running low on context", complex exploration

Context Management Strategies

Universal patterns for managing token budget efficiently in long conversations.

Core Principles

  1. Search before read - Find what you need, don't read everything
  2. Progressive disclosure - Overview → Sample → Targeted deep-dive
  3. Summarize as you go - Extract key points, discard verbose content
  4. Save large outputs to files - Keep conversation context lean
  5. Proactive awareness - Monitor usage, warn before limits

Smart Reading Strategy

For Any Large Content

code
Step 1: List/Search → Identify what exists
Step 2: Quick scan → Read headers/summaries only
Step 3: Relevance ranking → Prioritize what matters
Step 4: Targeted reading → Deep dive only on relevant sections
Step 5: Synthesize → Create concise summary, reference files for details

Progressive Disclosure Pattern

PhaseContext CostAction
DiscoveryLowList files, search keywords
SamplingMediumRead first N lines, headers only
TargetedMedium-HighRead specific sections of interest
FullHighOnly if absolutely necessary

File-Based Output Strategy

When outputs would be large (>100 lines, >1000 rows):

markdown
✅ GOOD: Save to file, show summary
"Saved 1,500 results to output/analysis.csv
Summary: Top 10 items by revenue..."

❌ BAD: Dump everything into conversation
[1,500 rows of data filling context...]

When to Save to Files

  • Query results > 100 rows
  • Code generation > 100 lines
  • Analysis with detailed breakdowns
  • Multi-step outputs with intermediate results
  • Reference material for later use

Context Checkpoints

Periodically assess context usage:

markdown
**Context Check**: ~60% used
- Explored: 15 files ✓
- Key findings: documented ✓
- Remaining work: 3 tasks

**Action**: Continuing normally
markdown
**Context Check**: ⚠️ ~85% used

**Saving state**:
- Analysis summary → saved to scratch/analysis_summary.md
- Query history → saved to scratch/queries.sql

**Options**:
A) Run `/compact` to continue with fresh context
B) Focus on specific remaining question
C) Wrap up with executive summary

Proactive Warnings

Before large operations, warn:

markdown
"This query will return ~50,000 rows.

**Context Management Plan**:
✓ Execute query
✓ Save full results to CSV
✓ Show summary statistics in chat
✓ Display top 20 rows as preview

This keeps our conversation efficient. Proceed?"

Best Practices Checklist

Before each major read:

  • Will this return >100 lines? → Sample first
  • Multiple files to read? → Search for relevant ones
  • Already at 70% context? → Save next output to file

During exploration:

  • Check context every 5-10 operations
  • Save intermediate findings to files
  • Aggregate instead of showing raw data

When context is low:

  • Immediately save current state
  • Summarize findings so far
  • Suggest /compact or focused continuation
  • Offer clear next steps

Recovery Patterns

When You Hit the Limit

markdown
⚠️ Context limit approaching

**Saved to files**:
- findings.md - Key discoveries
- next_steps.md - Remaining work

**Summary**: [Key points that survive context reset]

**To continue**: Run `/compact`, then reference saved files

Handoff Format

When context resets are needed, save:

markdown
# Session Handoff

## Completed
- [What was accomplished]

## Key Findings
- [Important discoveries]

## Remaining Work
- [What still needs to be done]

## Files to Reference
- path/to/relevant/files

Remember

Context is a precious resource. Be surgical: search to find, sample to assess, read only what matters. Save verbose outputs to files and keep the conversation focused on insights and decisions, not raw data.