citations
build-with-claude/citations
History
build-with-claude/citations Changed · +1 / -1 lines
Claude can provide detailed citations when answering questions about documents, helping you track and verify the sources behind each response. -All [active models](https://platform.claude.com/docs/en/about-claude/models/overview) support citations. +All [active models](https://platform.claude.com/docs/en/models/overview) support citations. <Tip> Share your feedback and suggestions about the citations feature using the [citations feedback form](https://forms.gle/9n9hSrKnKe3rpowH9).
build-with-claude/citations Changed · +7 / -13 lines
``` ```bash CLI - ant messages create \ - --model claude-opus-5 \ - --max-tokens 1024 <<'YAML' + ant messages create --model claude-opus-5 --max-tokens 1024 <<'YAML' messages: - role: user content:
``` ```php PHP - // The PHP SDK supports file_id document and image sources only through $client->beta->messages with the files beta. - $citedResponse = $client->beta->messages->create( + $citedResponse = $client->messages->create( maxTokens: 1024, messages: [ [
'content' => [ [ 'type' => 'document', - 'source' => ['type' => 'file', 'file_id' => $fileId], + 'source' => ['type' => 'file', 'fileID' => $fileId], 'title' => 'Document Title', 'context' => 'Context about the document that will not be cited from', 'citations' => ['enabled' => true],
], ], model: 'claude-opus-5', - betas: ['files-api-2025-04-14'], ); - print_r($citedResponse); + echo $citedResponse; ``` ```ruby Ruby
``` ```php PHP - // The PHP SDK supports file_id document and image sources only through $client->beta->messages with the files beta. - $citedResponse = $client->beta->messages->create( + $citedResponse = $client->messages->create( maxTokens: 1024, messages: [ [
'content' => [ [ 'type' => 'document', - 'source' => ['type' => 'file', 'file_id' => $fileId], + 'source' => ['type' => 'file', 'fileID' => $fileId], 'title' => 'Document Title', 'context' => 'Context about the document that will not be cited from', 'citations' => ['enabled' => true],
], ], model: 'claude-opus-5', - betas: ['files-api-2025-04-14'], ); - print_r($citedResponse); + echo $citedResponse; ``` ```ruby Ruby
build-with-claude/citations Changed · +4 / -6 lines
</Tab> <Tab title="Files API"> - <Note> - These examples reference the uploaded file as a `document` source, and no beta header is required. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. - </Note> + These examples reference a file uploaded through the [Files API](https://platform.claude.com/docs/en/build-with-claude/files) as a `document` source. <CodeGroup> ```bash cURL
``` ```php PHP + // The PHP SDK supports file_id document and image sources only through $client->beta->messages with the files beta. $citedResponse = $client->beta->messages->create( maxTokens: 1024, messages: [
</Tab> <Tab title="Files API"> - <Note> - These examples reference the uploaded file as a `document` source, and no beta header is required. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. - </Note> + These examples reference a file uploaded through the [Files API](https://platform.claude.com/docs/en/build-with-claude/files) as a `document` source. <CodeGroup> ```bash cURL
``` ```php PHP + // The PHP SDK supports file_id document and image sources only through $client->beta->messages with the files beta. $citedResponse = $client->beta->messages->create( maxTokens: 1024, messages: [
build-with-claude/citations Changed · +58 / -72 lines
<Tab title="Files API"> <Note> - These examples reference the uploaded file as a `document` source. They use the SDK `beta` client path and send the `anthropic-beta: files-api-2025-04-14` header, which the API accepts but does not require. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. + These examples reference the uploaded file as a `document` source, and no beta header is required. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. </Note> <CodeGroup>
curl -X POST https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ - -H "anthropic-beta: files-api-2025-04-14" \ -H "content-type: application/json" \ -d @- <<EOF {
``` ```bash CLI - ant beta:messages create --beta files-api-2025-04-14 <<YAML + ant messages create <<YAML model: claude-opus-5 max_tokens: 1024 messages:
``` ```python Python - cited_response = client.beta.messages.create( + cited_response = client.messages.create( model="claude-opus-5", max_tokens=1024, messages=[
], } ], - betas=["files-api-2025-04-14"], ) print(cited_response) ``` ```typescript TypeScript - const citedResponse = await client.beta.messages.create({ + const citedResponse = await client.messages.create({ model: "claude-opus-5", max_tokens: 1024, messages: [
], }, ], - betas: ["files-api-2025-04-14"], }); console.log(citedResponse); ``` ```csharp C# - var citedResponse = await client.Beta.Messages.Create( + var citedResponse = await client.Messages.Create( new MessageCreateParams { - Model = Messages::Model.ClaudeOpus5, + Model = Model.ClaudeOpus5, MaxTokens = 1024, - Betas = [AnthropicBeta.FilesApi2025_04_14], Messages = [ - new BetaMessageParam + new MessageParam { Role = Role.User, - Content = new List<BetaContentBlockParam> + Content = new List<ContentBlockParam> { - new BetaRequestDocumentBlock + new DocumentBlockParam { - Source = new BetaFileDocumentSource { FileID = fileId }, + Source = new FileDocumentSource { FileID = fileId }, Title = "Document Title", Context = "Context about the document that will not be cited from", - Citations = new BetaCitationsConfigParam { Enabled = true }, + Citations = new CitationsConfigParam { Enabled = true }, }, - new BetaTextBlockParam { Text = "Summarize this document." }, + new TextBlockParam { Text = "Summarize this document." }, } } ]
``` ```go Go - citedMsg, err := client.Beta.Messages.New(context.Background(), - anthropic.BetaMessageNewParams{ + citedMsg, err := client.Messages.New(context.Background(), + anthropic.MessageNewParams{ Model: anthropic.ModelClaudeOpus5, MaxTokens: 1024, - Betas: []anthropic.AnthropicBeta{anthropic.AnthropicBetaFilesAPI2025_04_14}, - Messages: []anthropic.BetaMessageParam{ - anthropic.NewBetaUserMessage( - anthropic.BetaContentBlockParamUnion{ - OfDocument: &anthropic.BetaRequestDocumentBlockParam{ - Source: anthropic.BetaRequestDocumentBlockSourceUnionParam{ - OfFile: &anthropic.BetaFileDocumentSourceParam{FileID: fileID}, + Messages: []anthropic.MessageParam{ + anthropic.NewUserMessage( + anthropic.ContentBlockParamUnion{ + OfDocument: &anthropic.DocumentBlockParam{ + Source: anthropic.DocumentBlockParamSourceUnion{ + OfFile: &anthropic.FileDocumentSourceParam{FileID: fileID}, }, Title: anthropic.String("Document Title"), Context: anthropic.String("Context about the document that will not be cited from"), - Citations: anthropic.BetaCitationsConfigParam{Enabled: anthropic.Bool(true)}, + Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)}, }, }, - anthropic.NewBetaTextBlock("Summarize this document."), + anthropic.NewTextBlock("Summarize this document."), ), }, })
```java Java MessageCreateParams citedParams = MessageCreateParams.builder() .model(Model.CLAUDE_OPUS_5) - .addBeta("files-api-2025-04-14") .maxTokens(1024) - .addUserMessageOfBetaContentBlockParams(List.of( - BetaContentBlockParam.ofDocument(BetaRequestDocumentBlock.builder() - .source(BetaFileDocumentSource.builder().fileId(fileId).build()) + .addUserMessageOfBlockParams(List.of( + ContentBlockParam.ofDocument(DocumentBlockParam.builder() + .fileSource(fileId) .title("Document Title") .context("Context about the document that will not be cited from") - .citations(BetaCitationsConfigParam.builder().enabled(true).build()) + .citations(CitationsConfigParam.builder().enabled(true).build()) .build()), - BetaContentBlockParam.ofText(BetaTextBlockParam.builder() + ContentBlockParam.ofText(TextBlockParam.builder() .text("Summarize this document.") .build()) )) .build(); - BetaMessage citedMessage = client.beta().messages().create(citedParams); + Message citedMessage = client.messages().create(citedParams); System.out.println(citedMessage); ```
``` ```ruby Ruby - cited_response = client.beta.messages.create( + cited_response = client.messages.create( model: "claude-opus-5", max_tokens: 1024, - betas: ["files-api-2025-04-14"], messages: [ { role: "user",
<Tab title="Files API"> <Note> - These examples reference the uploaded file as a `document` source. They use the SDK `beta` client path and send the `anthropic-beta: files-api-2025-04-14` header, which the API accepts but does not require. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. + These examples reference the uploaded file as a `document` source, and no beta header is required. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. </Note> <CodeGroup>
curl -X POST https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ - -H "anthropic-beta: files-api-2025-04-14" \ -H "content-type: application/json" \ -d @- <<EOF {
``` ```bash CLI - ant beta:messages create --beta files-api-2025-04-14 <<YAML + ant messages create <<YAML model: claude-opus-5 max_tokens: 1024 messages:
``` ```python Python - cited_response = client.beta.messages.create( + cited_response = client.messages.create( model="claude-opus-5", max_tokens=1024, messages=[
], } ], - betas=["files-api-2025-04-14"], ) print(cited_response) ``` ```typescript TypeScript - const citedResponse = await client.beta.messages.create({ + const citedResponse = await client.messages.create({ model: "claude-opus-5", max_tokens: 1024, messages: [
], }, ], - betas: ["files-api-2025-04-14"], }); console.log(citedResponse); ``` ```csharp C# - var citedResponse = await client.Beta.Messages.Create( + var citedResponse = await client.Messages.Create( new MessageCreateParams { - Model = Messages::Model.ClaudeOpus5, + Model = Model.ClaudeOpus5, MaxTokens = 1024, - Betas = [AnthropicBeta.FilesApi2025_04_14], Messages = [ - new BetaMessageParam + new MessageParam { Role = Role.User, - Content = new List<BetaContentBlockParam> + Content = new List<ContentBlockParam> { - new BetaRequestDocumentBlock + new DocumentBlockParam { - Source = new BetaFileDocumentSource { FileID = fileId }, + Source = new FileDocumentSource { FileID = fileId }, Title = "Document Title", Context = "Context about the document that will not be cited from", - Citations = new BetaCitationsConfigParam { Enabled = true }, + Citations = new CitationsConfigParam { Enabled = true }, }, - new BetaTextBlockParam { Text = "Summarize this document." }, + new TextBlockParam { Text = "Summarize this document." }, } } ]
``` ```go Go - citedMsg, err := client.Beta.Messages.New(context.Background(), - anthropic.BetaMessageNewParams{ + citedMsg, err := client.Messages.New(context.Background(), + anthropic.MessageNewParams{ Model: anthropic.ModelClaudeOpus5, MaxTokens: 1024, - Betas: []anthropic.AnthropicBeta{anthropic.AnthropicBetaFilesAPI2025_04_14}, - Messages: []anthropic.BetaMessageParam{ - anthropic.NewBetaUserMessage( - anthropic.BetaContentBlockParamUnion{ - OfDocument: &anthropic.BetaRequestDocumentBlockParam{ - Source: anthropic.BetaRequestDocumentBlockSourceUnionParam{ - OfFile: &anthropic.BetaFileDocumentSourceParam{FileID: fileID}, + Messages: []anthropic.MessageParam{ + anthropic.NewUserMessage( + anthropic.ContentBlockParamUnion{ + OfDocument: &anthropic.DocumentBlockParam{ + Source: anthropic.DocumentBlockParamSourceUnion{ + OfFile: &anthropic.FileDocumentSourceParam{FileID: fileID}, }, Title: anthropic.String("Document Title"), Context: anthropic.String("Context about the document that will not be cited from"), - Citations: anthropic.BetaCitationsConfigParam{Enabled: anthropic.Bool(true)}, + Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)}, }, }, - anthropic.NewBetaTextBlock("Summarize this document."), + anthropic.NewTextBlock("Summarize this document."), ), }, })
```java Java MessageCreateParams citedParams = MessageCreateParams.builder() .model(Model.CLAUDE_OPUS_5) - .addBeta("files-api-2025-04-14") .maxTokens(1024) - .addUserMessageOfBetaContentBlockParams(List.of( - BetaContentBlockParam.ofDocument(BetaRequestDocumentBlock.builder() - .source(BetaFileDocumentSource.builder().fileId(fileId).build()) + .addUserMessageOfBlockParams(List.of( + ContentBlockParam.ofDocument(DocumentBlockParam.builder() + .fileSource(fileId) .title("Document Title") .context("Context about the document that will not be cited from") - .citations(BetaCitationsConfigParam.builder().enabled(true).build()) + .citations(CitationsConfigParam.builder().enabled(true).build()) .build()), - BetaContentBlockParam.ofText(BetaTextBlockParam.builder() + ContentBlockParam.ofText(TextBlockParam.builder() .text("Summarize this document.") .build()) )) .build(); - BetaMessage citedMessage = client.beta().messages().create(citedParams); + Message citedMessage = client.messages().create(citedParams); System.out.println(citedMessage); ```
``` ```ruby Ruby - cited_response = client.beta.messages.create( + cited_response = client.messages.create( model: "claude-opus-5", max_tokens: 1024, - betas: ["files-api-2025-04-14"], messages: [ { role: "user",
build-with-claude/citations Changed · +2 / -2 lines
<Tab title="Files API"> <Note> - Files API document sources are in beta. These examples use the beta client path; see [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. + These examples reference the uploaded file as a `document` source. They use the SDK `beta` client path and send the `anthropic-beta: files-api-2025-04-14` header, which the API accepts but does not require. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. </Note> <CodeGroup>
<Tab title="Files API"> <Note> - Files API document sources are in beta. These examples use the beta client path; see [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. + These examples reference the uploaded file as a `document` source. They use the SDK `beta` client path and send the `anthropic-beta: files-api-2025-04-14` header, which the API accepts but does not require. See [Files API](https://platform.claude.com/docs/en/build-with-claude/files) for upload details. </Note> <CodeGroup>
build-with-claude/citations First recorded · 2283 lines, first recorded
## Compatibility ## How citations work ### Citable versus non-citable content ### Citation indices ### Token costs ### Feature compatibility #### Using prompt caching with citations ## Document types ### Choosing a document type ### Plain text documents ### PDF documents ### Custom content documents ## Response structure ### Streaming support ## Next steps
The first capture of this source. The page was already there, and this is what it said.
---
title: Citations
url: https://platform.claude.com/docs/en/build-with-claude/citations
description: Ground Claude's responses in your source documents. Citations return the exact passages that support each claim, so you can verify answers and surface sources to your users.
---
## Compatibility
- [ZDR](https://platform.claude.com/docs/en/manage-claude/api-and-data-retention): eligible (excludes [Covered Models](https://platform.claude.com/docs/en/manage-claude/api-and-data-retention#model-specific-data-retention-requirements))
- Platforms: Claude API, Claude Platform on AWS, Amazon Bedrock, Google Cloud, Microsoft Foundry
Claude can provide detailed citations when answering questions about documents, helping you track and verify the sources behind each response.
All [active models](https://platform.claude.com/docs/en/about-claude/models/overview) support citations.
<Tip>
Share your feedback and suggestions about the citations feature using the [citations feedback form](https://forms.gle/9n9hSrKnKe3rpowH9).
</Tip>
The following example shows how to enable citations on a plain text document with the Messages API:
<CodeGroup>
```bash cURL
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"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?"
}
]
}
]
}'
```
```bash CLI
ant messages create <<'YAML'
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?
YAML
```
```python Python
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)
```
```typescript TypeScript
const client = new Anthropic();
const response = await 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?"
}
]
}
]
});
console.log(response);
```
```csharp C#
var client = new AnthropicClient();
var response = await client.Messages.Create(
new()
{
Model = Model.ClaudeOpus5,
MaxTokens = 1024,
Messages =
[
new()
{
Role = Role.User,
Content = new MessageParamContent(new List<ContentBlockParam>
{
new ContentBlockParam(new DocumentBlockParam(
new DocumentBlockParamSource(new PlainTextSource()
{
Data = "The grass is green. The sky is blue.",
})
)
{
Title = "My Document",
Context = "This is a trustworthy document.",
Citations = new CitationsConfigParam { Enabled = true },
}),
new ContentBlockParam(new TextBlockParam("What color is the grass and sky?")),
}),
},
],
}
);
Console.WriteLine(response);
```
```go Go
client := anthropic.NewClient()
response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
Model: anthropic.ModelClaudeOpus5,
MaxTokens: 1024,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(
anthropic.ContentBlockParamUnion{
OfDocument: &anthropic.DocumentBlockParam{
Source: anthropic.DocumentBlockParamSourceUnion{
OfText: &anthropic.PlainTextSourceParam{
Data: "The grass is green. The sky is blue.",
},
},
Title: anthropic.String("My Document"),
Context: anthropic.String("This is a trustworthy document."),
Citations: anthropic.CitationsConfigParam{Enabled: anthropic.Bool(true)},
},
},
anthropic.NewTextBlock("What color is the grass and sky?"),
),
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
```
```java Java
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
PlainTextSource source = PlainTextSource.builder()
.data("The grass is green. The sky is blue.")
.build();
DocumentBlockParam documentParam = DocumentBlockParam.builder()
.source(source)
.title("My Document")
.context("This is a trustworthy document.")
.citations(CitationsConfigParam.builder().enabled(true).build())
.build();
TextBlockParam textBlockParam = TextBlockParam.builder()
.text("What color is the grass and sky?")
.build();
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.CLAUDE_OPUS_5)
.maxTokens(1024)
.addUserMessageOfBlockParams(
List.of(
ContentBlockParam.ofDocument(documentParam),
ContentBlockParam.ofText(textBlockParam)
)
)
.build();
Message message = client.messages().create(params);
System.out.println(message);
```
```php PHP
$client = new Client();
$response = $client->messages->create(
maxTokens: 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?',
],
],
],
],
model: 'claude-opus-5',
);
echo json_encode($response, JSON_PRETTY_PRINT);
```
```ruby Ruby
client = Anthropic::Client.new
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?"
}
]
}
]
)
puts response
```
Cut at 300 lines.