Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

DELPHI

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

Claude Components for Delphi

A native Embarcadero Delphi component that lets you call Anthropic's Claude API (the Messages API) directly from your VCL or FireMonkey apps.

  • Target: Delphi 10.2 Tokyo (CompilerVersion 32) and later.
  • Dependencies: RTL only (System.Net.HttpClient, System.JSON). No third-party libraries. Works in both VCL and FMX projects, Win32 and Win64.
  • Default model: claude-opus-4-8.

Anthropic does not ship an official Delphi SDK, so this component talks to the REST endpoint POST /v1/messages over HTTPS using the built-in THTTPClient.

Repository layout

pascal
source/    Claude.Client.pas      The TClaudeClient component (runtime, framework-agnostic)
           Claude.Register.pas    Design-time palette registration
packages/  ClaudeComponents.dpk   Design-time package (.dpk + .dproj)
examples/  ConsoleDemo.dpr        Minimal console demo

Installing the component into the IDE

  1. Open packages/ClaudeComponents.dproj in the RAD Studio IDE (10.2 Tokyo+).
  2. Right-click the package in the Project Manager → Build, then Install.
  3. TClaudeClient now appears on the Claude tab of the Tool Palette.
  4. Add the source folder to your project's search path (Project → Options → Building → Delphi Compiler → Search path) so apps can compile Claude.Client.

You can also skip the IDE palette entirely and just add source\Claude.Client.pas to your project — the component works fine created in code.

Quick start (in code)

pascal
uses
  Claude.Client;

var
  Client: TClaudeClient;
begin
  Client := TClaudeClient.Create(nil);
  try
    Client.APIKey := GetEnvironmentVariable('ANTHROPIC_API_KEY');
    // Client.Model is 'claude-opus-4-8' by default.
    ShowMessage(Client.Ask('Say hello in Korean.'));
  finally
    Client.Free;
  end;
end;

Dropped on a form, set the APIKey property (or, better, assign it at run time from an environment variable / secure store rather than persisting it in the .dfm/.fmx).

API overview

Stateless calls

pascal
function SendMessage(const APrompt: string): TClaudeResponse;          // full result record
function SendMessages(const AMessages: array of TClaudeMessage): TClaudeResponse;
function Ask(const APrompt: string): string;                          // text only; raises EClaudeError on failure

TClaudeResponse carries Success, Text, StopReason, Model, Usage (InputTokens / OutputTokens), StatusCode, RawJSON, and on failure ErrorType / ErrorMessage.

Multi-turn conversation

The component keeps an in-memory message history:

pascal
Client.SystemPrompt := 'You are a helpful assistant.';
Writeln(Client.Chat('My name is Ada.'));      // history is preserved
Writeln(Client.Chat('What is my name?'));     // -> "Ada"
Client.ClearConversation;

Chat adds the user turn, sends the whole conversation, appends the assistant reply to the history, and returns its text. Lower-level building blocks: AddUserMessage, AddAssistantMessage, Send, ClearConversation, MessageCount.

Streaming

pascal
// OnStreamText is a method pointer ("of object"), so assign a method, e.g.:
procedure TForm1.HandleDelta(Sender: TObject; const Delta: string);
begin
  Memo1.Text := Memo1.Text + Delta;
end;

// ...
Client.OnStreamText := HandleDelta;
Client.SendStreaming('Write a short poem.');

OnStreamText fires for each text delta as it arrives (server-sent events are parsed internally). SendStreaming returns the fully assembled TClaudeResponse when complete.

Note: SendStreaming blocks the calling thread until the stream finishes. In a GUI app, call it from a background thread (e.g. TTask.Run) and marshal OnStreamText updates back to the main thread with TThread.Queue.

Published properties

Property Default Notes
APIKey (empty) Your Anthropic API key.
Model claude-opus-4-8 Any valid Claude model id.
MaxTokens 16000 Max output tokens.
SystemPrompt (empty) Optional system prompt.
AnthropicVersion 2023-06-01 anthropic-version header.
BaseURL https://api.anthropic.com Override for proxies/gateways.
TimeoutMS 600000 Applied on Delphi 10.3+ (Tokyo's THTTPClient has no timeout API).

Running the demo

bat
set ANTHROPIC_API_KEY=sk-ant-...
cd examples
dcc32 -U..\source ConsoleDemo.dpr
ConsoleDemo.exe

Notes

  • The default model claude-opus-4-8 rejects temperature/top_p/top_k and budget_tokens, so this component does not send them. Adaptive thinking is controlled server-side; omit it for normal use.
  • Always check Result.Success (or catch EClaudeError from Ask/Chat). StopReason = 'refusal' indicates a safety refusal rather than a transport error.