# Examples Practical examples demonstrating AgentKit usage patterns URL: https://agentkit.tech/docs/examples.mdx Learn AgentKit through practical, runnable examples that showcase different use cases and integration patterns. Each example is designed to be educational and ready for production adaptation. ## Overview AgentKit examples are organized into two categories: ### **Framework Examples** Demonstrate core AgentKit features and patterns: * Basic agent creation and configuration * Session management and conversation state * Streaming responses and event handling * Tool integration and function calling ### **Business Use Cases** Real-world applications ready for production: * Sales automation and lead qualification * Customer support and ticketing * Research and analysis workflows * Domain-specific agent implementations ## Quick Start ### Prerequisites ```bash # Set your OpenAI API key export OPENAI_API_KEY="your-api-key-here" # Navigate to any example cd examples/simple_agent # Run the example go run . ``` ### Basic Agent Pattern All examples follow this core pattern: ```go // 1. Create and configure model model := model.Model("gpt-4o"). SetAPIKey(os.Getenv("OPENAI_API_KEY")). SetTemperature(0.7) // 2. Create agent with tools agent := agent.New(). SetModel(model). SetSystemPrompt("You are a helpful assistant"). AddTool(tool1). AddTool(tool2) // 3. Run with session for conversation state session := session.New(agent) response, err := session.Run(ctx, []agent.ChatMessage{ agent.NewUserMessage("Hello!"), }, nil) ``` ## Featured Examples ### Simple Agent **File**: `examples/simple_agent/main.go` Perfect starting point demonstrating: * Basic agent creation and configuration * Tool integration (greeting and calculator) * Session-based execution * Error handling patterns ```go // Create tools greetingTool := tool.NewFunctionTool( "greeting", "Greet a person by name", greetingHandler, ) calculatorTool := tool.NewFunctionTool( "calculator", "Perform basic math operations", calculateHandler, ) // Create agent agent := agent.New(). SetModel(model). SetSystemPrompt("You are a helpful assistant with greeting and calculator tools."). AddTool(greetingTool). AddTool(calculatorTool) ``` ### Agent Session **File**: `examples/agent_session/main.go` Demonstrates advanced session features: * Multi-turn conversations with memory * Streaming responses with real-time output * Advanced tool usage (calculator with multiple operations) * Event handling and completion detection ```go // Streaming with session sess := session.New(agent) stream, err := sess.RunStreamed(ctx, []agent.ChatMessage{ agent.NewUserMessage(input), }, nil) for event, err := range stream.StreamEvents() { if err != nil { break } switch { case event.IsTextDelta(): fmt.Print(event.GetContent()) case event.IsCompleted(): fmt.Println("\n✅ Response completed!") } } ``` ### Sales Agent **File**: `examples/sales_agent/main.go` Production-ready sales automation: * BANT (Budget, Authority, Need, Timeline) qualification * Lead scoring and prioritization * Product catalog and pricing tools * Professional sales conversation flows ### Support Agent **File**: `examples/support_agent/main.go` Customer service automation: * Knowledge base integration * Ticket creation and management * Issue classification and routing * Customer lookup and history ### Research Agent **File**: `examples/research_agent/main.go` Intelligent research assistant: * Multi-source information gathering * Data analysis and synthesis * Report generation * Industry-specific research domains ## Running Examples ### Method 1: Direct Execution ```bash # Run any example directly cd examples/simple_agent go run . ``` ### Method 2: Build and Run ```bash # Build first, then run cd examples/sales_agent go build ./sales_agent ``` ### Method 3: Test Mode ```bash # Some examples support test scenarios cd examples/support_agent go run . --test ``` ## Example Architecture ### Common Patterns All examples demonstrate these patterns: #### **Model Configuration** ```go model := model.Model("gpt-4o"). SetAPIKey(os.Getenv("OPENAI_API_KEY")). SetTemperature(0.7). // Adjust for use case SetMaxTokens(1000) // Control response length ``` #### **Tool Creation** ```go // Define input structure type ToolInput struct { Param string `json:"param" jsonschema:"description=Parameter description"` } // Implement handler func toolHandler(ctx common.RunContext, input ToolInput) (string, error) { // Tool logic here return result, nil } // Create tool tool := tool.NewFunctionTool("tool_name", "description", toolHandler) ``` #### **Session Management** ```go // For stateful conversations session := session.New(agent) // Multiple interactions maintain context response1, _ := session.Run(ctx, []agent.ChatMessage{agent.NewUserMessage("First question")}, nil) response2, _ := session.Run(ctx, []agent.ChatMessage{agent.NewUserMessage("Follow-up question")}, nil) ``` #### **Error Handling** ```go response, err := session.Run(ctx, messages, nil) if err != nil { log.Printf("Agent error: %v", err) // Handle gracefully return } ``` ## Next Steps 1. **Start Simple**: Run `simple_agent` to understand basics 2. **Try Streaming**: Explore `agent_session` for real-time responses 3. **Business Use Cases**: Adapt `sales_agent` or `support_agent` for your needs 4. **Build Custom**: Create your own agent using these patterns ## Source Code All examples are available in the [`examples/`](https://github.com/model-box/agent-kit/tree/main/examples) directory with: * Complete, runnable code * Detailed README files * Configuration examples * Best practices demonstrations Explore the examples to learn AgentKit patterns and build your own intelligent agents!