Integrate Anthropic Claude API in Your Delphi Applications

sgcWebSockets delivers a production-ready Delphi component for the full Anthropic Claude API — from text generation and vision to tool use, extended thinking, and beyond. 

Anthropic Claude is one of the most advanced AI model families available today, renowned for its exceptional reasoning, safety-first design, and versatile capabilities across text, code, vision, and structured data. For Delphi developers looking to harness these capabilities, sgcWebSockets provides TsgcHTTP_API_Anthropic — a comprehensive, native component that wraps the entire Anthropic API surface.

Whether you are building intelligent chatbots, automating document analysis, orchestrating complex tool pipelines, or processing thousands of requests in batch, this component gives you direct access to every Claude feature through clean, type-safe Delphi code. No REST boilerplate. No JSON wrangling. Just drop the component, set your API key, and start building.

Complete API Coverage

 Every major feature of the Anthropic Claude API is supported out of the box.

Messages & Streaming

Send messages with system prompts, receive responses synchronously or streamed in real-time via Server-Sent Events.
Vision

Analyze images in JPEG, PNG, GIF, and WebP formats. Claude describes, interprets, and reasons about visual content.
Tool Use

Define custom tools with JSON Schema and let Claude call them. Build agentic workflows with function calling.
Extended Thinking

Enable step-by-step reasoning for complex tasks. Ideal for math, analysis, and multi-step problem solving.
Document Processing

Send PDF documents and text files for analysis, summarization, and question answering with citation support.
Structured Outputs

Force Claude to return valid JSON conforming to your schema. Guaranteed parseable, type-safe responses.
Web Search

Let Claude search the web for real-time information during conversations using built-in server-side tools.
Prompt Caching

Cache system prompts, content blocks, and tool definitions. Reduce costs by up to 90% on repeated context.
Message Batches

Process thousands of requests asynchronously. Perfect for bulk content generation and data processing pipelines.
Files API

Upload, list, download, and manage files on Anthropic's servers. Reference them across multiple conversations.
MCP Connector

Connect Claude to external Model Context Protocol servers. Extend capabilities with third-party tools and services.
Token Counting

Count tokens before sending requests. Accurately estimate costs and manage context window budgets.

Getting Started

Integrate Claude into your Delphi project in under a minute. Drop the component, configure your API key, and send your first message. 

// Create the component and configure the API key
var
  Anthropic: TsgcHTTP_API_Anthropic;
  vResponse: string;
begin
  Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
  Try
    Anthropic.AnthropicOptions.ApiKey := 'YOUR_API_KEY';

    // Send a simple message to Claude
    vResponse := Anthropic._CreateMessage(
      'claude-sonnet-4-20250514', 'Hello, Claude!');

    ShowMessage(vResponse);
  Finally
    Anthropic.Free;
  End;
end; 

Messages & Streaming

The Messages API is the foundation of every Claude interaction. Send text with optional system prompts, and receive responses synchronously or streamed in real-time.

System Prompts

Control Claude's behavior by providing a system prompt that sets the context, personality, or constraints for the conversation. 

vResponse := Anthropic._CreateMessageWithSystem(
  'claude-sonnet-4-20250514',
  'You are a helpful assistant that responds in Spanish.',
  'What is the capital of France?');
// Returns: "La capital de Francia es París." 

Real-Time Streaming

For responsive user interfaces, stream Claude's response token-by-token using Server-Sent Events. Assign the OnHTTPAPISSE event handler and call _CreateMessageStream

// Enable streaming via SSE
Anthropic.OnHTTPAPISSE := OnSSEEvent;
Anthropic._CreateMessageStream('claude-sonnet-4-20250514',
  'Tell me a story about a brave explorer.');

procedure TForm1.OnSSEEvent(Sender: TObject;
  const aEvent, aData: string; var Cancel: Boolean);
begin
  // aEvent: event type (e.g., content_block_delta)
  // aData: JSON payload for this event
  Memo1.Lines.Add(aData);
end; 

Advanced Typed API

For full control over request parameters — temperature, top-p, stop sequences, metadata — use the typed request and response classes. 

var
  oRequest: TsgcAnthropicClass_Request_Messages;
  oMessage: TsgcAnthropicClass_Request_Message;
  oResponse: TsgcAnthropicClass_Response_Messages;
