Inline Citation
A small source chip placed inside a sentence that shows the page behind the claim on hover or focus.
Next.js streams a route in chunks, so the page shows up before every request finishes.
Wrap a slow part of the page in a Suspense boundary and React sends the fallback first. When the data resolves, the finished HTML streams into the same response and replaces the fallback, with no extra request from the browser. Server Components help here, because their code never ships to the browser.
A loading.tsx file does the same for a whole route segment. It all runs over chunked transfer encoding, so there is nothing extra to set up.
Installation
pnpm dlx shadcn@latest add @silkware/ai-inline-citationTo copy the pattern by hand, install the components it composes, then copy the pattern into components/ai-elements/inline-citation.tsx:
pnpm dlx shadcn@latest add @silkware/button @silkware/hover-card"use client"
import { Button } from "@/components/ui/button"import { HoverCard, HoverCardTrigger } from "@/components/ui/hover-card"
type Source = { url: string title: string description: string}
const sources = { streaming: { url: "https://nextjs.org/docs/app/getting-started/linking-and-navigating#streaming", title: "Linking and Navigating", description: "How the App Router streams a route to the browser.", }, suspense: { url: "https://react.dev/reference/react/Suspense", title: "<Suspense>", description: "Display a fallback until its children have finished loading.", }, serverComponents: { url: "https://react.dev/reference/rsc/server-components", title: "Server Components", description: "Components that render ahead of time, on the server.", }, loading: { url: "https://nextjs.org/docs/app/api-reference/file-conventions/loading", title: "loading.js", description: "Show an instant loading state for a route segment.", }, chunked: { url: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Transfer-Encoding", title: "Transfer-Encoding", description: "Send a response as a series of chunks.", },} satisfies Record<string, Source>
function hostname(url: string) { return new URL(url).hostname.replace(/^www\./, "")}
function Citation({ source }: { source: Source }) { const host = hostname(source.url)
return ( <HoverCardTrigger delay={10} closeDelay={100}> <Button variant="secondary" size="xs"> {host} </Button> <HoverCard className="flex w-64 flex-col gap-1"> <div className="font-medium">{source.title}</div> <p className="text-muted-foreground">{source.description}</p> <a href={source.url} target="_blank" rel="noreferrer" className="text-xs text-muted-foreground underline-offset-4 hover:underline" > {host} <span className="sr-only"> (opens in a new tab)</span> </a> </HoverCard> </HoverCardTrigger> )}
export function InlineCitationDemo() { return ( <div className="mx-auto flex max-w-sm flex-col gap-3 py-6 text-sm leading-relaxed"> <p> Next.js streams a route in chunks, so the page shows up before every request finishes. <Citation source={sources.streaming} /> </p> <p> Wrap a slow part of the page in a Suspense boundary and React sends the fallback first. When the data resolves, the finished HTML streams into the same response and replaces the fallback, with no extra request from the browser. <Citation source={sources.suspense} /> Server Components help here, because their code never ships to the browser.{" "} <Citation source={sources.serverComponents} /> </p> <p> A loading.tsx file does the same for a whole route segment.{" "} <Citation source={sources.loading} /> It all runs over chunked transfer encoding, so there is nothing extra to set up.{" "} <Citation source={sources.chunked} /> </p> </div> )}Usage
import { Button } from "@/components/ui/button"import { HoverCard, HoverCardTrigger } from "@/components/ui/hover-card"function Citation({ source }: { source: Source }) { const host = hostname(source.url)
return ( <HoverCardTrigger delay={10} closeDelay={100}> <Button variant="secondary" size="xs"> {host} </Button> <HoverCard className="flex w-64 flex-col gap-1"> <div className="font-medium">{source.title}</div> <p className="text-muted-foreground">{source.description}</p> <a href={source.url} target="_blank" rel="noreferrer"> {host} <span className="sr-only"> (opens in a new tab)</span> </a> </HoverCard> </HoverCardTrigger> )}Put <Citation /> at the end of the sentence it supports, as in the demo. The chip is a real button, so it can take focus. The card is a React Aria preview: it opens on hover, on keyboard focus and on long press, and unlike a tooltip it can hold a link that the person can reach. The delays (10 ms to open, 100 ms to close) let the pointer move from the chip into the card without it closing.
Examples
Hostname chips
The demo uses the page's hostname as the chip text. It is short and tells the reader where the claim comes from.
Numbered chips
With many claims in one paragraph, numbers keep the text readable. Give the chip an aria-label that names the source, because a number alone says nothing to a screen reader.
Cavenzo Metalli raised steel sheet to 4.20 per kg. The contract requires 30 days of notice, so the rise was not allowed to apply to orders placed before 1 February.
AI SDK
The AI SDK gives you the sources, not the positions: source-url parts arrive as separate parts of the message, and the model's text has no marker saying which sentence each one supports. Decide how you connect them:
- Ask the model to write markers such as
[1]in its answer, then split the text on them and render aCitationfor each marker, using the matchingsource-urlpart by order. - Or keep the sources in a list under the answer (see Sources) when the model cannot place them.
const sources = message.parts.filter((part) => part.type === "source-url")const text = getMessageText(message)
const pieces = text.split(/\[(\d+)\]/)
{pieces.map((piece, index) => index % 2 === 0 ? ( piece ) : ( <Citation key={index} source={sources[Number(piece) - 1]} /> ))}Guard the lookup: if the model writes a marker for a source that never arrived, sources[n] is undefined. Render the marker as plain text in that case. Enable sources on the route with toUIMessageStreamResponse({ sendSources: true }). While text streams, a marker can be half written ([1 before ]), so render citations only for complete markers.
The demo has fixed data and no stream, so it does not use createChat; use writer.sourceUrl in lib/ai.ts to script the parts when you build the real version.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus to the chip, which opens the card, then to the link in the card, then to the next chip. |
| Enter | Follows the link in the card when the link has focus. |
| Esc | Closes the card. |
The composed components provide:
- A focusable button for the chip and a popover that opens on hover and on focus, so keyboard users get the same card as pointer users.
- An interactive card, which can hold a link. Tab from the chip moves focus into the card, to the link.
You provide:
- A name for each chip. The hostname works as text, and a number needs an
aria-label. - A note that the link opens in a new tab, as in the demo.
- The source's title and a short description, so the card is useful without leaving the page.
Guidelines
When to use
- When an answer makes several claims from different sources and the person should be able to check each one.
- In running text, where a footnote list would send the reader away from the sentence.
When not to use
- For one source behind the whole answer. List it with Sources.
- For definitions or help text. Use a Tooltip or a Popover.
Content
- Place the chip after the punctuation of the sentence it supports.
- Keep the card to a title, one sentence and the link.
- One chip per claim. Repeating the same chip on every sentence adds noise.
Human in the loop
A citation is a promise that the source says this. The person must be able to open the page and check, so link to the exact page and never to a home page. If you cannot say which sentence a source supports, do not place a chip: list the source under the answer instead.
Cite the specific page that supports the sentence, with its title in the card.
Don’t attach a chip to a claim the source does not make. It is worse than no citation.
Composition
| Component | What it does here |
|---|---|
| Button | The chip, as a small secondary button. |
| Hover Card | HoverCardTrigger ties the chip to the card, and HoverCard is the card that shows the source. |
The pattern exports one component, InlineCitationDemo, which holds three paragraphs and a sources object of sample data. Citation and hostname are internal to the file: copy them to cite from your own text.