Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1ay1 Agentty Context Injection

From Leeroopedia
Knowledge Sources
Domains RAG, LLM_Inference
Last Updated 2026-10-28 12:00 GMT

Overview

Process of injecting retrieved, compressed, source-tagged context snippets into an agent's tool interface so the model can ground its responses in real file paths and line numbers without the snippets consuming unbounded context.

Description

After retrieval, diversification, and compression, the results are code/documentation snippets with provenance (file path, line numbers). These must be delivered to the model in a form it can use: source-tagged, byte-budgeted, and accessible through the agent's tool interface (e.g., search_code and search_docs tools).

The injection surface is a ToolOutput with snippets formatted for model consumption — each snippet carries its file path and line range so the model can cite and navigate. A byte budget ensures the total injected context stays bounded.

Usage

Use this principle when:

  • An agent needs to ground responses in retrieved code or documentation
  • Retrieved snippets must carry provenance (file, line numbers) for citation
  • Context injection must stay within a byte budget

Theoretical Basis

inject:[Snippet]×byte_budget→ToolOutput

# Abstract algorithm (NOT real implementation)
def search_code(query, engine):
    results = engine.retrieve(query)          # hybrid retrieval
    results = rerank(query, results)           # deterministic reranking
    results = mmr(results, lambda=0.7)         # diversification
    results = compress(results, byte_budget)   # sentence-level trim
    # Format as ToolOutput with source tags
    return ToolOutput(text=format_snippets(results))
    # Each snippet: "path/to/file.py:L42-80\n<code>"

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment