config module
On this page
Overview
Defines a tree of @dataclass-decorated configuration objects, all wrapped under one root: ContextEngineConfig. None of these classes have any methods beyond what @dataclass generates automatically, except ContextEngineConfig, which adds three convenience properties for backward compatibility.
LengthCounter
LengthCounter = Callable[[str], int]
A type alias, not a class. Any function matching this signature (e.g. char_counter or the output of make_token_counter) can be passed wherever a LengthCounter is expected.
QueryAnalyzerConfig
@dataclass
class QueryAnalyzerConfig:
budget_low: int = 800
budget_medium: int = 2000
budget_high: int = 4000
custom_entities: List[str] = field(default_factory=list)
arabic_threshold: float = 0.3
No validation is performed on construction — e.g., setting budget_low > budget_high is allowed and will silently produce confusing budget assignments.
RetrieverConfig
@dataclass
class RetrieverConfig:
top_k: int = 20
vector_weight: float = 0.60
keyword_weight: float = 0.30
metadata_weight: float = 0.10
use_vector: bool = True
use_keyword: bool = True
use_metadata: bool = False
metadata_filters: Dict[str, str] = field(default_factory=dict)
min_vector_score: float = 0.0
FilterConfig
@dataclass
class FilterConfig:
dedup_method: str = "hash"
prefix_length: int = 100
semantic_sim_threshold: float = 0.92
min_chunk_length: int = 20
max_chunk_length: int = 4000
noise_patterns: List[str] = field(default_factory=lambda: [
r"^\s*$",
r"^[\.\-\_\*]{5,}$",
r"^\s*(page|صفحة)\s*\d+\s*$",
])
noise_patterns are matched with re.match (anchored at the start) in ChunkFilter, against the stripped chunk text. Patterns are compiled once at ChunkFilter.__init__ time.
RankingConfig
@dataclass
class RankingConfig:
weight_vector_score: float = 0.40
weight_keyword_score: float = 0.25
weight_source_quality: float = 0.20
weight_recency: float = 0.10
weight_position: float = 0.05
max_chunks_to_rank: int = 15
ComposerConfig
@dataclass
class ComposerConfig:
max_context_length: int = 2000
separator: str = "\n---\n"
template: str = "arabic"
include_scores: bool = False
include_metadata: bool = False
include_sources: bool = True
group_by_source: bool = False
summarize_long_chunks: bool = False
min_compression_chars: int = 50
Valid template values accepted by SmartContextComposer: "arabic", "english", "minimal", "structured". Passing any other string does not raise — SmartContextComposer.__init__ falls back to "arabic" via _TEMPLATES.get(self.cfg.template, _TEMPLATES["arabic"]).
GuardConfig
@dataclass
class GuardConfig:
enabled: bool = True
min_context_length: int = 10
max_context_length: int = 8000
check_empty: bool = True
check_relevance: bool = False
relevance_threshold: float = 0.10
check_source_coverage: bool = True
CacheConfig
@dataclass
class CacheConfig:
enabled: bool = True
max_size: int = 256
ttl_seconds: int = 300
ContextEngineConfig
@dataclass
class ContextEngineConfig:
query_analyzer: QueryAnalyzerConfig = field(default_factory=QueryAnalyzerConfig)
retriever: RetrieverConfig = field(default_factory=RetrieverConfig)
filter_cfg: FilterConfig = field(default_factory=FilterConfig)
ranking: RankingConfig = field(default_factory=RankingConfig)
composer: ComposerConfig = field(default_factory=ComposerConfig)
guard: GuardConfig = field(default_factory=GuardConfig)
cache: CacheConfig = field(default_factory=CacheConfig)
language: str = "auto"
enable_adaptive: bool = True
multi_turn: bool = True
Properties
max_context_length
@property
def max_context_length(self) -> int: ...
@max_context_length.setter
def max_context_length(self, v: int): ...
Proxies directly to self.composer.max_context_length. Reading or writing config.max_context_length is exactly equivalent to config.composer.max_context_length.
separator (read-only)
@property
def separator(self) -> str: ...
Proxies to self.composer.separator. There is no setter — attempting config.separator = "..." raises AttributeError: can't set attribute.
template (read-only)
@property
def template(self) -> str: ...
Proxies to self.composer.template. Also has no setter.
ContextConfig
ContextConfig = ContextEngineConfig
A plain assignment, not a subclass. ContextConfig is ContextEngineConfig evaluates to True. Both names construct identical objects.
Examples
Basic — defaults:
from zerda.context import ContextEngineConfig
cfg = ContextEngineConfig()
Intermediate — nested overrides:
cfg = ContextEngineConfig()
cfg.retriever.top_k = 50
cfg.guard.check_relevance = True
cfg.composer.template = "structured"
Advanced — fully custom construction:
from zerda.context import (
ContextEngineConfig, RetrieverConfig, ComposerConfig, GuardConfig,
)
cfg = ContextEngineConfig(
retriever = RetrieverConfig(top_k=30, use_metadata=True),
composer = ComposerConfig(template="english", include_scores=True),
guard = GuardConfig(max_context_length=12000),
)
Error handling — read-only property:
cfg = ContextEngineConfig()
try:
cfg.template = "minimal" # raises AttributeError — use cfg.composer.template instead
except AttributeError as e:
print(e)
Production usage — via legacy proxy property:
cfg = ContextEngineConfig()
cfg.max_context_length = 6000 # equivalent to cfg.composer.max_context_length = 6000
assert cfg.composer.max_context_length == 6000
Related
- Configuration Reference — semantic meaning of every field, plus which fields are documented-but-unused
modelsengine