begin
  oRequest := TsgcAnthropicClass_Request_Messages.Create;
  Try
    oRequest.Model := 'claude-sonnet-4-20250514';
    oRequest.MaxTokens := 1024;
    oRequest.System := 'You are a helpful assistant.';
    oRequest.Temperature := 0.7;

    oMessage := TsgcAnthropicClass_Request_Message.Create;
    oMessage.Role := 'user';
    oMessage.Content := 'Explain quantum computing in simple terms.';
    // ... add message to request, send, and process response

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

Vision — Image Understanding

 Claude can analyze and reason about images. Send photographs, screenshots, diagrams, or charts and receive detailed descriptions, data extraction, or visual Q&A.

Supported formats include JPEG, PNG, GIF, and WebP. Images are sent as base64-encoded content blocks.

// Load an image and ask Claude to describe it
var
  vBase64: string;
begin
  vBase64 := sgcBase64Encode(LoadFileToBytes('product-photo.png'));

  ShowMessage(Anthropic._CreateVisionMessage(
    'claude-sonnet-4-20250514',
    'Describe this product image for an e-commerce listing.',
    vBase64, 'image/png'));
end; 

Tool Use — Function Calling

Define custom tools with JSON Schema, and Claude will decide when and how to invoke them. This is the foundation for building agentic, multi-step workflows.

The tool use flow follows a clear pattern: define tools, send a request, detect tool_use in the response, execute the function locally, and return the result to Claude for the final answer. 

// Define a weather tool with JSON Schema input
oTool := TsgcAnthropicClass_Request_Tool.Create;
oTool.Name := 'get_weather';
oTool.Description := 'Get the current weather in a given location';
oTool.InputSchema :=
  '{"type":"object","properties":{"location":{"type":"string",' +
  '"description":"The city and state"}},"required":["location"]}';

// Send request with tools defined
oResponse := Anthropic.CreateMessage(oRequest);

// Check if Claude wants to call a tool
if oResponse.StopReason = 'tool_use' then
begin
  for i := 0 to Length(oResponse.Content) - 1 do
  begin
    if oResponse.Content[i].ContentType = 'tool_use' then
    begin
      vToolUseId := oResponse.Content[i].Id;
      vToolName  := oResponse.Content[i].Name;
      vToolInput := oResponse.Content[i].Input;
      // Execute the tool and return the result to Claude
    end;
  end;
end; 

Document Processing & Citations

Send PDF documents and text files to Claude for analysis, summarization, and question answering. Enable citations to receive verifiable references back to the source material. 

// Send a PDF and ask Claude to summarize it
vBase64 := sgcBase64Encode(LoadFileToBytes('annual-report.pdf'));
vResponse := Anthropic._CreateDocumentMessage(
  'claude-sonnet-4-20250514',
  'Summarize the key findings of this report.',
  vBase64, 'application/pdf'); 

Citations

Enable citations on document content blocks to receive source references — including page numbers, character ranges, and cited text — in Claude's response. 

oDocBlock := TsgcAnthropicClass_Request_Content_Block.Create;
oDocBlock.ContentType := 'document';
oDocBlock.SourceType := 'base64';
oDocBlock.MediaType := 'application/pdf';
oDocBlock.Data := vBase64;
oDocBlock.Title := 'Annual Report';
oDocBlock.CitationsEnabled := True;   

MCP Connector

Connect Claude to external Model Context Protocol (MCP) servers to extend its capabilities with third-party tools and services — databases, CRMs, internal APIs, and more. 

Anthropic.AnthropicOptions.BetaHeaders := 'mcp-client-2025-11-20';

// Connect to an MCP server with one method call
vResponse := Anthropic._CreateMessageWithMCP(
  'claude-sonnet-4-20250514',
  'What tools are available?',
  'https://my-mcp-server.example.com/sse',
  'my-mcp-server'); 

Authenticated MCP Servers

For servers that require authentication, the typed API supports OAuth Bearer tokens. 

oServer := TsgcAnthropicClass_Request_MCPServer.Create;
oServer.ServerType := 'url';
oServer.Url := 'https://my-mcp-server.example.com/sse';
oServer.Name := 'my-server';
oServer.AuthorizationToken := 'OAUTH_TOKEN';

oTool := TsgcAnthropicClass_Request_Tool.Create;
oTool.ToolType := 'mcp_toolset';
oTool.MCPServerName := 'my-server'; 

Delphi Sample

sgcAnthropic
2.8 mb