---
title: "LLM Citations: Make AI Cite Sources Accurately (and Verify)"
description: "Prompt an LLM to cite numbered sources, check every citation points at a real source, catch uncited claims, verify exact quotes against the source text, use Claude's native search_result citations, and what our OpenAI search logs show about which results get cited."
category: "guides"
url: https://keirolabs.cloud/blog/llm-citations
published: 2026-11-24
updated: 2026-11-24
---

# LLM citations: how to make a model cite sources accurately, and check them in code

## The 60-second version

**To get accurate citations from an LLM, number your sources in the prompt, ask for a `[n]` after every claim, and then check the citations in code.** Models cite wrongly in predictable ways, and most of them are cheap to catch:

| Failure | Example | How to catch it |
|---|---|---|
| Citation points nowhere | `[7]` when you gave 5 sources | Check every `[n]` is in range |
| Uncited claim | A sentence with no `[n]` | Flag sentences without a citation |
| Wrong source | `[2]` cited, but `[2]` doesn't say that | Ask for an exact quote and check it's in the source |
| Invented URL | A link that wasn't in the results | Only render URLs from your own source list |
| Answer from memory | Correct-looking but not from the sources | "Answer only from the sources" + quote checks |

**Or use a native citations feature,** such as Claude's `search_result` blocks, which return the exact cited text with each citation.

## How do I make an LLM cite sources?

**Give each source a number, and say exactly how to cite.** A prompt that works across models:

```python
def build_prompt(question: str, results: list[dict]) -> str:
    sources = "\n\n".join(f"[{i+1}] {r['title']}\n{r['url']}\n{r['snippet']}"
                          for i, r in enumerate(results))
    return f"""Answer the question using only the sources below.
Put the source number in square brackets after every sentence that states a fact, like [2].
If the sources don't answer the question, say so. Don't use outside knowledge.

Sources:
{sources}

Question: {question}"""
```

**Why each line is there:**

- **"Only the sources below"** stops the model blending in training data, which is usually where wrong claims come from.
- **"After every sentence"** gives you something to check. "Cite your sources" alone often gets one citation at the end.
- **"Say so"** gives the model a way out besides inventing an answer.
- **Numbers, not URLs.** Models copy numbers more reliably than long URLs. Map `[n]` back to URLs yourself, so the reader never sees an invented link.

## How do I check citations in code?

**Two checks catch most problems.** First, every citation must point at a real source, and every factual sentence must have one:

```python
import re

def check_citations(answer: str, sources: list[str]) -> dict:
    """Find citations that point nowhere and sentences with no citation."""
    cited = [int(n) for n in re.findall(r"\[(\d+)\]", answer)]
    bad = sorted({n for n in cited if not 1 <= n <= len(sources)})
    sentences = [s.strip() for s in re.split(r"(?<=[.!?])\s+", answer) if s.strip()]
    uncited = [s for s in sentences if not re.search(r"\[\d+\]", s)]
    unused = [i + 1 for i in range(len(sources)) if i + 1 not in cited]
    return {"bad_refs": bad, "uncited_sentences": uncited, "unused_sources": unused}
```

On an answer citing `[4]` with only 3 sources, plus one sentence with no citation, it returns:

```text
{'bad_refs': [4], 'uncited_sentences': ['It is the fastest Python ever.'], 'unused_sources': [3]}
```

**Second, check that the source actually says it.** Ask the model to return claims as JSON, each with a source number and an exact quote from that source, then check the quote is there:

```python
def norm(s: str) -> str:
    return re.sub(r"\s+", " ", s).strip().lower()

def check_quotes(claims: list[dict], sources: list[str]) -> list[dict]:
    """Each claim carries the source number and an exact quote. Return the ones whose quote isn't there."""
    failed = []
    for c in claims:
        n, quote = c["source"], c["quote"]
        if not 1 <= n <= len(sources) or norm(quote) not in norm(sources[n - 1]):
            failed.append(c)
    return failed
```

**A claim whose quote isn't in its source is either miscited or invented.** Retry it, drop it, or show it as unverified. The quote check needs the full source text, so run it against page content rather than 2-line snippets when accuracy matters.

**What to do with failures:**

- **Bad reference:** retry once with the error in the prompt ("Source [4] doesn't exist"), or remove the citation.
- **Uncited sentence:** fine for transitions and summaries; for anything with a number, a date or a name, retry or remove.
- **Failed quote:** the riskiest failure. Never show it as cited.

## Do LLM APIs have built-in citations?

**Claude does.** Return search results to Claude as `search_result` blocks inside a `tool_result`, with citations enabled:

```json
{
  "type": "search_result",
  "source": "https://docs.example.com/pricing",
  "title": "Pricing Plans",
  "content": [{"type": "text", "text": "The Starter plan costs $10 per user per month."}],
  "citations": {"enabled": true}
}
```

**Claude's text blocks then carry citations** with the `source`, the `title` and the exact `cited_text`, so you don't have to parse `[n]` markers. Citations are off by default, and they're all-or-nothing within a request. `document` blocks support the same setting. Built-in web search tools (Claude's, OpenAI's, Gemini's) also return citations, but only for searches they run themselves ([the Claude web search tool](/blog/claude-web-search-tool)).

**Native citations still need checks of your own.** They tell you which text the model *says* it used. They don't confirm that the claim follows from it.

## Which sources do LLMs actually cite?

**Not just the top results.** We logged 163 answers from three OpenAI models with web search on. On average each answer retrieved 13.2 results and cited 7.2 of them.

- **The median cited result was at rank 8.**
- **Only 28% of citations came from the top 3.**
- **43% came from below rank 10.**

**So retrieval depth matters.** If you pass only the top 3 results, the model can't cite what a broader search would have found. Pass 8 to 15 results as snippets, and let the model choose. The full study is in [grounding AI agents with web search](/blog/grounding-ai-agents-web-search).

## Checklist

- **Number the sources; ask for `[n]` after every factual sentence.**
- **"Answer only from the sources", and allow "the sources don't say".**
- **Render URLs from your own list, never from model output.**
- **Check every reference is in range, and flag uncited factual sentences.**
- **For important claims, require exact quotes and verify them against full page text.**
- **Use native citations where available, and still verify.**
- **Retrieve 8 to 15 results, not 3.**

To get numbered sources with full page text in one call, use Keirolabs `/search/content` (1.5 credits). New accounts get up to 2,000 free credits with no card ([get a key](https://platform.keirolabs.cloud/register)).
