Anthropic | Vision

Claude can understand and analyze images. You can send images as base64-encoded data within content blocks.

Supported Image Formats

Simple Example

Send an image with a prompt asking Claude to describe it.


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

// Load image and encode to base64
oStream := TFileStream.Create('photo.png', fmOpenRead);
Try
  oBytes := TBytesStream.Create;
  Try
    oBytes.CopyFrom(oStream, 0);
    vBase64 := EncodeBase64(oBytes.Memory, oBytes.Size);
  Finally
    oBytes.Free;
  End;
Finally
  oStream.Free;
End;

WriteLn(Anthropic._CreateVisionMessage('claude-sonnet-4-20250514',
  'What is in this image?', vBase64, 'image/png'));

Advanced Example

Use content blocks for more control over the image message.


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

  oMessage := TsgcAnthropicClass_Request_Message.Create;
  oMessage.Role := 'user';

  // Image content block
  oImageBlock := TsgcAnthropicClass_Request_Content_Block.Create;
  oImageBlock.ContentType := 'image';
  oImageBlock.MediaType := 'image/jpeg';
  oImageBlock.Data := vBase64;

  // Text content block
  oTextBlock := TsgcAnthropicClass_Request_Content_Block.Create;
  oTextBlock.ContentType := 'text';
  oTextBlock.Text := 'Describe this image in detail.';

  SetLength(oBlocks, 2);
  oBlocks[0] := oImageBlock;
  oBlocks[1] := oTextBlock;
  oMessage.ContentBlocks := oBlocks;

  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
  oImageBlock.Free;
  oTextBlock.Free;
  oMessage.Free;
  oRequest.Free;
End;