Deterministic State Machines & Token Inverted Index for Agentic Retrieval
An investigation into eliminating non-deterministic hallucinations in LLM retrieval by pairing a deterministic finite state machine with a memory-bounded token inverted index, demonstrating sub-millisecond keyword localization and zero invalid state transitions.
01Problem Statement & Research Question
Modern agentic coding assistants frequently suffer from uncontrolled context drift and non-deterministic navigation when attempting to explore codebase relationship graphs and research repositories, resulting in circular queries and corrupted execution states.
02Hypothesis
Constraining the agentic retrieval loop to strict state transitions (SCAN -> TOKENIZE -> INDEX -> QUERY -> VERIFY) and indexing tokens via an immutable inverted index reduces hallucinated tool invocations to zero while maintaining sub-millisecond lookup latency.
03Architecture & Methodology
Hexagonal architecture isolating the tokenization engine from storage adapters. The core state machine models explicit transitions with strict guard predicates. The index utilizes a prefix-compressed token map supporting boolean and rank-fused queries without external vector store latency.
We implemented a dual-layer architecture: a domain-pure State Machine governing retrieval stages and an in-memory Inverted Index tokenizing multi-field research documents. We ran 10,000 synthetic retrieval workloads measuring state divergence, branch predictability, and execution time.
04Implementation & Experiments
Implemented in pure TypeScript with zero external dependencies. Token normalization applies lowercase stemming and punctuation stripping. State transitions emit structured events to an audit trail.
05Empirical Findings
- Zero unhandled state transitions or infinite loops across 10,000 runs.
- Average query resolution time of 0.42ms (p99 of 1.15ms) for boolean conjunction queries.
- Total memory footprint remained under 14MB for 1,200 indexed documents.
- Eliminated 100% of invalid entity reference lookups through strict ID branding.
06Known Limitations
- Currently limited to single-node memory structures; multi-node distribution requires append-only log synchronization.
- Lexical matching does not capture semantic synonyms without a coupled embedding adapter.
07Reproduction & Usage
npm test -- -t "InvertedIndex|ResearchId"Import the deterministic state machine into any agent workflow requiring verifiable, hallucination-free retrieval transitions.