Self-Editing Search Agents

Agentic AI Systems advanced Building with AI ~35 min read

Overview

Agentic search systems break complex questions into multiple retrieval steps. That solves the recall problem of single-pass RAG, but it creates a new control problem: every search result, tool observation, and read document consumes context. After several turns, the search agent can be surrounded by stale chunks that looked promising earlier but no longer help answer the question. A self-editing search agent treats context as a managed workspace rather than an append-only transcript.

Chroma’s Context-1 report describes this pattern concretely. The search agent runs as a retrieval subagent for a downstream reasoning model. It decomposes the user query into subqueries, calls tools such as hybrid search, regex search, and document reads, and returns a ranked set of supporting documents. The distinctive mechanism is that the agent can also call a pruning tool to remove previously retrieved chunks from its own visible context when those chunks become irrelevant or redundant.

The transferable lesson is not tied to one model. Any long-horizon search agent needs a policy for what to retain, what to discard, when to keep exploring, and when to stop. Self-editing makes that policy explicit in the harness: token usage is visible, retrieval results are deduplicated, soft and hard limits create pressure to prune, and reward or evaluation can distinguish documents encountered during search from documents returned in the final answer set.

Core Concepts

Search Subagent

A search subagent is specialized for finding evidence, not composing the final natural-language answer. It receives a query and access to corpus tools, then returns supporting documents or chunks to a downstream model. This separation keeps expensive frontier reasoning focused on synthesis while a smaller or specialized policy performs the repetitive multi-turn retrieval loop.

Editable Working Context

Traditional agent loops append every observation to the conversation. Self-editing loops add a controlled deletion action. The model can remove chunk IDs from its current view, freeing context capacity for later searches. The harness may still preserve the full unedited trajectory for auditing and evaluation, but the model’s next decision is made from a leaner visible workspace.

Budget-Aware Action Space

Self-editing works best when the context budget is visible and enforceable. The agent should see current token usage after each turn. Past a soft threshold, the system nudges the agent to prune or conclude. Past a hard cutoff, non-pruning tool calls can be rejected until space is freed. This turns context management from hidden prompt engineering into part of the agent’s policy.

Key Concept: Pruning Is an Agent Action

In a self-editing search agent, pruning is not just a post-retrieval filter. It is a first-class tool call chosen during the search trajectory. The agent can decide that evidence was useful for exploration but should not remain in working context or the final returned set.

How It Works

Harness Loop

The harness exposes a small set of structured tools. Context-1 uses hybrid corpus search, regex search, full document reads, and chunk pruning. A production implementation can use different backends, but the important shape is the same: retrieval actions add observations, pruning actions remove visible observations, and final actions return selected evidence.

class SearchHarness:
    def __init__(self, corpus, token_budget=32768):
        self.corpus = corpus
        self.visible_chunks = {}
        self.seen_chunk_ids = set()
        self.token_budget = token_budget

    def observe_budget(self):
        used = count_tokens(self.visible_chunks.values())
        return f"[Token usage: {used}/{self.token_budget}]"

    def search_corpus(self, query):
        results = hybrid_search(self.corpus, query, exclude=self.seen_chunk_ids)
        reranked = rerank(query, results)[:10]
        for chunk in fit_to_budget(reranked, self.remaining_budget()):
            self.visible_chunks[chunk.id] = chunk.text
            self.seen_chunk_ids.add(chunk.id)
        return self.observe_budget()

    def prune_chunks(self, chunk_ids):
        for chunk_id in chunk_ids:
            self.visible_chunks.pop(chunk_id, None)
        return self.observe_budget()

The exclusion set is separate from the visible context. A pruned chunk should not be immediately retrieved again through a near-identical query, but it may still count as encountered evidence in the trace. This prevents loops where the agent repeatedly finds and deletes the same distracting result.

Soft and Hard Context Limits

The harness can create a graduated response to memory pressure. Below the soft threshold, search and read tools behave normally. Above it, tool observations include a reminder that pruning may be necessary. Above the hard cutoff, search and read calls fail fast, leaving pruning or finalization as the only useful actions.

def allowed_tools(used_tokens, budget):
    soft = int(budget * 0.75)
    hard = int(budget * 0.92)

    if used_tokens >= hard:
        return {"prune_chunks", "finish"}
    if used_tokens >= soft:
        return {"search_corpus", "grep_corpus", "read_document", "prune_chunks", "finish"}, "prune-soon"
    return {"search_corpus", "grep_corpus", "read_document", "prune_chunks", "finish"}

Training and Evaluation Signals

A self-editing policy needs feedback that rewards both exploration and final selection. Chroma’s report separates trajectory recall from final output precision and recall. That distinction matters: an agent may find the right document but prune it by mistake, or it may encounter the right document and correctly omit several distractors from the final result. Keeping both metrics exposes where the search policy failed.

Practical Applications

Enterprise questions often require chained evidence: a project name points to an internal planning document, which names a vendor, which points to a contract, which links to a security review. A self-editing search agent can keep early clues long enough to guide the next query, then prune intermediate clutter once the authoritative documents are found.

Regulated-domain retrieval requires high recall without flooding the final answer context with every plausible hit. Self-editing helps the agent distinguish evidence that should be returned from distractors that merely share names, dates, or partial constraints. The final output can be a compact ranked set of documents for a separate reasoning or review step.

Email and Support Corpus Investigation

Email and support archives contain informal language, quoted threads, repeated signatures, and fragmented references. Search agents need to follow clues across messages while discarding repeated or irrelevant quoted material. A pruning action gives the agent a way to keep the working set small as it follows those references.

Implementation Patterns

Separate Visible Context from Audit Trace

Deleting a chunk from the agent’s view should not delete it from logs. Keep a durable trace of all tool calls, retrieved chunk IDs, scores, pruning decisions, and final outputs. The visible context is optimized for the next model call; the audit trace is optimized for debugging, evaluation, and reproducibility.

Use Stable Chunk Identifiers

Pruning works only if chunks have stable IDs. The model should prune by ID, not by copying text spans. Stable identifiers also support deduplication, exclusion filters, reward attribution, and UI explanations showing why specific evidence was retained or discarded.

{
  "tool": "prune_chunks",
  "arguments": {
    "chunk_ids": ["legal_0421#p3", "legal_0421#p4"],
    "reason": "Same party name, wrong filing year; distractor for current clue."
  }
}

Return Evidence, Not Answers

For complex systems, keep the search agent’s contract narrow: return the best supporting documents and metadata. Let a downstream answer model handle synthesis. This makes evaluation cleaner because retrieval quality can be measured independently from answer-writing quality.

Performance Considerations

Common Pitfalls

Further Reading