Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Agent Library

A Go library for creating AI agents that can execute tasks using Anthropic's Claude AI with various tools.

Usage

Basic Usage

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)
	}
}

Advanced Usage

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)
	}
}

Configuration

Environment Variables

  • GA_ANTHROPIC_API_KEY: Your Anthropic API key (required unless provided via WithAPIKey option)

Agent Options

  • 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 key
  • WithMaxTokens(tokens): Set maximum tokens for responses (default: 1024)

Available Models

  • anthropic.SONNET_4: Claude 3.5 Sonnet (default)

Available Tools

  • anthropic.BASH: Execute bash commands
  • anthropic.TEXT_EDITOR: Edit text files

API Reference

NewAgent(opts ...AgentOption) (*Agent, error)

Creates a new agent with the given options.

(a *Agent) Run(task string) error

Executes the given task using the agent.

(a *Agent) RunWithContext(ctx context.Context, task string) error

Executes the given task using the agent with a custom context for cancellation/timeout.

Error Handling

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

Examples

See the /example directory for complete working examples.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages