Agents
Agents enable autonomous behavior by allowing language models to dynamically choose which tools to use based on user input. Unlike predetermined chains, agents make real-time decisions about their actions.
Core Concepts
An agent system consists of several key components:
- Agent: The decision-making component that chooses which tools to use
- Tools: The available functions/APIs the agent can invoke
- Executor: Manages the agent's execution loop and tool invocation
- Memory: Maintains conversation context across agent interactions
How Agents Work
- Receive Input: Agent receives a user query or task
- Plan Action: Agent analyzes the input and decides which tool(s) to use
- Execute Tool: Agent invokes the selected tool with appropriate parameters
- Process Result: Agent evaluates the tool's output
- Decide Next Step: Agent determines if more actions are needed or if the task is complete
- Respond: Agent provides a final response to the user
Agent Types
MRKL Agent (ReAct)
Uses a Reasoning and Acting pattern where the agent alternates between thinking about what to do and taking actions.
OpenAI Functions Agent
Leverages OpenAI's function calling capabilities for more structured tool usage and parameter passing.
Conversational Agent
Designed for multi-turn conversations, maintaining context while using tools to assist with tasks.
Plan-and-Execute Agent
Creates a plan of actions first, then executes each step systematically.
Available Tools
LangChainGo provides several built-in tools:
- Calculator: Perform mathematical calculations
- Web Search: Search the internet for information
- File Operations: Read, write, and manipulate files
- Database Query: Execute database operations
- API Calls: Make HTTP requests to external services
- Custom Tools: Create your own tools for specific use cases
Building Agents
Basic Agent Setup
// Create tools
tools := []tools.Tool{
tools.Calculator{},
tools.WebSearch{APIKey: "your-api-key"},
}
// Create agent
agent := agents.NewOneShotAgent(llm, tools)
// Create executor
executor := agents.NewExecutor(agent)
// Execute
result, err := executor.Call(ctx, map[string]any{
"input": "What is 25 * 4 and what's the weather like today?",
})
Custom Tools
type CustomTool struct{}
func (c CustomTool) Name() string {
return "custom_tool"
}
func (c CustomTool) Description() string {
return "Performs custom operations"
}
func (c CustomTool) Call(ctx context.Context, input string) (string, error) {
// Your custom logic here
return "Tool result", nil
}
Best Practices
- Tool Selection: Choose tools that complement each other and cover your use case
- Prompt Engineering: Design clear tool descriptions and agent prompts
- Error Recovery: Implement fallback strategies for tool failures
- Resource Management: Set timeouts and limits on tool execution
- Security: Validate tool inputs and sanitize outputs
- Monitoring: Track agent performance and decision quality
Agent Components
📄️ Agents
Conceptual Guide
🗃️ Agent Executors
1 item
📄️ Tools
Conceptual Guide
📄️ Agents
Conceptual Guide • How-to Guides