Embedding Algebra¶
The embedding algebra is Lethe's quantitative control plane. It replaces prompt-engineering-based control (telling the LLM "you have 5 iterations left") with vector-space computations that the LLM never observes. All numeric decisions --- which task to work on, how much thinking budget to allocate, when to terminate, how to compose context --- flow through this algebra.
The system uses an instruction-tuned embedding model (default dimensionality 1024, configurable via model_dimensions). These vectors support asymmetric prefixes: document embeddings (embed_doc) and query embeddings (embed_query) occupy related but distinct regions of the embedding space, which affects how similarity thresholds are calibrated.
Architectural Role¶
The embedding algebra occupies a unique position in the architecture. It is consumed by both the agent loop (loop/iterate.py) and the research engine (search/engine.py), using the same mathematical functions but different configuration parameters. This symmetry is deliberate: both loops solve the same fundamental problem (explore an information space until convergence) at different scales.
flowchart TB
EMBED["embed/<br/>shared algebra"]
AGENT["Agent loop<br/>loop/iterate.py"]
RESEARCH["Research engine<br/>search/engine.py"]
AGENT_CFG["EmbedConfig +<br/>module constants<br/>(convergence_weights)"]
SEARCH_CFG["SearchConfig<br/>convergence_weights"]
EMBED --> AGENT
EMBED --> RESEARCH
AGENT --- AGENT_CFG
RESEARCH --- SEARCH_CFG
The algebra never touches the graph store directly. It receives pre-extracted numpy arrays from the caller and returns scalar signals or transformed vectors. This boundary (principle P8 in the enforced import rules) keeps the algebra pure, testable, and reusable.
Signal Pipeline¶
Each iteration, compute_iteration_signals() produces an IterationSignals dataclass containing all control-plane state. Signals are computed at the end of iteration N (after graph mutations) and consumed at the start of iteration N+1 (for task selection, context weighting, thinking budget, and termination \(\psi\)). This one-iteration delay is intentional: the orchestrator needs a complete, post-mutation graph snapshot to compute meaningful signals, and those signals drive the next iteration's decisions.
The pipeline has four stages:
flowchart LR
S1["Stage 1<br/>Spectral Analysis<br/>SharedSVD"]
S2["Stage 2<br/>Goal Alignment<br/>GA blend"]
S3["Stage 3<br/>Temporal Tracking<br/>EMA trackers"]
S4["Stage 4<br/>Thinking Budget<br/>(novelty-driven)"]
OUT["IterationSignals"]
S1 --> S2 --> S3 --> S4 --> OUT
Stage 1: Spectral Analysis¶
A shared SVD decomposition of the evidence embedding matrix forms the foundation. The evidence matrix is mean-centered, then decomposed via truncated SVD. The resulting SharedSVD object carries:
- Singular values \(\sigma_i\) --- the energy distribution across principal components
- Right singular vectors \(V^T\) --- the evidence subspace basis (k directions of maximum variance)
- Mean embedding --- the evidence centroid before centering
- Eigengap --- \((\sigma_0 - \sigma_1) / \sigma_0\), the spectral decisiveness
The eigengap is a leading indicator of convergence. A large eigengap means the evidence is concentrating around a dominant direction, predicting fast subspace convergence. This connection to convergence rate comes from spectral graph theory: the ratio \((\sigma_0 - \sigma_1) / \sigma_0\) predicts how quickly iterative methods converge to the dominant subspace (Xu and Gao, IJCAI 2018).
The SharedSVD object is carried forward between iterations for Grassmannian chordal distance computation --- measuring how much the evidence subspace rotated since the last iteration. The Grassmannian \(\text{Gr}(k, D)\) is the manifold of all \(k\)-dimensional subspaces of \(\mathbb{R}^D\).
Lethe uses the chordal distance between consecutive subspaces rather than geodesic distance: given orthonormal bases \(V_{\text{prev}}\) and \(V_{\text{curr}}\), compute the SVD of \(V_{\text{prev}} V_{\text{curr}}^T\) to get cosines of principal angles \(\theta_i\), then:
The result is normalized to \([0, 1]\). This requires one small SVD (of the \(k \times k\) product, not the full embedding matrix), achieving significant speedup over geodesic distance while remaining metrically equivalent for small angles (Ramirez et al., 2024).
When evidence is insufficient (fewer items than the configured minimum), the algebra returns EMPTY_SVD, an algebraic zero that participates correctly in all downstream computations: coverage returns zero, eigengap returns zero, subspace distance returns maximum. This eliminates Optional types from the signal chain entirely.
Stage 2: Goal Alignment¶
Goal alignment measures how well the current state serves the user's original objective. It blends two sources:
- Task-level GA: cosine similarity between the current task embedding and the goal query embedding, affine-mapped from \([-1,1]\) to \([0,1]\) via \(0.5(1 + \cos\theta)\).
- Evidence-level GA: per-evidence cosine similarities are affine-mapped from \([-1,1]\) to \([0,1]\) via \(0.5(1 + \cos\theta)\), then aggregated using attention-weighted mean (strength-weighted softmax over top evidence). The affine map ensures anti-aligned evidence contributes near-zero rather than introducing a hard kink at zero.
The two are combined through a confidence ramp:
where \(\text{ga\_peak}\) is the attention-weighted evidence GA when evidence exists, or the task-level GA otherwise. The confidence function \(1 - e^{-n}\) reaches 0.63 with one evidence item and 0.95 with three. With zero evidence, \(\text{ga\_conf} = 0\), so the signal correctly reports zero alignment (no data yet).
Stage 3: Temporal Tracking¶
State embedding. A recency-weighted centroid combines evidence embeddings, task embedding, and active task embeddings into a single vector representing the session's current focus. Evidence weights use exponential linspace (more recent evidence weighted higher), blended with the current task embedding. The state embedding is the input to cross-iteration stability.
Stability. Cosine similarity between consecutive state embeddings, affine-mapped from \([-1,1]\) to \([0,1]\) to distinguish anti-correlation (oscillation, near 0) from orthogonal drift (0.5) from convergence (near 1).
Three EMA (exponential moving average) trackers maintain temporal state:
- Stability EMA (\(\alpha = 0.3\)) --- smoothed state embedding drift, gated by
ev_confidence * prior_confidence - Residual magnitude EMA (\(\alpha = 0.3\)) --- chord distance from evidence centroid to goal, gated by
ev_confidence - Eigengap EMA (\(\alpha = 0.3\)) --- spectral decisiveness, gated by
svd_confidence
Each tracker uses a confidence-weighted update:
Confidence ramps (all continuous, zero branching):
| Ramp | Formula | Purpose |
|---|---|---|
ev_confidence |
\(1 - e^{-n_{\text{ev}}/2}\) | Evidence count trust |
prior_confidence |
\(1 - e^{-\text{iters}}\) | Iteration count trust |
svd_confidence |
\(1 - e^{-n_{\text{ev}}/\text{min\_evidence}}\) | SVD data sufficiency |
When confidence is zero (no evidence, first iteration), the tracker holds its previous value. As confidence grows, the tracker responds more to observations. This eliminates "warm-up period" boolean flags entirely.
Stage 4: Thinking Budget¶
The thinking budget determines how many reasoning tokens the LLM receives, driven by the previous iteration's novelty signal:
High novelty (unfamiliar territory) allocates more reasoning budget; low novelty (diminishing returns) reduces it, encouraging synthesis over exploration. This replaces explicit exploration/exploitation switching with a smooth, single-variable interpolation.
Smooth Numeric Primitives¶
The embed/smooth.py module provides the C\(^{\infty}\) building blocks used throughout the algebra:
soft_gate¶
A tanh-based sigmoid gate centered at threshold:
Replaces if x > threshold throughout the codebase. Temperature controls the transition width: at \(T = 10\), the 90%-to-10% transition spans approximately 0.44 around the threshold. The tanh formulation avoids overflow guards needed by the logistic sigmoid.
budget_horizon¶
The exponential budget curve shaping pressure over time:
This curve has \(c(0) = 0\) and \(c(1) = 1\), with steepness controlled by \(\alpha\). The function default is \(\alpha = 5.0\); the agent loop passes \(\alpha = 4.0\) from TerminationConfig.budget_alpha. At \(\alpha = 5\): \(c(0.5) \approx 0.076\), \(c(0.8) \approx 0.33\), \(c(0.95) \approx 0.78\) --- the system operates at negligible pressure through its first half, then ramps steeply.
clamp01¶
Hard projection to \([0, 1]\): \(\text{clamp}_{01}(x) = \max(0, \min(1, x))\). Used where gradient flow is not needed (signal post-processing, not loss computation). A vectorized variant clamp01_vec wraps np.clip for batch operations.
Convergence Score¶
The convergence score is a weighted blend of three signals that determines when the system has gathered sufficient evidence:
The temporal signal is context-dependent: the agent loop uses centroid stability (has the evidence settled?), while the research engine uses information-gain saturation \((1 - \overline{\text{IG}})\) (are new pages still useful?). The two callers pass different weights:
| Signal | Agent loop | Research | Interpretation |
|---|---|---|---|
| Goal alignment | 0.30 | 0.20 | Cross-prefix cosine; best-evidence proximity to goal |
| Temporal | 0.70 | 0.40 | Stability (agent) / IG saturation (research) --- primary indicator |
| 1 - reconstruction error | 0.00 | 0.40 | Coverage; degenerate for the agent loop (doc-SVD ⊥ query), \(1-\overline{\text{GA}}\) breadth for research |
Two earlier signals were removed after audit: eigengap (spectral decisiveness, near-zero for broad topics) and residual trend (directional centroid motion, structurally ≈0 once goal-ranked evidence accumulates — a constant offset already guarded by iteration readiness). Both consumed computation for no discriminative value.
The convergence score feeds into the budget manager's termination potential \(\psi\). Termination is not a simple threshold on the raw score; \(\psi\) is a complement-product of convergence signal and pressure urgency (see Agent Loop: Termination). Either strong convergence or heavy budget pressure can trigger termination, but neither alone at moderate levels.
Warp Toward Goal¶
As budget pressure increases, the system shifts from exploration to exploitation. Rather than switching modes discretely (a common failure point in multi-agent systems), warp_toward_goal performs a geodesic rotation on the unit hypersphere \(S^{D-1}\). This is a Riemannian exponential map: the natural generalization of straight-line interpolation to curved manifolds (Absil et al., 2008). By operating on the sphere rather than in flat Euclidean space, the rotation preserves the unit-norm constraint and avoids the "inward chord dip" that linear interpolation would produce.
where \(\theta = \text{pressure} \times 0.5\) radians and \(\hat{\mathbf{t}}\) is the unit tangent vector from \(\mathbf{e}\) toward the warp direction. The warp direction itself incorporates the "gap" --- the least-covered dimensions of the goal that the evidence subspace has not yet reached.
At zero pressure, this is an identity operation. At maximum pressure, it rotates embeddings up to 0.5 radians (~28 degrees) toward the goal, biasing task selection toward filling coverage gaps rather than exploring new directions.
flowchart LR
EMB["Task embedding<br/>on S^(D-1)"]
WARP["warp_toward_goal()<br/>geodesic rotation"]
RESULT["Warped embedding<br/>biased toward gap"]
PRESSURE["Budget pressure<br/>θ = p × 0.5 rad"]
GAP["Gap direction<br/>least-covered<br/>goal dimensions"]
EMB --> WARP --> RESULT
PRESSURE --> WARP
GAP --> WARP
Module Organization¶
| Module | Category | Operations |
|---|---|---|
static_ops.py |
Pure vector | unit_vector, cosine_similarity, goal_alignment, normalize_cosine |
signals.py |
Signals + Temporal | compute_iteration_signals, EMATracker, SignalInputs (composes all above) |
batch_ops.py |
Set-level | novelty, batch_strengths, goal_projected_gap |
spectral.py |
Spectral | SharedSVD, compute_evidence_svd, convergence_score, warp_toward_goal |
smooth.py |
Primitives | soft_gate, soft_gate_vec, budget_horizon, clamp01, clamp01_vec, zeros_f32 |
client.py |
I/O | HttpEmbedClient (llama.cpp HTTP) |
llm/embed_text.py |
I/O | LLM-assisted embedding projection (outside embed/ package) |
Embedding Projection¶
Raw content text (task descriptions, evidence) embeds poorly with instruction-tuned embedding models. Common problems include:
- Length collapse --- short descriptions lose information in high-dimensional space
- Anisotropy --- LLM-generated text concentrates in narrow cones
- Prefix mismatch --- content written for human readers does not match the model's training distribution
The embed_text module addresses this by having the LLM rewrite content into an embedding-optimized projection before embedding. The LLM receives the raw text and produces a version designed for the specific embedding model (Qwen3-Embedding-4B), following instruction formats that the model was trained on.
This projection is mandatory on every node creation. The embed_text field is what gets embedded; the original content is preserved separately for serialization in DIN-Read format. The embedding algebra's thresholds are configured in EmbedConfig.
Related Work¶
The embedding algebra draws on techniques from differential geometry, spectral analysis, and signal processing. Several of these techniques have seen renewed attention in the context of LLM-based retrieval and reasoning systems:
Grassmannian subspace tracking has a rich history in signal processing. The GROUSE algorithm (Balzano et al., 2010) demonstrated efficient rank-one updates on the Grassmannian manifold. More recently, the GREAT algorithm (Ramirez et al., 2024) established convergence certificates for online subspace tracking with bounded noise, and GeRoST (2025) extended this with min-max robustness guarantees. Lethe uses the simpler chordal distance metric \(\|\mathbf{U}_t \mathbf{U}_t^T - \mathbf{U}_{t-1} \mathbf{U}_{t-1}^T\|_F\) rather than geodesic distance, trading geometric exactness for a 5x computational speedup (matrix multiplications vs. SVD-based logarithmic maps).
Hyperspherical embeddings for exploration-exploitation have appeared in recommendation systems (vMF-exp, Bendada et al., 2025) and controllable generation (HEART, 2025). Lethe's geodesic warping toward the goal is conceptually similar to HEART's position-aware geodesic rotation, but applied to the task selection problem rather than text generation.
Spectral convergence indicators using eigengap analysis originate in random matrix theory and have been applied to clustering (Xu and Gao, IJCAI 2018). Lethe applies this to evidence convergence: a growing eigengap indicates the evidence is concentrating around a dominant direction, predicting that further research will yield diminishing returns.
References¶
- Absil, P.-A., Mahony, R., and Sepulchre, R. Optimization Algorithms on Matrix Manifolds. Princeton University Press, 2008 --- Riemannian exp/log maps for geodesic operations on \(S^{D-1}\). Foundation for
warp_toward_goaland all hypersphere computations. - Xu, J., and Gao, J. "Eigengap Convergence Rate." IJCAI, 2018 --- Eigengap \((\sigma_0 - \sigma_1)/\sigma_0\) as a predictor of subspace convergence rate in iterative methods.
- Ramirez, D., et al. "Subspace Tracking with Dynamical Models on the Grassmannian." 2024 --- Chordal distance via principal angle SVD for subspace tracking; metrically equivalent to geodesic for small angles.
- Balzano, L., Nowak, R., and Recht, B. "Online Identification and Tracking of Subspaces from Highly Incomplete Information." Allerton, 2010 --- GROUSE algorithm: Grassmannian rank-one updates with convergence guarantees for streaming subspace estimation. Foundation for interpreting Lethe's SharedSVD chordal distance as a convergence diagnostic.
- Sasfi, A., Padoan, A., Markovsky, I., and Dörfler, F. "GREAT: Online Grassmannian Tracking." IEEE Trans. Automatic Control, 2025 --- Exponential convergence certificates for online subspace tracking under bounded noise and drift. Strengthens the theoretical basis for Lethe's chordal-distance stability signal.
- Bharadwaj, S., Mishra, B., et al. "GeRoST: Min-Max Grassmannian Optimization for Online Subspace Tracking." 2026 --- Extends GREAT with min-max robustness and explicit spectral-gap conditions (eigengap at index \(k\)). Supports Lethe's composite eigengap + chordal convergence signal.
- Klicpera, J., Bojchevski, A., and Gunnemann, S. "Predict then Propagate." ICLR, 2019 --- Decoupled PPR propagation informing goal alignment's interaction with graph-based context expansion.
- Miolane, N., et al. "Geomstats: A Python Package for Riemannian Geometry in Machine Learning." JMLR, 2020 --- Reference implementation for differential geometry on manifolds.
- Lin, H., and Bilmes, J. "Multi-document Summarization via Budgeted Maximization of Submodular Functions." NAACL, 2010 --- Submodular coverage framework underlying the reconstruction error signal.