Claude kann detaillierte Zitate liefern, wenn es Fragen zu Dokumenten beantwortet, und hilft dir so, die Quellen hinter jeder Antwort nachzuverfolgen und zu überprüfen.
Alle aktiven Modelle unterstützen Zitate.
Das folgende Beispiel zeigt, wie du Zitate für ein Klartext-Dokument mit der Messages API aktivierst:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "The grass is green. The sky is blue.",
},
"title": "My Document",
"context": "This is a trustworthy document.",
"citations": {"enabled": True},
},
{"type": "text", "text": "What color is the grass and sky?"},
],
}
],
)
print(response)Integriere Zitate mit Claude in diesen Schritten:
Dokument(e) bereitstellen und Zitate aktivieren
citations.enabled=true für jedes deiner Dokumente. Derzeit müssen Zitate entweder für alle oder für keines der Dokumente innerhalb einer Anfrage aktiviert sein.Dokumente werden verarbeitet
Claude liefert eine Antwort mit Zitaten
source-Inhalt eines Dokuments befindet, kann zitiert werden.title und context sind optionale Felder, die an das Modell übergeben, aber nicht für zitierte Inhalte verwendet werden.title ist in der Länge begrenzt, daher ist das context-Feld nützlich, um Dokument-Metadaten als Text oder als String serialisiertes JSON zu speichern.content-Liste, die im Dokument mit benutzerdefiniertem Inhalt bereitgestellt wurde.cited_text wird der Einfachheit halber bereitgestellt und zählt nicht zu den Output-Token.cited_text in nachfolgenden Gesprächsrunden zurückgegeben wird, zählt es ebenfalls nicht zu den Input-Token.Zitate funktionieren in Verbindung mit anderen API-Features, einschließlich Prompt-Caching, Token-Zählung und Batch-Verarbeitung.
Zitate und Prompt-Caching können effektiv zusammen verwendet werden.
Die in Antworten generierten Zitat-Blöcke können nicht direkt gecacht werden, aber die Quelldokumente, auf die sie verweisen, können gecacht werden. Um die Performance zu optimieren, wende cache_control auf deine Dokument-Content-Blöcke auf oberster Ebene an.
client = anthropic.Anthropic()
# Langer Dokumentinhalt (zum Beispiel technische Dokumentation)
long_document = (
"This is a very long document with thousands of words..." + " ... " * 1000
) # Minimum cacheable length
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": long_document,
},
"citations": {"enabled": True},
"cache_control": {
"type": "ephemeral"
}, # Cache the document content
},
{
"type": "text",
"text": "What does this document say about API features?",
},
],
}
],
)
print(response)In diesem Beispiel:
cache_control auf dem Dokument-Block gecacht.Drei Dokumenttypen werden für Zitate unterstützt. Dokumente können direkt in der Nachricht bereitgestellt werden (base64, Text oder URL) oder über die Files API hochgeladen und per file_id referenziert werden:
| Typ | Am besten geeignet für | Chunking | Zitat-Format |
|---|---|---|---|
| Klartext | Einfache Textdokumente, Prosa | Satz | Zeichenindizes (0-indiziert) |
| PDF-Dateien mit Textinhalt | Satz | Seitenzahlen (1-indiziert) | |
| Benutzerdefinierter Inhalt | Listen, Transkripte, spezielle Formatierung, feinere Zitat-Granularität | Kein zusätzliches Chunking | Block-Indizes (0-indiziert) |
Klartext-Dokumente werden automatisch in Sätze aufgeteilt. Du kannst sie inline oder per Referenz mit ihrer file_id bereitstellen:
Das Einführungsbeispiel oben auf dieser Seite zeigt eine vollständige Klartext-Anfrage in jedem SDK. Der Dokument-Block verwendet eine text-Quelle:
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "Plain text content..."
},
"title": "Document Title",
"context": "Context about the document that will not be cited from",
"citations": { "enabled": true }
}PDF-Dokumente können als base64-kodierte Daten, als URL oder per file_id bereitgestellt werden. PDF-Text wird extrahiert und in Sätze aufgeteilt. Da Bildzitate noch nicht unterstützt werden, sind PDFs, die Scans von Dokumenten sind und keinen extrahierbaren Text enthalten, nicht zitierbar.
client = anthropic.Anthropic()
pdf_base64 = base64.standard_b64encode(
pathlib.Path("/path/to/document.pdf").read_bytes()
).decode()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_base64,
},
"title": "Document Title",
"context": "Context about the document that will not be cited from",
"citations": {"enabled": True},
},
{"type": "text", "text": "Summarize this document."},
],
}
],
)
print(response)Dokumente mit benutzerdefiniertem Inhalt geben dir Kontrolle über die Zitat-Granularität. Es erfolgt kein zusätzliches Chunking und die Chunks werden dem Modell entsprechend den bereitgestellten Content-Blöcken übergeben.
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "content",
"content": [
{"type": "text", "text": "First chunk"},
{"type": "text", "text": "Second chunk"},
],
},
"title": "Document Title",
"context": "Context about the document that will not be cited from",
"citations": {"enabled": True},
},
{"type": "text", "text": "Summarize this document."},
],
}
],
)
print(response)Wenn Zitate aktiviert sind, enthalten Antworten mehrere Textblöcke mit Zitaten:
{
"content": [
{"type": "text", "text": "According to the document, "},
{
"type": "text",
"text": "the grass is green",
"citations": [
{
"type": "char_location",
"cited_text": "The grass is green.",
"document_index": 0,
"document_title": "Example Document",
"start_char_index": 0,
"end_char_index": 20,
}
],
},
{"type": "text", "text": " and "},
{
"type": "text",
"text": "the sky is blue",
"citations": [
{
"type": "char_location",
"cited_text": "The sky is blue.",
"document_index": 0,
"document_title": "Example Document",
"start_char_index": 20,
"end_char_index": 36,
}
],
},
{
"type": "text",
"text": ". Information from page 5 states that ",
},
{
"type": "text",
"text": "water is essential",
"citations": [
{
"type": "page_location",
"cited_text": "Water is essential for life.",
"document_index": 1,
"document_title": "PDF Document",
"start_page_number": 5,
"end_page_number": 6,
}
],
},
{
"type": "text",
"text": ". The custom document mentions ",
},
{
"type": "text",
"text": "important findings",
"citations": [
{
"type": "content_block_location",
"cited_text": "These are important findings.",
"document_index": 2,
"document_title": "Custom Content Document",
"start_block_index": 0,
"end_block_index": 1,
}
],
},
]
}Bei Streaming-Antworten kommen Zitate als citations_delta-Delta-Typ innerhalb von content_block_delta-Events an. Jedes Delta enthält ein einzelnes Zitat, das zur citations-Liste des aktuellen text-Content-Blocks hinzugefügt werden soll.
Verarbeite den citations_delta-Delta-Typ zusammen mit Text-Deltas, um zitierte Antworten während des Streamings darzustellen.
Übergib Suchergebnisse aus deiner RAG-Pipeline als erstklassige Content-Blöcke mit integrierter Zitat-Unterstützung.
Erfahre, wie Claude Text aus PDFs extrahiert und wie seitenbasierte Zitate auf deine Quelldateien zurückverweisen.
Lade Dokumente einmal hoch und referenziere sie per file_id über mehrere Zitat-Anfragen hinweg.
| Supported platforms |
|
|---|
Was this page helpful?