1. Generate an answer
Use whatever model you already use. Keep the exact answer string you show to the user.2. Attribute the answer
links is empty, render the answer without citations.
3. Split the answer into segments
Walk the answer once. Text between links stays plain, and each link becomes a numbered citation:4. Render
Put each marker after its phrase, and list the sources underneath:source.start and source.end to highlight the evidence in the document pane. Here is the same idea in JavaScript. It relies on the default utf-16 encoding, which matches String.prototype.slice:
5. Rank or style by confidence
source.confidence is the strength of each link, from 0 to 1. Use it to order a source list or to style strong and weak links differently:
confidence describes how strongly the phrase is tied to the source text. It does not grade whether the answer is correct.
Multiple documents
To cite across several retrieved documents, join them into onedocument and record where each one starts. Map each source.start back to its document:
document for generation and attribution, so the model and TokenPath see identical text.