A Go library for creating AI agents that can execute tasks using Anthropic's Claude AI with various tools.
package main
import (
"log"
"github.com/frozenkro/goagent/goagent"
"github.com/frozenkro/goagent/models/anthropic"
)
func main() {
// Create a minimal agent with the bash tool and default settings
agent, err := goagent.NewAgent(goagent.WithTools(anthropic.BASH))
if err != nil {
log.Fatal("Failed to create agent:", err)
}
// Run a simple task
ch := agent.Run("List all files in the current directory")
for event := range ch {
if event.Error != nil {
log.Fatalf("Error: %v", event.Error)
}
log.Println(event.Message)
}
}package main
import (
"context"
"log"
"time"
"github.com/frozenkro/goagent/goagent"
"github.com/frozenkro/goagent/models/anthropic"
)
func main() {
// Create an agent with custom options
agent, err := goagent.NewAgent(
goagent.WithModel(anthropic.SONNET_4),
goagent.WithTools(anthropic.BASH, anthropic.TEXT_EDITOR),
goagent.WithMaxTokens(2048),
goagent.WithAPIKey("your-api-key"), // Optional, uses GA_ANTHROPIC_API_KEY env var by default
)
if err != nil {
log.Fatal("Failed to create agent:", err)
}
// Run a more complex task
ch = customAgent.Run("Create a simple Go function that calculates fibonacci numbers and save it to fib/fib.go")
for event := range ch {
if event.Error != nil {
log.Fatalf("Error: %v", event.Error)
}
log.Println(event.Message)
}
}GA_ANTHROPIC_API_KEY: Your Anthropic API key (required unless provided viaWithAPIKeyoption)
WithModel(model): Set the AI model to use (default:anthropic.SONNET_4)WithTools(tools...): Set available tools (default:anthropic.BASH,anthropic.TEXT_EDITOR)WithAPIKey(key): Set a custom API keyWithMaxTokens(tokens): Set maximum tokens for responses (default: 1024)
anthropic.SONNET_4: Claude 3.5 Sonnet (default)
anthropic.BASH: Execute bash commandsanthropic.TEXT_EDITOR: Edit text files
Creates a new agent with the given options.
Executes the given task using the agent.
Executes the given task using the agent with a custom context for cancellation/timeout.
All methods return detailed error messages that wrap the underlying errors. Common error scenarios:
- Missing API key
- Network/HTTP errors
- Anthropic API errors
- JSON marshaling/unmarshaling errors
See the /example directory for complete working examples.