Master the four operations of context engineering — Write, Select, Compress, Isolate. Manage token budgets, compaction strategies, and context partitioning to keep AI sessions sharp and efficient.
git clone https://github.com/rohitg00/pro-workflow.git--- name: context-engineering description: Master the four operations of context engineering — Write, Select, Compress, Isolate. Manage token budgets, compaction strategies, and context partitioning to keep AI sessions sharp and efficient. user-invocable: true --- # Context Engineering Four operations control everything about how context flows through an AI coding session. Master them and you control the quality of every response. ## The Four Operations ### 1. Write — Persist Info Outside Context Move information out of the context window into durable storage so it survives compaction and session boundaries. **Where to write:** | Target | When | Example | |--------|------|---------| | CLAUDE.md | Permanent project rules | "Always use pnpm, never npm" | | NOTES.md / scratchpad | Working state for current task | Architecture decisions, open questions | | `.claude/memory/` | Learnings and patterns | `[LEARN]` rules from corrections | | External files | Data too large for context | Test plans, migration checklists | **Pattern — Scratchpad workflow:** ```text 1. Start complex task → create NOTES.md with goals and constraints 2. After research → write findings to NOTES.md 3. After compaction → NOTES.md survives, context does not 4. Resume → read NOTES.md to recover full state ``` ### 2. Select — Retrieve Relevant Info Pull the right information into context at the right time. Precision matters more than volume. **Methods ranked by precision:** 1. `@file` references — exact file injection 2. `grep` / `Glob` — targeted pattern search 3. Subagent exploration — delegated deep search 4. RAG / embeddings — semantic retrieval for large codebases **Key principle: Focused 300 tokens > unfocused 113K tokens.** A surgical grep result that returns the exact function signature beats dumping an entire module into context. Every irrelevant token dilutes attention. **Pattern — Progressive retrieval:** ```text 1. Start with file names (Glob) 2. Narrow to specific functions (Grep) 3. Read only the relevant lines (Read with offset+limit) 4. Never read entire large files when you need one function ``` ### 3. Compress — Reduce Tokens, Preserve Signal Shrink context without losing the information that matters. **Compaction strategies:** | Strategy | How | When | |----------|-----|------| | `/compact` with focus | `/compact focus: auth module changes` | Task boundaries | | Microcompact | Ask Claude to summarize tool output inline | After large reads/searches | | Head+tail | Read first 20 + last 20 lines of large output | Log analysis, test results | | Tool result clearing | Subagent results auto-clear after reporting | Heavy exploration | | Semantic selection | Summarize findings, discard raw data | Research phases | **Compaction triggers:** - After planning, before implementation - After completing a feature or milestone - When context exceeds 50% (set `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=50`) - Before switching task domains - After heavy search/read operations **PostCompact hook — Re-inject critical context:** ```json { "type": "PostCompact", "command": "cat .claude/critical-context.md" } ``` Use this to ensure project rules, current task state, or architecture constraints survive every compaction. ### 4. Isolate — Partition Across Execution Spaces Don't load everything into one context. Split work across independent execution spaces. | Method | Isolation Level | Use When | |--------|----------------|----------| | Subagents | Forked context | Heavy exploration, test runs, doc generation | | Worktrees (`claude -w`) | Full repo copy | Parallel features, competing approaches | | `/btw` (built-in Claude Code) | Temporary overlay | Quick questions without entering conversation history | | Agent teams | Independent sessions | Cross-layer changes, parallel reviews | | Fresh session (`/resume`) | Clean slate | Unrelated work, degraded context | **Pattern — Subagent delegation:** ```text Main session: planning, coordination, commits Subagent 1: explore auth module, report findings Subagent 2: run test suite, report failures Subagent 3: generate migration script ``` Main context stays clean. Subagents handle the volume. ## Context Budget Planning Example baseline (calibrate with `/context`): a 200K window (Haiku 4.5; Fable 5.1, Opus 5.5, and Sonnet 5 carry 1M) with ~20K overhead (CLAUDE.md, tool definitions, MCP schemas) leaves **~180K usable**. Quality still drops as context fills, so keep the same discipline on 1M tiers. Actual budgets vary by model and configuration. | Allocation | Budget | What Goes Here | |------------|--------|----------------| | Static context | 20-30K | CLAUDE.md, tool schemas, MCP definitions | | Dynamic context | 150-180K | Code, conversation, tool results | **Put static context first.** CLAUDE.md and tool definitions load before conversation. Keeping them stable maximizes prompt cache hits — saves cost and latency. | Phase | Target Usage | Action If Over | |-------|-------------|----------------| | Planning | < 20% | Keep plans concise, write to scratchpad | | Implementation | < 50% | Compact between files, delegate reads | | Testing | < 70% | Delegate test runs to subagents | | Review | < 85% | Start fresh session if degraded | ## When to /clear vs /compact vs Subagent | Situation | Action | |-----------|--------| | Task boundary, want to keep learnings | `/compact` with focus | | Context degraded, Claude repeating itself | `/compact`, then `/resume` if still bad | | Starting unrelated work | `/clear` or new session | | Heavy read/search operation | Delegate to subagent | | Quick side question | `/btw` (doesn't pollute main context) | | Exploring multiple approaches | Worktrees or agent teams | ## Anti-Patterns - Loading entire files when you need one function - Keeping MCP tool results in context after extracting what you need - Running 15+ MCPs (each adds tool schema overhead to every request) - Vague prompts that force Claude to search broadly ("fix the code") - Never compacting until auto-compact triggers at 95% ## Add to CLAUDE.md ```markdown ## Context Engineering Write to NOTES.md for working state that must survive compaction. Select with precision — grep first, read specific lines, never dump whole files. Compact at 50% or task boundaries. Set CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=50. Isolate heavy work to subagents. Main session stays for coordination and commits. ```
[{"step":"Define your token budget and compression goals. Use tools like 'token-counter' (Claude Code) or 'ChatGPT Token Counter' to estimate your current context size. Decide on a target compression ratio (e.g., 30% of original size) based on your session's needs.","tip":"For long sessions, aim for 20-40% compression. For short, focused tasks, 50-70% may suffice. Use the 'WRITE' operation to create a fresh background summary rather than reusing stale context."},{"step":"Select relevant prior interactions. Review your session history and identify 3-5 key interactions that directly relate to your current query. Use the 'SELECT' operation to extract these while excluding tangential discussions.","tip":"Look for interactions containing: specific data, previous decisions, or unresolved questions. Exclude generic greetings or off-topic tangents. Use interaction IDs if your platform supports them."},{"step":"Compress the selected context. Apply the 'COMPRESS' operation to distill the selected context into essential bullet points. Focus on preserving: key facts, unresolved issues, and actionable items.","tip":"Prioritize information by recency and relevance. Remove redundant details, merge similar points, and use abbreviations where unambiguous (e.g., 'RDS' instead of 'Amazon Relational Database Service')."},{"step":"Isolate the core focus. Use the 'ISOLATE' operation to extract the single most important question or action item that needs to be addressed next. This becomes your new context anchor.","tip":"Frame the isolated focus as a specific task (e.g., 'Recommend 3 cost-saving actions for RDS') rather than a vague question (e.g., 'How can I save money?'). This prevents scope creep."},{"step":"Iterate as needed. After receiving the AI's response, evaluate whether the context engineering achieved its goals. If the output is too broad or misses key details, repeat the process with adjusted parameters.","tip":"For complex topics, consider breaking the session into smaller, context-engineered segments. This prevents token bloat and keeps each interaction focused."}]
No install command available. Check the GitHub repository for manual installation instructions.
git clone https://github.com/rohitg00/pro-workflow/tree/main/skills/context-engineeringCopy the install command above and run it in your terminal.
Launch Claude Code, Cursor, or your preferred AI coding agent.
Use the prompt template or examples below to test the skill.
Adapt the skill to your specific use case and workflow.
I need to engineer the context for a conversation about [TOPIC]. First, [WRITE] a concise background summary (under [TOKEN_LIMIT] tokens) covering: [KEY_POINTS]. Then, [SELECT] the most relevant prior interactions from this session: [INTERACTION_IDS]. Next, [COMPRESS] the selected context into a bullet-point summary (under [COMPRESSION_RATIO]% of original size) that preserves only the essential details. Finally, [ISOLATE] the core question or action item that needs to be addressed in the next turn. Present the results in this format: '=== Background === [SUMMARY] === Selected Context === [INTERACTIONS] === Compressed Context === [BULLET_POINTS] === Isolated Focus === [ACTION_ITEM]'
=== Background === You're advising a SaaS startup on optimizing their AWS RDS PostgreSQL costs. The team has struggled with unpredictable query performance and rising database expenses over the past 6 months. Key pain points include: 1) 40% of queries are unoptimized, 2) RDS instances are over-provisioned by 30%, and 3) No automated scaling policies exist. Current monthly spend: $12,450. Previous attempts to address this involved manual query tuning (2 weeks effort) and a failed migration to Aurora Serverless. === Selected Context === Interaction #3: User shared their pg_stat_statements output showing 15 slow queries. Interaction #7: User mentioned their AWS bill increased by 22% last month despite 10% user growth. Interaction #12: User confirmed they're using db.t3.medium instances for all databases. === Compressed Context === - **Cost Problem**: $12,450/month AWS bill, +22% increase last month - **Performance Issues**: 15 slow queries (from pg_stat_statements), 40% unoptimized queries - **Infrastructure**: Over-provisioned RDS (db.t3.medium for all), no scaling policies - **History**: Manual tuning failed (2 weeks), Aurora Serverless migration didn't work === Isolated Focus === Recommend 3 immediate actions to reduce costs while improving query performance, prioritizing solutions that can be implemented within 2 weeks with minimal risk.
skills-collection
Take a free 3-minute scan and get personalized AI skill recommendations.
Take free scan