CTX: LLM-free Code Context Loader
Every AI coding agent faces the same problem: what context to include in the prompt?
Naive approach: dump the whole codebase → 100% of token budget gone.
Smart approach: use BM25 search → misses dependency files, import chains, recent edits.
I built CTX to solve this with a deterministic, zero-LLM approach.
How It Works
CTX classifies every query into one of 4 trigger types and routes it to the right retrieval strategy:
from ctx import CTXRetriever
retriever = CTXRetriever(project_path=".")
result = retriever.retrieve("Where is AuthMiddleware defined?")
# → Trigger: EXPLICIT_SYMBOL → symbol index lookup → returns 2 files
Trigger types:
-
EXPLICIT_SYMBOL→ exact symbol lookup in structural index -
SEMANTIC_CONCEPT→ BM25 hybrid blend -
TEMPORAL_HISTORY→ git log + recently modified files -
IMPLICIT_CONTEXT→ import graph traversal (finds what the target imports)
Benchmark Results
87 queries, 29 docs:
| Metric | CTX | BM25 | Dense TF-IDF |
|---|---|---|---|
| R@3 | 0.862 | 0.667 | 0.690 |
| R@5 | 0.954 | 0.839 | 0.805 |
| MRR | 0.795 | 0.611 | 0.563 |
| Token usage | 5.2% | 100% | 100% |
For dependency queries specifically: R@5 = 1.000 (BM25 = 0.400).
COIR Benchmark (CodeSearchNet Python)
| Metric | CTX | BM25 |
|---|---|---|
| R@1 | 0.720 | — |
| R@5 | 0.740 | — |
| MRR | 0.728 | — |
Install
pip install ctx-retriever
GitHub: jaytoone/CTX
The key insight: query intent classification before retrieval beats generic search for code context loading. The trigger classifier adds ~0ms overhead (pure regex + structural index).
Would love feedback from anyone building RAG pipelines for code.
Top comments (0)