AI SDK

Agents

Build stateful, autonomous AI agents with memory and tools

Agents

Build stateful, autonomous AI agents with persistent memory, conversation history, and tool execution.

Basic Agent

agent := sdk.NewAgent(
    "my-agent",
    llmManager,
    logger,
    metrics,
    &sdk.AgentOptions{
        Model:       "gpt-4",
        Temperature: 0.7,
        MaxHistory:  50,
    },
)

result, err := agent.Execute(ctx, "Hello, how are you?")
fmt.Println(result.Content)

Persistent State

agent := sdk.NewAgent(
    "customer-support",
    llmManager,
    logger,
    metrics,
    &sdk.AgentOptions{
        Model:       "gpt-4",
        StateStore:  stateStore,  // Persists across restarts
        MaxHistory:  100,
    },
)

// First conversation
agent.Execute(ctx, "My order number is #12345")

// Later (even after restart)
agent.Execute(ctx, "What's the status?")
// Agent remembers order #12345

With Tools

// Register tools
agent.RegisterTool(&sdk.Tool{
    Name:        "get_weather",
    Description: "Get current weather",
    Parameters: map[string]interface{}{
        "type": "object",
        "properties": map[string]interface{}{
            "location": map[string]string{
                "type": "string",
            },
        },
    },
    Handler: func(ctx context.Context, args map[string]interface{}) (interface{}, error) {
        location := args["location"].(string)
        return getWeatherFromAPI(location)
    },
})

result, _ := agent.Execute(ctx, "What's the weather in San Francisco?")
// Agent automatically calls get_weather tool

Conversation History

// Add messages to history
agent.AddMessage(sdk.AgentMessage{
    Role:    "user",
    Content: "What's your name?",
})

agent.AddMessage(sdk.AgentMessage{
    Role:    "assistant",
    Content: "I'm an AI assistant.",
})

// Get history
history := agent.GetHistory()
for _, msg := range history {
    fmt.Printf("%s: %s\n", msg.Role, msg.Content)
}

// Clear history
agent.ClearHistory()

State Management

// Set custom state
agent.SetStateData("user_id", "12345")
agent.SetStateData("preferences", map[string]interface{}{
    "language": "en",
    "theme":    "dark",
})

// Get state
userID := agent.GetStateData("user_id")
prefs := agent.GetStateData("preferences")

With Callbacks

agent := sdk.NewAgent(
    "agent",
    llmManager,
    logger,
    metrics,
    &sdk.AgentOptions{
        OnToolCall: func(tool *sdk.Tool, result interface{}) {
            log.Printf("Tool %s returned: %v", tool.Name, result)
        },
        OnComplete: func(result *sdk.Result) {
            log.Printf("Response: %s (tokens: %d)", 
                result.Content, result.Usage.TotalTokens)
        },
        OnError: func(err error) {
            log.Printf("Error: %v", err)
        },
    },
)

Real-World Examples

Customer Support Agent

func createSupportAgent(userID string) *sdk.Agent {
    agent := sdk.NewAgent(
        fmt.Sprintf("support-%s", userID),
        llmManager,
        logger,
        metrics,
        &sdk.AgentOptions{
            Model:      "gpt-4",
            StateStore: stateStore,
            SystemMessage: `You are a helpful customer support agent.
                Access order history and help resolve issues.`,
        },
    )
    
    agent.RegisterTool(&sdk.Tool{
        Name: "get_orders",
        Handler: func(ctx context.Context, args map[string]interface{}) (interface{}, error) {
            return database.GetUserOrders(userID)
        },
    })
    
    agent.RegisterTool(&sdk.Tool{
        Name: "create_ticket",
        Handler: func(ctx context.Context, args map[string]interface{}) (interface{}, error) {
            return ticketSystem.CreateTicket(userID, args)
        },
    })
    
    return agent
}

Research Agent

researchAgent := sdk.NewAgent("researcher", llm, logger, metrics, 
    &sdk.AgentOptions{
        SystemMessage: "You are a research assistant.",
    },
)

researchAgent.RegisterTool(&sdk.Tool{
    Name: "search_papers",
    Handler: func(ctx context.Context, args map[string]interface{}) (interface{}, error) {
        return searchAcademicPapers(args["query"].(string))
    },
})

researchAgent.RegisterTool(&sdk.Tool{
    Name: "summarize_paper",
    Handler: func(ctx context.Context, args map[string]interface{}) (interface{}, error) {
        return summarizePaper(args["paper_id"].(string))
    },
})

Next Steps

How is this guide?

Last updated on