|
| 1 | +--- |
| 2 | +name: flow-code-context-eng |
| 3 | +description: "Use when context window is filling up, agent output quality drops, or starting a new complex task that needs careful context loading" |
| 4 | +--- |
| 5 | + |
| 6 | +# Context Engineering |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +Context engineering is about loading the RIGHT information at the RIGHT time — not "load everything." The context window is finite, attention degrades with noise, and wrong context is worse than no context. This skill teaches strategic context management: what to load, when to load it, and when to prune. |
| 11 | + |
| 12 | +## When to Use |
| 13 | + |
| 14 | +- Starting a new session or complex task that spans multiple files |
| 15 | +- Agent output quality is declining (wrong patterns, hallucinated APIs, ignored conventions) |
| 16 | +- Switching between different parts of a codebase |
| 17 | +- Agent ignores project conventions despite them being documented |
| 18 | +- Context window pressure — responses are getting slower or less accurate |
| 19 | +- Planning a review that needs cross-file understanding |
| 20 | + |
| 21 | +**When NOT to use:** |
| 22 | +- Simple single-file edits where the file is already loaded |
| 23 | +- Tasks where the relevant context is already in the conversation |
| 24 | +- Quick fixes where the error message contains all needed information |
| 25 | + |
| 26 | +## Core Process |
| 27 | + |
| 28 | +### Phase 1: Audit Current Context |
| 29 | + |
| 30 | +Before loading anything new, assess what you already have. |
| 31 | + |
| 32 | +**Ask these questions:** |
| 33 | +1. What files/specs are currently loaded in this conversation? |
| 34 | +2. What information is missing for the current task? |
| 35 | +3. What loaded context is stale or irrelevant (from a previous task)? |
| 36 | +4. How deep into the conversation are we? (stale context risk increases with depth) |
| 37 | + |
| 38 | +``` |
| 39 | +Current context audit: |
| 40 | + Loaded: [list what's in the conversation] |
| 41 | + Missing: [what the task needs but isn't loaded] |
| 42 | + Stale: [what was loaded for a previous task] |
| 43 | + Action: [load X, prune Y, refresh Z] |
| 44 | +``` |
| 45 | + |
| 46 | +### Phase 2: Apply the 5-Tier Hierarchy |
| 47 | + |
| 48 | +Structure context from most persistent to most transient. Higher tiers have higher priority — when the window is tight, cut from the bottom. |
| 49 | + |
| 50 | +``` |
| 51 | +┌─────────────────────────────────────────┐ |
| 52 | +│ Tier 1: Rules files │ ← Persistent, highest priority |
| 53 | +│ (CLAUDE.md, .cursorrules) │ |
| 54 | +├─────────────────────────────────────────┤ |
| 55 | +│ Tier 2: Specs & architecture │ ← Per-feature |
| 56 | +│ (SPEC.md, ADRs, epic specs) │ |
| 57 | +├─────────────────────────────────────────┤ |
| 58 | +│ Tier 3: Source files │ ← Per-task |
| 59 | +│ (relevant code, types, tests) │ |
| 60 | +├─────────────────────────────────────────┤ |
| 61 | +│ Tier 4: Error output │ ← Per-iteration |
| 62 | +│ (test failures, build errors, logs) │ |
| 63 | +├─────────────────────────────────────────┤ |
| 64 | +│ Tier 5: Conversation history │ ← Accumulating, lowest priority |
| 65 | +│ (previous messages, prior attempts) │ |
| 66 | +└─────────────────────────────────────────┘ |
| 67 | +``` |
| 68 | + |
| 69 | +**Tier 1 — Rules files:** Always loaded, never pruned. This is persistent context that survives across sessions. If it's not in the rules file, it doesn't exist for the agent. |
| 70 | + |
| 71 | +**Tier 2 — Specs & architecture:** Load the relevant section only. "Here's the auth section of our spec" beats "here's our entire 5000-word spec" when working on auth. |
| 72 | + |
| 73 | +**Tier 3 — Source files:** Load before editing. Read the file, its tests, one example of the pattern, and relevant type definitions. Use `structure` commands for signatures before committing to full file reads. |
| 74 | + |
| 75 | +**Tier 4 — Error output:** Feed specific errors, not entire logs. One failing test's traceback, not 500 lines of test output. |
| 76 | + |
| 77 | +**Tier 5 — Conversation history:** Compresses and degrades over time. Start fresh sessions when switching major features. Summarize progress when context is getting long. |
| 78 | + |
| 79 | +### Phase 3: Optimize CLAUDE.md |
| 80 | + |
| 81 | +CLAUDE.md is the highest-leverage context in any project. 10 lines there outweigh 1000 lines in conversation. |
| 82 | + |
| 83 | +**A good CLAUDE.md covers:** |
| 84 | +- Tech stack and versions |
| 85 | +- Build/test/lint commands (exact invocations) |
| 86 | +- Code conventions (with one example) |
| 87 | +- Boundaries (what NOT to do) |
| 88 | +- Project-specific patterns |
| 89 | + |
| 90 | +**Audit checklist:** |
| 91 | +- [ ] Does the CLAUDE.md exist? |
| 92 | +- [ ] Does it cover the commands an agent needs to run? |
| 93 | +- [ ] Does it show the conventions the agent keeps violating? |
| 94 | +- [ ] Is it under 200 lines? (longer = lower compliance) |
| 95 | + |
| 96 | +If the agent keeps making the same mistake, the fix is almost always adding a line to CLAUDE.md — not correcting the agent in conversation. |
| 97 | + |
| 98 | +### Phase 4: Use Existing Tools Strategically |
| 99 | + |
| 100 | +Do not reinvent context gathering. flow-code has purpose-built tools for each context need. |
| 101 | + |
| 102 | +| Need | Tool | When | |
| 103 | +|------|------|------| |
| 104 | +| Deep codebase understanding | `context-scout` agent | Before planning or major implementation | |
| 105 | +| AI-powered file discovery | `flow-code-rp-explorer` skill | When you need to find all files related to a feature | |
| 106 | +| Cross-model review context | `flow-code-export-context` skill | When preparing context for external LLM review | |
| 107 | +| Code signatures without full reads | `rp-cli structure` or `get_code_structure` | When you need function/type shapes, not full files | |
| 108 | +| Targeted file search | `rp-cli search` or `file_search` | When you know what pattern to find | |
| 109 | + |
| 110 | +**Decision tree:** |
| 111 | + |
| 112 | +``` |
| 113 | +Need to understand a feature? |
| 114 | + ├─ Know the files → Read them directly (Tier 3) |
| 115 | + ├─ Know the area → structure + targeted search |
| 116 | + └─ Don't know where to start → context-scout agent or builder |
| 117 | +
|
| 118 | +Need cross-file analysis? |
| 119 | + ├─ Architecture question → context_builder(response_type="question") |
| 120 | + ├─ Planning → context_builder(response_type="plan") |
| 121 | + └─ Review → context_builder(response_type="review") |
| 122 | +
|
| 123 | +Need to share context externally? |
| 124 | + └─ flow-code-export-context skill |
| 125 | +``` |
| 126 | + |
| 127 | +### Phase 5: Monitor and Prune |
| 128 | + |
| 129 | +Context management is not set-and-forget. Monitor throughout the session. |
| 130 | + |
| 131 | +**Prune triggers:** |
| 132 | +- Switched to a different task → remove files from the old task |
| 133 | +- Error was fixed → remove the error output and failed attempts |
| 134 | +- Conversation exceeds ~50 messages → consider fresh session |
| 135 | +- Agent starts hallucinating APIs → context is stale, reload source files |
| 136 | + |
| 137 | +**Token budgeting principles:** |
| 138 | +- Signatures (structure) cost ~500 tokens vs ~5000 for full files (10x savings) |
| 139 | +- Slice reads (`--start-line --limit`) cost ~300 tokens for 50 lines |
| 140 | +- Prefer structure first, full read only for files you will edit |
| 141 | +- Never dump full files for context — use signatures + targeted slices |
| 142 | + |
| 143 | +**Fresh session checklist:** |
| 144 | +1. Summarize progress so far (what's done, what's next) |
| 145 | +2. Note any decisions or conventions discovered |
| 146 | +3. Start new session with the summary + relevant Tier 1-2 context |
| 147 | +4. Reload only the Tier 3 files needed for the next task |
| 148 | + |
| 149 | +## Common Rationalizations |
| 150 | + |
| 151 | +| Excuse | Reality | |
| 152 | +|--------|---------| |
| 153 | +| "I'll just load everything" | Context windows are finite. Irrelevant context dilutes signal and degrades output quality. Precision beats volume. | |
| 154 | +| "CLAUDE.md doesn't matter much" | It's the highest-leverage file in any project. 10 lines in CLAUDE.md prevent more mistakes than 100 lines of in-conversation correction. | |
| 155 | +| "I can hold it all in my head" | Context compresses and degrades. What you loaded 20 messages ago may be effectively gone. Verify, don't assume. | |
| 156 | +| "More context is always better" | Research shows performance degrades with excess instructions. Wrong context is actively worse than no context — it introduces false patterns. | |
| 157 | +| "I'll read files when I need them" | Reactive loading causes mid-task quality drops. Proactive context loading at task start prevents the "forgot the convention" class of errors entirely. | |
| 158 | +| "The context window is huge, I'll use it all" | Window size is not attention budget. A 200K window with 190K of noise performs worse than a 50K window with 50K of signal. | |
| 159 | +| "I don't need to start a fresh session" | Stale context from previous tasks actively misleads. The cost of reloading is minutes; the cost of stale context is hours of wrong-direction work. | |
| 160 | + |
| 161 | +## Red Flags |
| 162 | + |
| 163 | +- Agent output stops following project conventions that are documented in CLAUDE.md |
| 164 | +- Agent invents APIs, imports, or utilities that don't exist in the codebase |
| 165 | +- Agent re-implements something that already exists (failed to load existing code) |
| 166 | +- Quality visibly degrades as conversation gets longer |
| 167 | +- Agent gives generic advice instead of project-specific answers |
| 168 | +- Same correction given to the agent more than twice in one session |
| 169 | +- No rules file exists in the project (context starvation guaranteed) |
| 170 | +- Full files dumped into context when only a function signature was needed |
| 171 | + |
| 172 | +## Verification |
| 173 | + |
| 174 | +After applying context engineering, confirm: |
| 175 | + |
| 176 | +- [ ] CLAUDE.md exists, is current, and covers tech stack, commands, conventions, and boundaries |
| 177 | +- [ ] Only task-relevant files are loaded (no leftover context from previous tasks) |
| 178 | +- [ ] Agent output references actual project files and APIs (not hallucinated ones) |
| 179 | +- [ ] Structure/signatures used before full file reads (token efficiency) |
| 180 | +- [ ] Context was refreshed when switching between major tasks |
| 181 | +- [ ] Error output is specific (single failure, not full logs) |
| 182 | +- [ ] Session was restarted if conversation exceeded ~50 messages on different topics |
0 commit comments