Welcome to the Anthropic Go SDK
Relevant source files
The following files were used as context for generating this wiki page:
The Anthropic Go SDK provides a robust, type-safe interface for Go applications to interact with the Anthropic Claude API. It supports a wide array of features including text generation, message streaming, multimodal inputs (images and documents), and tool use (function calling). The SDK is designed to be extensible, offering specialized support for cloud providers like Google Vertex AI and AWS Bedrock.
Most of the SDK code is automatically generated, ensuring consistency with the official OpenAPI specification, while the lib/ and examples/ directories are reserved for manual modifications and implementation guides.
Sources: README.md:7-9, CONTRIBUTING.md:16-19
Core Architecture and Initialization
The SDK revolves around a central Client object created via anthropic.NewClient(). Configuration is handled through functional options, allowing users to specify API keys, base URLs, and cloud-specific authentication.
The diagram above illustrates the initialization flow and the service hierarchy within the client.
Sources: README.md:31-35, examples/file-upload/main.go:16-17, betamessagebatch_test.go:28-31
Authentication Methods
| Method | Provider | Description |
|---|---|---|
option.WithAPIKey | Anthropic | Sets the standard ANTHROPIC_API_KEY. Defaults to environment lookup. |
vertex.WithGoogleAuth | Google Cloud | Configures OAuth for Vertex AI with Region and Project ID. |
bedrock.WithLoadDefaultConfig | AWS | Loads AWS credentials from environment (e.g., AWS_BEARER_TOKEN_BEDROCK). |
| Sources: README.md:32, examples/vertex/main.go:14-16, examples/bedrock-bearer-token/main.go:26-28 |
Message Interaction
The primary interface for generating text is the Messages service. It provides two main modes: synchronous requests and streaming responses.
Synchronous Messages
Synchronous calls block until the full response is received. Developers define MessageNewParams containing the model, max tokens, and an array of Messages.
message, err := client.Messages.New(ctx, anthropic.MessageNewParams{
MaxTokens: 1024,
Messages: []anthropic.MessageParam{anthropic.NewUserMessage(anthropic.NewTextBlock("Query"))},
Model: anthropic.ModelClaudeSonnet4_5_20250929,
})
Sources: examples/message/main.go:15-22, usage_test.go:31-41
Streaming Messages
Streaming allows applications to process responses incrementally. The NewStreaming method returns a Stream object that can be iterated over using .Next() and .Current().
The sequence diagram shows the flow of Server-Sent Events from the API to the application layer.
Sources: examples/message-streaming/main.go:19-24, examples/message-streaming/main.go:28-40
Advanced Features
Tool Use (Function Calling)
The SDK enables Claude to interact with external functions. Tools are defined with a Name, Description, and a JSON Schema for inputs. The SDK includes a schemautil.go utility to transform standard JSON schemas into the specific format required by the Anthropic API (e.g., forcing additionalProperties: false).
Sources: examples/tools/main.go:26-40, schemautil.go:78-100
Multimodal Support
Claude can process images and documents alongside text.
- Images: Encoded as Base64 strings within
NewImageBlockBase64. - Documents: Uploaded via the
Beta.Filesservice and referenced by ID inNewBetaDocumentBlock.
Sources: examples/multimodal/main.go:23-42, examples/file-upload/main.go:23-46
Beta Features and Schema Utilities
The Beta service namespace contains experimental features such as:
- Structured Outputs: Forcing model responses into a specific JSON schema using
OutputFormat. - Message Batches: Submitting multiple requests for asynchronous processing.
- Context Management: Features like
CacheControlto optimize token usage.
Sources: examples/structured-outputs/main.go:42-45, betamessage_test.go:41-47, betamessagebatch_test.go:32-35
Development and Testing
The repository includes a robust suite of scripts for local development.
| Command | Purpose |
|---|---|
./scripts/bootstrap | Installs dependencies and builds the SDK. |
./scripts/lint | Runs code linters. |
./scripts/mock | Starts a mock server based on the OpenAPI spec for testing. |
./scripts/test | Executes the test suite. |
go run ./examples/<name> | Runs a specific implementation example. |
| Sources: CONTRIBUTING.md:10-12, CONTRIBUTING.md:27-29, CONTRIBUTING.md:46-52 |
Summary
The Anthropic Go SDK is a comprehensive toolset for integrating Claude into Go environments. It balances ease of use with powerful low-level control, providing type safety for complex features like Tool Use and Beta API access. By supporting multiple cloud providers and offering utilities for schema transformation, it serves as a flexible foundation for building AI-native applications.