Home Knowledge Base Citations and Attribution in RAG

Citations and Attribution in RAG

Why Citations Matter LLMs can hallucinate. Citations ground responses in source documents, enabling verification and building trust.

Citation Approaches

Inline Citations Reference sources within the response:

According to the documentation [1], the maximum batch size is 64.
The API rate limit is 1000 requests per minute [2].

[1] api-docs.md, Section 3.2
[2] rate-limits.md

Post-hoc Attribution After generation, find supporting sources:

def add_citations(response: str, sources: list) -> str:
    sentences = split_sentences(response)
    cited = []
    for sentence in sentences:
        source = find_best_source(sentence, sources)
        if source and similarity > threshold:
            cited.append(f"{sentence} [{source.id}]")
        else:
            cited.append(sentence)
    return " ".join(cited)

Grounded Generation Force LLM to cite while generating:

Generate a response using ONLY the provided sources.
For each claim, cite the source in [brackets].

Sources:
[1] doc1.txt: ...
[2] doc2.txt: ...

Question: ...
Answer (cite every fact):

Implementation Patterns

Chunk-Level Attribution

def generate_with_citations(query: str, chunks: list) -> str:
    context = "
".join([f"[{i}] {c.text}" for i, c in enumerate(chunks)])

    response = llm.generate(f"""
    Answer using the sources below. Cite each fact with [source number].

    Sources:
    {context}

    Question: {query}
    Answer:
    """)

    return response

Verification Check if citations are accurate:

def verify_citation(claim: str, source: str) -> bool:
    result = llm.generate(f"""
    Does this source support this claim?
    Claim: {claim}
    Source: {source}
    Answer (yes/no):
    """)
    return "yes" in result.lower()

Citation Metadata Include useful context:

citation = {
    "source_id": "doc123",
    "title": "API Documentation",
    "page": 5,
    "chunk_text": "...",
    "confidence": 0.92,
    "url": "https://..."
}

Best Practices

citationattributionsource

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.