Citations
When a chatbot answers from OCS collections, it can cite the files it used. The user sees numbered references in the answer, and a list at the end with each file's name and a download link. Citations are produced by pipeline LLM nodes with the generate_citations setting on. See the user guide for citations for the user's view, and Index Manager Classes for how the retrieval works.
Example
LLM response (before the build step):
The sky is blue <CIT 123 />. The grass is green <CIT 456 />.
Message after the build step:
The sky is blue [^1]. The grass is green [^2].
[^1]: [document_a.pdf](https://example.com/download/123)
[^2]: [source_b.txt](https://example.com/download/456)
Messaging channels such as Telegram and WhatsApp show [1] instead of [^1] (see section 4).
The citation chain
Citations are processed in four steps, from the LLM writing its response to the user seeing the answer:
- Generate (section 1): The LLM writes a citation wherever it uses information from a retrieved file.
- Collect (section 2): The output parser reads the citations and resolves them to cited files.
- Build (section 3): Each citation is replaced with a reference and the reference section is appended to the message.
- Render (section 4): The channel layer adapts the references to what the channel supports, such as plain
[1]markers or native file attachments.
References come only from <CIT file-id /> citations, so only local indexes produce them. Files cited through a remote index are covered in section 1 and section 2.
Terms
- Citation: the
<CIT file-id />text the LLM writes in its response, wherefile-idis the ID of aFile. - Cited files: the set of
Filerecords the response cites (cited_files). - Reference: the numbered footnote (
[^1]) that replaces a citation in the message. - Reference section: the list appended to the end of the message, with one line per reference. Each line has the reference number, the file's citation text and a download link. The citation text is the file name unless the file's metadata overrides it.
1. Citation Generation
A file is cited in one of two ways, depending on where the collection's index lives:
- Local index: the LLM writes the citations. The search tool returns the matching chunks, each labelled with the
file_idandfilenameof its file. Ifgenerate_citationsis on, the tool also tells the LLM to write a citation after each claim that uses that content, putting that chunk'sfile_idin the<CIT file-id />citation. These citations become references. - Remote index: the provider reports the citations. Currently, only OpenAI vector stores are supported. OpenAI's built-in file search has no citation instruction, so the LLM writes no citations. Cited files come only from provider annotations. They are attached to the chat but get no reference.
2. Collecting Cited Files
After the LLM responds, the node passes the response to an output parser, which returns the text plus cited_files. Anthropic has its own parser; every other provider uses the default parser.
| Scenario | Parser | cited_files come from |
Gets references? |
|---|---|---|---|
| 1. Local index, Anthropic | Anthropic parser | <CIT file-id /> citations in the text |
Yes |
| 2. Local index, other provider | Default parser | <CIT file-id /> citations in the text |
Yes |
| 3. Remote index, OpenAI | Default parser | Provider annotations | No |
Why Anthropic has its own parser. Anthropic's web search tool returns its sources as citations on the response's text blocks. The Anthropic parser adds them to the text as [title](url) links. They are links, not files, so they never appear in cited_files or the reference section.
Provider annotations are entries in the annotations list of a LangChain content block. For OpenAI, entries of type citation carry the file's external ID, which the default parser matches to files by File.external_id.
Rules for all scenarios:
- When the node's
generate_citationssetting is off,cited_filesis left empty. The node then removes any citations from the message instead of building references. - A file in
cited_filesis attached to the chat. It gets a reference only if its citation is in the text, because section 3 only numbers files that have one. This is why scenario 3 has no references.
3. Building the Reference Section
The pipeline LLM node builds the reference section after the output parser, when generate_citations is on. The result is the message that is saved and sent on.
- Citations are numbered in the order they first appear in the message.
- Repeat citations of the same file reuse its number.
- A citation whose file is not in
cited_filesis removed. This drops IDs the LLM made up. - The reference section is appended after a blank line, one line per reference in number order:
[^1]: [citation text](download link). The download link is the file's own URL if its metadata sets one, otherwise the platform's download URL for the session.
4. Final Rendering in Channels
How the references reach the user depends on the channel.
- Web and API channels return the saved markdown unchanged. The web interface renders the
[^1]footnotes itself. - Messaging channels (those that send the reply themselves) rewrite the message before sending:
[^1]becomes[1]throughout, because plain-text apps do not support footnotes.- Each reference line shows the citation text, followed by the download link only if the channel cannot send the file as an attachment.
- Voice replies skip the reference formatting. The URLs are taken out of the spoken text and sent as a separate text message.