Are you the author? Sign in to claim
Claude agent sdk for Go(lang)
Official Go SDK for Claude Agent, providing programmatic access to Claude's agent capabilities.
go get github.com/connerohnesorge/claude-agent-sdk-go
package main
import (
"context"
"fmt"
"log"
"github.com/connerohnesorge/claude-agent-sdk-go/pkg/claude"
)
const (
// maxTurns is the maximum number of conversation turns allowed.
maxTurns = 5
)
func main() {
ctx := context.Background()
// Create client with basic options
opts := &claude.Options{
Model: "claude-sonnet-4-5",
MaxTurns: maxTurns,
}
client, err := claude.NewClient(opts)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer func() {
if closeErr := client.Close(); closeErr != nil {
log.Printf("Failed to close client: %v", closeErr)
}
}()
// Send a simple query
fmt.Println("Sending query: What is 2+2?")
query := "What is 2+2? Just respond with the number."
if err = client.Query(ctx, query); err != nil {
log.Printf("Failed to send query: %v", err)
return
}
// Receive and process responses
msgChan, errChan := client.ReceiveMessages(ctx)
for {
select {
case msg := <-msgChan:
if msg == nil {
fmt.Println("Query completed")
return
}
handleMessage(msg)
case err := <-errChan:
if err != nil {
log.Printf("Error: %v", err)
return
}
}
}
}
// handleMessage processes different types of SDK messages.
func handleMessage(msg claude.SDKMessage) {
switch m := msg.(type) {
case *claude.SDKSystemMessage:
if m.Subtype == "init" {
fmt.Printf("Initialized with model: %v\n", m.Data["model"])
}
case *claude.SDKAssistantMessage:
fmt.Println("\nAssistant response:")
for _, block := range m.Message.Content {
switch b := block.(type) {
case claude.TextBlock:
fmt.Printf(" %s\n", b.Text)
case claude.TextContentBlock:
fmt.Printf(" %s\n", b.Text)
}
}
case *claude.SDKResultMessage:
fmt.Printf("\nResult: %s\n", m.Subtype)
fmt.Printf("Duration: %dms\n", m.DurationMS)
fmt.Printf("Cost: $%.4f\n", m.TotalCostUSD)
fmt.Printf("Turns: %d\n", m.NumTurns)
}
}
opts := &claude.Options{
// Model configuration
Model: "claude-sonnet-4-5",
FallbackModel: "claude-3-5-haiku-20241022",
MaxTurns: 10,
MaxThinkingTokens: 8000,
// Directory and tool configuration
Cwd: "/path/to/working/dir",
AdditionalDirectories: []string{"/path/to/extra/dir"},
AllowedTools: []string{"Read", "Write", "Bash"},
DisallowedTools: []string{"WebSearch"},
// Permission handling
PermissionMode: claudeagent.PermissionModeDefault,
PermissionPromptToolName: "PermissionPrompt",
// Message handling
IncludePartialMessages: true,
// Environment variables
Env: map[string]string{
"MAX_THINKING_TOKENS": "8000",
},
}
The SDK automatically validates that the Claude Code CLI version is compatible before establishing a connection. This ensures you receive clear error messages rather than cryptic protocol failures when the CLI is outdated.
The SDK requires Claude Code CLI version 2.0.0 or later. This can be verified by running:
claude --version
In some cases (testing, development, or using custom builds), you may want to skip the version check:
export CLAUDE_AGENT_SDK_SKIP_VERSION_CHECK=true
When the version check fails, you'll receive a clear error message indicating the current CLI version and the minimum required version.
If the CLI version is too old, the SDK returns a version_mismatch error with details:
See the examples/ directory for complete examples:
# Run unit tests
go test ./...
# Run with coverage
go test -cover ./...
# Run integration tests
go test -tags=integration ./test/integration/...
# Run e2e tests (requires ANTHROPIC_API_KEY)
go test -tags=e2e ./test/e2e/...
The SDK is fully functional and tested with the claude CLI binary.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
For issues and questions:
Manage multiple Claude Code instances in separate git worktrees simultaneously
⚠️ Experimentelle Skill-Sammlung für deutsches Recht (Arbeits-, Gesellschafts-, Insolvenz-, Datenschutz-, Prozessrecht u
Manage multiple Claude Code agents from TUI or Web with tmux and git worktrees
Project management using GitHub Issues + Git worktrees for parallel agent execution