Citations That Survive a TypeScript Refactor
dev.to·
Retrieval-Augmented Generation (RAG) systems frequently suffer from brittle citation structures that break when source documents are re-chunked or updated over time. This article introduces a TypeScript type architecture designed to create stable, refactor-surviving source references for LLM responses. By defining explicit data structures like SourceRef—which captures stable document IDs, content SHA-256 hashes, character offsets, and document revision versions—applications can securely track exact source spans. The implementation leverages non-empty tuple types like [SourceRef, ...SourceRef[]] to enforce at compile time that generated claims or prose spans always carry at least one valid source citation. For systems architects and backend engineers implementing RAG pipelines in Node.js or TypeScript, this structural pattern offers a robust way to ensure data provenance and auditability. Enforcing non-empty source tuples directly within your domain types prevents ungrounded responses from reaching downstream consumers, elevating reliability and maintainability across complex AI knowledge systems.