Prebuilt Agents
While LangGraphGo provides low-level capabilities to build arbitrary graphs, for many common tasks, we don't need to start from scratch. LangGraphGo provides a series of prebuilt Agent factory functions that encapsulate best practices, allowing you to get started quickly.
1. ReAct Agent
Background & Functionality
ReAct (Reason + Act) is one of the most popular Agent patterns. It allows LLM to reason and call tools to acquire information before generating a response. This loop (Think -> Act -> Observe -> Think) enables Agents to solve problems requiring external knowledge or computational power.
Implementation Principle
CreateReactAgent internally builds a graph with two main nodes:
- Agent Node: Calls LLM, deciding whether to reply to user or call tools based on current state.
- Tools Node: Executes tool calls requested by LLM and returns results to Agent.
These two nodes are connected by conditional edges, forming a closed loop until LLM decides to end.
Code Showcase
import "github.com/smallnest/langgraphgo/prebuilt"
// 1. Define Tools
tools := []llms.Tool{
// ... Your tool definitions ...
}
// 2. Create Agent
// This function automatically builds graph, adds nodes and edges
agent := prebuilt.CreateReactAgent(model, tools)
// 3. Run
res, err := agent.Invoke(ctx, input)
2. General Agent (CreateAgent)
Background & Functionality
Sometimes you need more flexible configuration than ReAct, such as customizing System Message, adding
Checkpointer, or modifying State Schema. CreateAgent provides a more general entry point.
Implementation Principle
It uses Go's Option pattern to receive configuration. The underlying logic is similar to ReAct but allows more customization.
Code Showcase
agent := prebuilt.CreateAgent(
model,
tools,
// Custom System Message
prebuilt.WithSystemMessage("You are an assistant focused on math problems."),
// Enable Persistence
prebuilt.WithCheckpointer(checkpointer),
)
Skills Integration v0.4.0
Starting from v0.4.0, CreateAgent supports dynamic skill loading. Skills are functional modules that can be loaded and executed on demand, supporting Claude Skills and other skill systems.
// Create an agent with skills support
agent := prebuilt.CreateAgent(
model,
tools,
prebuilt.WithSkills(skillsConfig), // Enable skills support
)
// The agent can automatically discover and load appropriate skills based on user requests
res, err := agent.Invoke(ctx, map[string]any{
"messages": []string{"Use the calculator skill to help me calculate 123 * 456"},
})
See the dynamic_skill_agent example for detailed usage.
3. Supervisor
Background & Functionality
As described in the "Multi-Agent Systems" guide, Supervisor is used to coordinate multiple Worker Agents.
The prebuilt CreateSupervisor saves you from manually writing complex routing logic.
Implementation Principle
It creates a specialized Supervisor node, which uses a special Prompt to guide the LLM to output the name of the next Worker. It also handles state passing and result aggregation.
Code Showcase
supervisor := prebuilt.CreateSupervisor(
model,
[]string{"Researcher", "Writer", "Reviewer"}, // Worker List
prebuilt.WithSupervisorSystemMessage("Manage researchers, writers, and reviewers..."),
)