Architecture
On this page
Overview
zerda.context is built around one orchestrator, ContextEngine, which wires together six pluggable strategy components plus two cross-cutting concerns (caching, adaptive learning). Every component implements an abstract interface defined in strategy.py, which means any stage can be swapped out for a custom implementation without touching the engine's orchestration logic.
The run() pipeline, step by step
This is the exact sequence executed by ContextEngine.run() (see api/engine), in source order:
Stage responsibilities
[!NOTE] What
AdaptiveLearningSystem._auto_tuneactually does Reading the implementation directly:_auto_tune()only logs a warning if the guard failure rate exceeds 30%, and logs an info message if average response time exceeds 2000ms suggesting the caller enable caching. It does not automatically change anyContextEngineConfigvalue. The budget adjustment that does take effect is insuggest_budget(), which is called everyrun(), independent of_auto_tune().
Component substitution points
Every stage's default implementation can be replaced by passing an alternative to ContextEngine.__init__:
engine = ContextEngine(
config = my_config,
query_analyzer = MyLLMAnalyzer(), # implements QueryAnalyzerStrategy
retriever = MyVectorStoreRetriever(), # a HybridRetriever instance, or any object exposing .retrieve()
chunk_filter = MyCustomFilter(), # implements ChunkFilterStrategy
ranker = MyCustomRanker(), # implements ChunkRankingStrategy
composer = MyCustomComposer(), # implements ContextComposerStrategy
guard = MyHallucinationGuard(), # implements ContextGuardStrategy
budget_allocator = MyAllocator(), # implements BudgetAllocationStrategy
length_counter = make_token_counter(my_tokenizer), # swap chars for tokens
)
This is taken verbatim from the "advanced use case" example in the ContextEngine class docstring.
Class relationships
[!NOTE]
VectorDatabaseBase/DocumentChunkcome from sibling packagesVectorDatabaseBase(and itsChromaVectorDatabase/FAISSVectorDatabase/PineconeVectorDatabasesubclasses) live inzerda.vector_database;DocumentChunk/ChunkMetadatalive inzerda.chunks.context.retrieveronly imports them underTYPE_CHECKING, so importingzerda.contextnever requires either package (or their optional deps) to be installed — only actually usingvector_db=does. Seeapi/retriever.md.
Data flow types
Multi-turn conversation flow
ConversationHistory (see api/models) is created automatically by ContextEngine.__init__ when config.multi_turn is True (the default), with a fresh session_id of the form session_{unix_timestamp}. Each call to run() with record_turn=True (the default) appends a ConversationTurn. When use_history=True (the default) and there is prior history, the next query is rewritten before analysis as:
[Previous conversation:
Q: <previous query>
A: <previous answer>]
Current question: <current query>
This enriched string is what gets analyzed and retrieved against — but note the original query (not the enriched one) is what gets recorded back into history and returned in ContextResult.query_analysis.original_query is actually the enriched string, since analysis = self._analyzer.analyze(effective_query). See the implementation note in api/engine for the exact distinction.
Where the code diverges from typical RAG-library architecture
- No LLM calls anywhere. This library never calls an LLM. Summarization (
summarize_long_chunksinComposerConfig) is a configuration flag that exists but, on inspection ofpipeline.py, is not read or acted upon anywhere inSmartContextComposer. It is effectively a no-op flag today. - No persistence.
ContextCacheis in-process memory only (a plaindict+deque); restarting the process clears it. - No async support. Every method in the package is synchronous; there is no
async defanywhere in the source.