Multi-agent context orchestration with LangGraph
How Context Refinery turns raw sources into clean, reranked, self-refined LLM context with a four-stage LangGraph pipeline.
Update, October 2026: Context Refinery is parked. Its README lists the known limits, for example that the refinement loop reruns the writer with the same inputs and keeps the last attempt.
The quality of an LLM's answer is capped by the quality of the context you hand it. Naive single-pass retrieval returns noisy, redundant or off-target chunks, and most pipelines never check their own work.
So I built Context Refinery, a desktop app (Tauri v2, Vue 3 and FastAPI) that runs a LangGraph pipeline to retrieve, rerank and refine context before it ever reaches the model.
Four stages
Context Refinery models retrieval as a LangGraph state machine with four stages:
- Intent analysis: work out what the query is actually asking for.
- Hybrid retrieval: ChromaDB dense vectors plus BM25 keyword search, merged with Reciprocal Rank Fusion (RRF).
- Cross-encoder reranking:
ms-marco-MiniLM-L-6-v2rescores the shortlist against the actual question. - Self-refinement: score the assembled context, and if it falls short, run the prompt writer again (up to three times).
Why hybrid retrieval and RRF
Dense vectors capture meaning; BM25 captures exact terms (error codes, function names, rare tokens). Each fails where the other shines. Reciprocal Rank Fusion merges the two ranked lists into one, so a result that's strong in either signal survives:
# Reciprocal Rank Fusion across dense + sparse rankings
def rrf(rankings, k=60):
scores = defaultdict(float)
for ranking in rankings: # e.g. [dense_hits, bm25_hits]
for rank, doc_id in enumerate(ranking):
scores[doc_id] += 1 / (k + rank)
return sorted(scores, key=scores.get, reverse=True)Why a cross-encoder
Bi-encoder retrieval is fast but approximate, because it embeds the query and each document separately. A cross-encoder reads the query and a candidate together and scores the pair directly. It's too slow to run over a whole corpus, but it's perfect for reranking a 20–50 candidate shortlist, where it sharply improves top-k precision.
Why LangGraph, and why Tauri
The refinement stage isn't a fixed path. It loops based on an eval score. A LangGraph state machine makes that conditional branching explicit and debuggable, unlike a nested chain. Wrapping it in a Tauri v2 desktop shell means the whole pipeline can run offline against local models (Llama or Mistral through Ollama), and no keys or documents leave the machine.
What “eval-gated” actually means
The default scorer is three custom local metrics: n-gram context grounding, type-token information density, and asymmetric budget utilization. They run with no API key so the offline path stays honest. DeepEval Faithfulness / Answer Relevancy (RAGAS as fallback) are an optional extra. They use those libraries' OpenAI-backed defaults, so they need an OpenAI key and pip install '.[eval]'. A Google or Anthropic key does not configure them. If the extra is missing, the local score stands.
Takeaways
Dense-only and keyword-only retrieval each miss things the other catches. A refinement loop gated by a score is cheap to try on a laptop, but I have not benchmarked it against a baseline, so I claim no accuracy gain. v0.1.0 is out under the MIT license, with Windows installers only.