Skip to main content
This cookbook takes an answer from any model and renders it with numbered citations. Each number points to the exact source text behind it. Running this cookbook against the API prints:

1. Generate an answer

Use whatever model you already use. Keep the exact answer string you show to the user.

2. Attribute the answer

Results come back in answer order. If 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:
In a web UI, render each cited phrase as a button. On click, use 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 one document and record where each one starts. Map each source.start back to its document:
Use the same joined document for generation and attribution, so the model and TokenPath see identical text.