agent-sdk-go

module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT

README

Claude Code Agent SDK for Go

Go SDK for building agents with Claude Code CLI. Provides programmatic control over Claude Code sessions via subprocess communication.

What This Is

This SDK wraps the Claude Code CLI, enabling Go applications to:

  • Run Claude Code programmatically
  • Build custom agent loops
  • Stream responses in real-time
  • Execute multi-turn conversations
┌─────────────┐      stdin/stdout       ┌────────────┐
│   Your Go   │ ◄──── stream-json ────► │ Claude CLI │
│   Program   │                         │  (claude)  │
└─────────────┘                         └────────────┘

No API key required - uses your authenticated Claude Code CLI.

Installation

# Requires Claude Code CLI installed and authenticated
# https://docs.anthropic.com/en/docs/claude-code

go get github.com/dotcommander/agent-sdk-go

Quick Start

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/dotcommander/agent-sdk-go/claude"
)

func main() {
    client, err := claude.NewClient()
    if err != nil {
        log.Fatal(err)
    }

    ctx := context.Background()
    response, err := client.Query(ctx, "What is 2+2?")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(response)
}

Streaming Responses

client, _ := claude.NewClient()

msgChan, errChan := client.QueryStream(ctx, "Tell me a story")

for {
    select {
    case msg, ok := <-msgChan:
        if !ok {
            return // Done
        }
        // Process message
        fmt.Printf("%T: %+v\n", msg, msg)
    case err := <-errChan:
        log.Fatal(err)
    }
}

Interactive Sessions

client, _ := claude.NewClient()

// Connect for multi-turn conversation
if err := client.Connect(ctx); err != nil {
    log.Fatal(err)
}
defer client.Disconnect()

// Send messages and receive responses
msgChan, errChan := client.ReceiveMessages(ctx)
// ... handle messages

Configuration

client, err := claude.NewClient(
    claude.WithModel("claude-sonnet-4-20250514"),
    claude.WithTimeout("60s"),
    claude.WithSystemPrompt("You are a helpful assistant"),
)

Using Subprocess Transport Directly

For lower-level control:

import "github.com/dotcommander/agent-sdk-go/claude/subprocess"

config := &subprocess.TransportConfig{
    Model:   "claude-sonnet-4-20250514",
    Timeout: 60 * time.Second,
}

// One-shot mode
transport, _ := subprocess.NewTransportWithPrompt(config, "Hello!")
transport.Connect(ctx)
defer transport.Close()

msgs, errs := transport.ReceiveMessages(ctx)
for msg := range msgs {
    fmt.Printf("%+v\n", msg)
}

Documentation

Guide Description
Getting Started Installation and first steps
Core Concepts Architecture and design
Client API Complete API reference
Streaming Real-time response handling
Sessions Multi-turn conversations
Configuration All options and settings
Advanced Usage Low-level transport, custom handlers
Troubleshooting Common issues and solutions

Project Structure

agent-sdk-go/
├── claude/                 # Public SDK package
│   ├── client.go           # High-level client
│   ├── subprocess/         # CLI subprocess transport
│   ├── parser/             # JSON message parsing
│   ├── shared/             # Shared types
│   ├── v2/                 # V2 session API
│   ├── cli/                # CLI discovery
│   └── mcp/                # MCP server support
├── agent/                  # Agent executor interface
├── docs/                   # Documentation
└── examples/               # 21 usage examples

Requirements

  • Go 1.21+
  • Claude Code CLI installed and authenticated
  • macOS, Linux, or Windows

License

MIT

Directories

Path Synopsis
Package claude provides the client implementation for communicating with the Claude CLI.
Package claude provides the client implementation for communicating with the Claude CLI.
cli
mcp
Package mcp provides in-process MCP (Model Context Protocol) server support.
Package mcp provides in-process MCP (Model Context Protocol) server support.
subprocess
Package subprocess provides subprocess communication with the Claude CLI.
Package subprocess provides subprocess communication with the Claude CLI.
v2
examples
basic-query command
Package main demonstrates a basic one-shot query using the Claude Agent SDK.
Package main demonstrates a basic one-shot query using the Claude Agent SDK.
chatapp command
Package main demonstrates a simple chat application with the Claude Agent SDK.
Package main demonstrates a simple chat application with the Claude Agent SDK.
configuration command
Package main demonstrates all configuration options for the Claude Agent SDK.
Package main demonstrates all configuration options for the Claude Agent SDK.
custom-tools command
Package main demonstrates custom tool registration and execution with the Claude Agent SDK.
Package main demonstrates custom tool registration and execution with the Claude Agent SDK.
debugging-diagnostics command
Package main demonstrates debugging and diagnostics with the Claude Agent SDK.
Package main demonstrates debugging and diagnostics with the Claude Agent SDK.
demo command
Package main provides a demo CLI tool showcasing the agent-sdk-go internal packages.
Package main provides a demo CLI tool showcasing the agent-sdk-go internal packages.
error-handling command
Package main demonstrates error handling patterns with the Claude Agent SDK.
Package main demonstrates error handling patterns with the Claude Agent SDK.
file-checkpointing command
Package main demonstrates file checkpointing with the Claude Agent SDK.
Package main demonstrates file checkpointing with the Claude Agent SDK.
hooks-lifecycle command
Package main demonstrates hook lifecycle patterns with the Claude Agent SDK.
Package main demonstrates hook lifecycle patterns with the Claude Agent SDK.
mcp_tools command
Package main demonstrates SDK MCP tool functionality for agent-sdk-go.
Package main demonstrates SDK MCP tool functionality for agent-sdk-go.
multi-turn command
Package main demonstrates multi-turn conversations using the Claude Agent SDK.
Package main demonstrates multi-turn conversations using the Claude Agent SDK.
partial-streaming command
Package main demonstrates partial message streaming with the Claude Agent SDK.
Package main demonstrates partial message streaming with the Claude Agent SDK.
permission-modes command
Package main demonstrates permission mode patterns with the Claude Agent SDK.
Package main demonstrates permission mode patterns with the Claude Agent SDK.
plugins command
Package main demonstrates plugin configuration with the Claude Agent SDK.
Package main demonstrates plugin configuration with the Claude Agent SDK.
programmatic-subagents command
Package main demonstrates programmatic subagents with the Claude Agent SDK.
Package main demonstrates programmatic subagents with the Claude Agent SDK.
research-agent command
Package main demonstrates a multi-agent research system with the Claude Agent SDK.
Package main demonstrates a multi-agent research system with the Claude Agent SDK.
sandbox-security command
Package main demonstrates sandbox security settings with the Claude Agent SDK.
Package main demonstrates sandbox security settings with the Claude Agent SDK.
sdk-mcp-server command
Package main demonstrates creating SDK MCP servers with the Claude Agent SDK.
Package main demonstrates creating SDK MCP servers with the Claude Agent SDK.
session-control command
Package main demonstrates session control patterns with the Claude Agent SDK.
Package main demonstrates session control patterns with the Claude Agent SDK.
streaming command
Package main demonstrates streaming responses from Claude using the Agent SDK.
Package main demonstrates streaming responses from Claude using the Agent SDK.
structured-output command
Package main demonstrates structured output with the Claude Agent SDK.
Package main demonstrates structured output with the Claude Agent SDK.
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL