Build a citation system by assigning unique IDs to every chunk during ingestion, instructing the LLM to embed these IDs in its response, and then mapping citations back to source documents using the chunk metadata.
A robust citation system relies on persistent chunk identifiers assigned during document ingestion. Each chunk receives a unique ID (e.g., doc_{doc_id}_{chunk_index}) and carries metadata including source filename, page number, and heading path. The prompt instructs the LLM to embed these chunk IDs as inline citations (e.g., [a3z1]) wherever it uses information from that chunk. After generation, the system extracts citations and validates that each cited claim is actually present in the referenced chunk, using embedding similarity or exact string matching. This creates a verifiable chain from the final answer back to the original source material.
Use short, unique chunk IDs (e.g., 4-character codes) to minimize token consumption
Store chunk metadata (page number, heading path) to enable document navigation
Validate citations after generation to reject hallucinated references
Inline citations create clickable badges in UI for source verification