Skip to main content

Citations

Citations link spans of an assistant response back to the sources that support them. Turn on enableCitations when you create or resume a session, then read the citations payload on assistant.message events to render footnotes, source lists, or inline links.

警告

Citations are experimental. The option name, event payload, and provider coverage can change in a future release.

How citations work

Citations are produced by the model provider, not by the SDK. The flow has three parts:

  1. Your application supplies citable material, such as a document attachment or a tool result that carries source content.
  2. The runtime marks that material as citable on the wire when enableCitations is on. For Anthropic models, file attachments are sent as document blocks with citations enabled.
  3. The model returns citation metadata, and the runtime normalizes it into a provider-agnostic citations object on the final assistant.message event.

Provider support is limited. The provider field on each source records where the citation came from:

Provider valueMeaning
anthropicCitation produced by an Anthropic (Claude) model response
openaiCitation produced by an OpenAI model response
clientCitation synthesized by the runtime from tool output

メモ

Turning on enableCitations does not guarantee that a response contains citations. Models emit them only when the response is grounded in citable source material. Always treat the citations field as optional.

Enable citations on a session

Set the option on session create, and set it again on resume if you want citations after a restart.

コード言語 navigation

TypeScript
const session = await client.createSession({
    onPermissionRequest: approveAll,
    enableCitations: true,
});

const resumed = await client.resumeSession(session.sessionId, {
    onPermissionRequest: approveAll,
    enableCitations: true,
});

Read citations from assistant messages

Citations arrive on the final assistant.message event, not on assistant.message_delta events. Wait for the final message before you render source markers.

コード言語 navigation

TypeScript
session.on((event) => {
    if (event.type !== "assistant.message" || !event.data.citations) {
        return;
    }

    const { sources, spans } = event.data.citations;
    const sourceById = new Map(sources.map((source) => [source.id, source]));

    for (const span of spans) {
        const quoted = event.data.content.slice(span.startIndex, span.endIndex);
        for (const reference of span.references) {
            const source = sourceById.get(reference.sourceId);
            const label = source?.title ?? source?.url ?? source?.path ?? source?.id;
            console.log(`"${quoted}" — ${label}`);
        }
    }
});

Citation payload reference

The citations object separates deduplicated sources from the spans that reference them, so a source cited five times appears once in sources.

TypeFieldDescription
CitationssourcesDeduplicated set of sources referenced by the citation spans
CitationsspansSpans of generated text annotated with their supporting sources
CitationSourceidStable, turn-scoped identifier referenced by CitationReference.sourceId
CitationSourceproviderSystem that produced the citation: anthropic, openai, or client
CitationSourcetitle?Human-readable title of the source
CitationSourceurl?URL of the source, when it is a web resource
CitationSourcepath?File path relative to the agent workspace root, when the source is a file
CitationSpanstartIndexStart offset in the final message content (UTF-16 code units, zero-based, inclusive)
CitationSpanendIndexEnd offset in the final message content (UTF-16 code units, zero-based, exclusive)
CitationSpanreferencesThe sources that support this span
CitationReferencesourceIdIdentifier of the CitationSource this reference points to
CitationReferencecitedText?Exact text from the source that supports the span, when the model provides it
CitationReferencelocation?Location within the source that supports the span
CitationReferenceproviderMetadata?Provider-native correlation data, passed through opaquely

ヒント

Span offsets are measured in UTF-16 code units against the final content string. TypeScript, Java, and .NET strings are already UTF-16, so you can slice them directly. Python strings are indexed by Unicode code point and Go and Rust strings are UTF-8, so convert the content to UTF-16 code units before slicing, as the examples above do.

Citation locations

CitationReference.location is a discriminated union keyed on type:

Location typeFieldsUse
charstartIndex, endIndexCharacter range within the source text
pagestartPage, endPagePage range within a paginated document
blockstartBlock, endBlockContent-block range within a structured document

Provide citable sources

Citations need source material the model can attribute. There are two ways to supply it.

Attach documents to a message

When citations are enabled and the session uses an Anthropic provider, file attachments are sent as document blocks with citations turned on, so the model can cite passages from them.

await session.sendAndWait({
    prompt: "Summarize the attached PDF and cite the passages you used.",
    attachments: [
        {
            type: "blob",
            data: pdfBase64,
            displayName: "quarterly-report.pdf",
            mimeType: "application/pdf",
        },
    ],
});

See 画像入力 for the attachment API and the file and blob attachment shapes.

Return citable sources from a tool

Tool results carry an experimental citableSources array. Each entry supplies content that the model can cite, along with an id and optional title, url, and path. These sources are persisted with the tool result, so they survive session resume, and citations built from them are tagged with the client provider.

Limitations

  • Citations are experimental in every SDK and are not covered by compatibility guarantees.
  • Coverage depends on the model provider. A session configured for a provider without citation support emits no citations payload.
  • Citations are only present on the final assistant.message event, so streaming consumers cannot render them mid-response.
  • Public code and IP-duplication citations are not part of this surface.

Further reading