Anthropic | Documents

Claude can process PDF documents and text files sent as content blocks within messages. Documents are sent as base64-encoded data or via URL, and Claude can analyze, summarize, and answer questions about their content.

Simple Example

Send a PDF document with a question using the convenience method.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';

// Load PDF file and encode to base64
vBase64 := sgcBase64Encode(LoadFileToBytes('document.pdf'));
WriteLn(Anthropic._CreateDocumentMessage('claude-sonnet-4-20250514',
  'Summarize this document.', vBase64, 'application/pdf'));

Advanced Example with Citations

Use the typed classes to send a document with citations enabled. When citations are enabled, Claude's response will include references to specific parts of the source document.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';

oRequest := TsgcAnthropicClass_Request_Messages.Create;
Try
  oRequest.Model := 'claude-sonnet-4-20250514';
  oRequest.MaxTokens := 4096;

  // Create document content block
  oDocBlock := TsgcAnthropicClass_Request_Content_Block.Create;
  oDocBlock.ContentType := 'document';
  oDocBlock.SourceType := 'base64';
  oDocBlock.MediaType := 'application/pdf';
  oDocBlock.Data := sgcBase64Encode(LoadFileToBytes('report.pdf'));
  oDocBlock.Title := 'Annual Report';
  oDocBlock.CitationsEnabled := True;

  // Create text prompt block
  oTextBlock := TsgcAnthropicClass_Request_Content_Block.Create;
  oTextBlock.ContentType := 'text';
  oTextBlock.Text := 'What are the key findings?';

  // Build message with content blocks
  oMessage := TsgcAnthropicClass_Request_Message.Create;
  oMessage.Role := 'user';
  oBlocks := oMessage.ContentBlocks;
  SetLength(oBlocks, 2);
  oBlocks[0] := oDocBlock;
  oBlocks[1] := oTextBlock;
  oMessage.ContentBlocks := oBlocks;

  oMessages := oRequest.Messages;
  SetLength(oMessages, 1);
  oMessages[0] := oMessage;
  oRequest.Messages := oMessages;

  oResponse := Anthropic.CreateMessage(oRequest);
  Try
    if Length(oResponse.Content) > 0 then
      WriteLn(oResponse.Content[0].Text);
  Finally
    oResponse.Free;
  End;
Finally
  oDocBlock.Free;
  oTextBlock.Free;
  oMessage.Free;
  oRequest.Free;
End;

Properties

Supported Formats