---
title: "Curated Code Context Window"
summary: "A search subagent finds the few relevant files for a task and injects only top snippets or summaries into the main coding agent's context"
status: "validated-in-production"
category: "Context & Memory"
url: "https://agentic-patterns.com/patterns/curated-code-context-window"
source: "https://www.youtube.com/watch?v=Xkwok_XXQgw"
authors: ["Nikola Balic (@nibzard)"]
tags: ["context-management","code-agent","file-selection","noise-reduction"]
signals: ["Coding agent works in a large repository","Loading all files adds noise and inflates token usage","Long-horizon tasks risk blowing up context length"]
anti_signals: ["Small codebase that fits in context","A frequently changing code index cannot be kept fresh"]
updated_at: "2026-10-14"
---

# Curated Code Context Window

> A search subagent finds the few relevant files for a task and injects only top snippets or summaries into the main coding agent's context

## Use when

- Coding agent works in a large repository
- Loading all files adds noise and inflates token usage
- Long-horizon tasks risk blowing up context length

## Avoid when

- Small codebase that fits in context
- A frequently changing code index cannot be kept fresh

## Problem

Loading **all source files** or dumping entire repositories into the agent's context overwhelms the model, introduces noise, and slows inference. Coding agents need to focus on **only the most relevant modules** to efficiently reason about changes or generate new functionality.

- Including every file biases the agent with irrelevant code; it "loses coherence" over large contexts.
- Large contexts inflate token usage, slowing down multi-turn RL training.

## Solution

Maintain a **minimal, high-signal code context** (keeping the context "sterile") for the main coding agent by:

**1. Context Sterilization**
- Exclude unrelated modules (e.g., test utilities when working on a UI component).
- Automatically identify relevant files via a lightweight **search agent** that returns top-K matches for a function or class name.

**2. Helper Subagent for Code Discovery**
- Spawn a **SearchSubagent** (a small LLM, vector-search index, or standard tools like `grep`/`ripgrep`) that takes a file path or query (e.g., "find definitions of `UserModel`") and returns a ranked list of file snippets.
- Only top-3 snippets (each ≤ 150 tokens) are injected into the main agent's context.
- Use **progressive disclosure**: fetch file summaries first, then load full content only when needed.

**3. Context Update Cycle**
- **Main Agent:** "I need to refactor `UserService`."
- **SearchSubagent:** "Found `user_service.py`, `models/user.py`, `utils/auth.py`."
- **Context Injection:** Only those three files (or their summaries) enter the main agent's window.

## Example

```mermaid
sequenceDiagram
    MainAgent->>SearchSubagent: "Find files defining UserModel"
    SearchSubagent-->>MainAgent: List of 3 file paths/snippets
    MainAgent->>Context: Inject these three code snippets only
    MainAgent->>Tool: edit_file(UserService)
```

## How to use it

- **Indexing Stage (Offline):** Build a simple **code index** (e.g., with `ripgrep`, `tree-sitter` AST parsing, or a vector store) to map function/class names to file paths.
- **Subagent Definition:** Define `SearchSubagent` as a function that queries the code index and uses a small LLM to filter and rank matches. Modern implementations often skip vector embeddings entirely, using standard tools (`grep`, `find`, file traversal) for cleaner deployment and always-current results.
- **Loading strategy per file:** Load small files in full; load selected sections of medium, high-relevance files; load only summaries, function signatures, or class structure for large or peripheral files. (Ported from the retired Curated File Context Window entry; the size bands are practitioner guidance, not measured thresholds.)
- **Relevance gate before injection:** Prefer files that share symbols with the primary task files or sit within one or two dependency hops. Same provenance: heuristic guidance, not validated thresholds.
- **Context Management Library:** Create a wrapper (e.g., `CuratedContextManager`) that automatically invokes `SearchSubagent` when the main agent asks for relevant code.

## Trade-offs

- **Pros:**
  - **Noise Reduction:** Keeps the context focused on pertinent code, improving reasoning clarity.
  - **Token Efficiency:** Dramatically reduces tokens consumed per step, boosting RL throughput.
  - **Context Anxiety Mitigation:** Helps prevent [context window anxiety](context-window-anxiety-management.md) by keeping usage well below limits.
  - **Cleaner Deployment:** Agentic search using standard tools avoids vector embedding infrastructure and always reflects current code state.
- **Cons/Considerations:**
  - **Index Freshness:** If code changes frequently, the index must be updated to avoid stale results.
  - **Complexity:** Adds an extra component (SearchSubagent + index) to the training and inference pipeline.
  - **Model Adaptation Required:** Different models may have varying tolerance for curated vs. full context approaches.

## References

- "Context is sacred" principle from the Open Source Agent RL talk (May 2025).
- Will Brown's commentary on "avoiding blowing up your context length" for long-horizon tasks.
- [Thorsten Ball's "Raising An Agent - Episode 3"](https://www.youtube.com/watch?v=5A7MkiWeOjk) - Amp's video series documenting a production coding agent built around a dedicated search agent.
- Sourcegraph (Amp) "Raising An Agent" series, the first-hand account of building enterprise coding agents: https://www.youtube.com/playlist?list=PL6zLuuRVa1_iUNbel-8MxxpqKIyesaubA (secondary summary: ["What Sourcegraph learned building AI coding agents"](https://www.nibzard.com/ampcode), May 2025).
- [Anthropic: Effective context engineering for AI agents](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents) (September 2025) - canonical current statement of just-in-time context retrieval and subagents that keep the main context clean.
- Claude Code best practices, which call the context window "the most important resource to manage" and recommend subagents for focused work: https://code.claude.com/docs/en/best-practices
