Anthropic Claude
Anthropic is an AI safety company that builds reliable, interpretable, and steerable AI systems. Their flagship model family is Claude, which excels at thoughtful dialogue, content creation, complex reasoning, coding, and more.
The sgcWebSockets library provides a Delphi component TsgcHTTP_API_Anthropic to interact with the Anthropic Claude API.
Anthropic API
The Anthropic API provides access to Claude models for building AI-powered applications. The API supports text generation, vision (image understanding), tool use (function calling), extended thinking, document/PDF processing, prompt caching, citations, web search, streaming, token counting, and message batches.
Features
Configuration
The Anthropic API uses API keys for authentication. Visit your API Keys page in the Anthropic Console to retrieve the API key you'll use in your requests.
Remember that your API key is a secret! Do not share it with others or expose it in any client-side code.
This API Key must be configured in the AnthropicOptions.ApiKey property of the component. The AnthropicOptions.AnthropicVersion property specifies the API version (default: 2023-06-01).
Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'YOUR_API_KEY';
Messages
Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.
- CreateMessage: Creates a message with the specified model and parameters.
- Model: The model to use (e.g. claude-sonnet-4-20250514).
- Message: The user message content.
- MaxTokens: The maximum number of tokens to generate (required, default 4096).
- CreateMessageWithSystem: Creates a message with a system prompt.
- System: System prompt that sets the behavior of the assistant.
- CreateMessageStream: Creates a message with streaming (SSE) enabled. Events are delivered through the OnHTTPAPISSE event handler.
- CreateMessageWithThinking: Creates a message with extended thinking enabled.
- BudgetTokens: Maximum token budget for thinking (minimum 1024).
- CreateDocumentMessage: Creates a message with a base64 document (PDF or text).
- DocumentBase64: The base64-encoded document data.
- MediaType: The MIME type (e.g. application/pdf).
Vision
Claude can understand images passed as base64-encoded content blocks within messages.
- CreateVisionMessage: Sends an image with a text prompt.
- ImageBase64: The base64-encoded image data.
- MediaType: The MIME type (image/jpeg, image/png, image/gif, image/webp).
- Prompt: The text prompt to accompany the image.
Tool Use
Claude can use tools (function calling) to interact with external systems. You define tools with their names, descriptions, and input schemas, and Claude will generate tool_use content blocks when it wants to call a tool.
Models
List and describe the available Claude models.
- GetModels: Lists all available models.
- GetModel: Retrieves information about a specific model.
- ModelId: The ID of the model to retrieve.
Extended Thinking
Extended thinking enables Claude to reason step-by-step before responding, improving quality for complex tasks like math, coding, and analysis.
- ThinkingType: Set to 'enabled' to activate thinking, 'disabled' to turn it off.
- ThinkingBudgetTokens: Token budget for thinking (min 1024, must be less than MaxTokens).
Documents
Claude can process PDF documents and text files sent as content blocks. Supports base64, text, and URL source types.
- CreateDocumentMessage: Sends a document with a text prompt.
- DocumentBase64: The base64-encoded document data.
- MediaType: The MIME type (application/pdf, text/plain).
Prompt Caching
Cache frequently used context (system prompts, content blocks, tool definitions) between API calls to reduce costs by up to 90% on cache reads.
- SystemCacheControl: Set to True on the request to cache the system prompt.
- CacheControl: Set to 'ephemeral' on content blocks or tool definitions.
- BetaHeaders: Optional beta feature headers in AnthropicOptions.
Citations
When documents are sent with citations enabled, Claude includes source references in its response. Citation types include char_location (text), page_location (PDF), content_block_location (custom content), and web_search_result_location (web search).
Web Search
Claude can search the web for real-time information using the built-in web_search tool. Other built-in tools include code_execution and computer use.
- CreateMessageWithWebSearch: Creates a message with web search enabled.
- Model: The model to use.
- Message: The user query.
Token Counting
Count the number of tokens in a message before sending it.
- CountTokens: Counts the number of input tokens for a message.
- Model: The model to use for token counting.
- Message: The message content to count tokens for.
Message Batches
The Message Batches API allows you to process large volumes of messages asynchronously.
- ListBatches: Lists all message batches.
- GetBatch: Retrieves a specific batch by ID.
- CancelBatch: Cancels a batch that is still processing.
- GetBatchResults: Retrieves the results of a completed batch.
Structured Outputs
Force Claude to return valid JSON conforming to a provided JSON Schema. Combine with the Effort parameter to control output quality vs. cost.
- CreateMessageJSON: Creates a message with JSON schema output.
- Schema: A JSON Schema string defining the output format.
- OutputFormatSchema: (Request property) JSON Schema for structured output.
- Effort: (Request property) Controls quality vs. cost: 'low', 'medium', 'high', 'max'.
- Strict: (Tool property) When True, tool inputs are guaranteed to match the input_schema.
Files API
Upload, list, retrieve, download, and delete files. Uploaded files can be referenced in messages using document content blocks with file source type. Requires beta header files-api-2025-04-14.
- UploadFile: Uploads a local file. Returns file metadata.
- ListFiles: Lists uploaded files with pagination.
- GetFile: Retrieves metadata for a specific file.
- DownloadFile: Downloads file content.
- DeleteFile: Permanently deletes a file.
Request Parameters
Additional request parameters available on the Messages API.
- ServiceTier: Controls priority tier usage: 'auto' (default) or 'standard_only'.
- InferenceGeo: Controls inference geography: 'global' (default) or 'us'.
- Container: Container ID for reusing code execution environments across requests.
- IsError: (Content block property) Set True on tool_result blocks to indicate tool execution failure.
- CacheTTL: Extended cache time-to-live: '5m' (default) or '1h'.
- ThinkingType: Set to 'adaptive' to let Claude decide thinking depth automatically.
MCP Connector
Connect Claude to external MCP (Model Context Protocol) servers to access third-party tools. Requires beta header mcp-client-2025-11-20.
- CreateMessageWithMCP: Creates a message with an MCP server connection.
- MCPServerUrl: HTTPS URL of the MCP server.
- MCPServerName: Unique name for the server.
- MCPServers: (Request property) Array of MCP server definitions with ServerType, Url, Name, AuthorizationToken.
- MCPServerName: (Tool property) References an MCP server when ToolType is 'mcp_toolset'.