Tutorial: Building Autonomous Agents
What is an Autonomous Agent?
An autonomous agent is a system that can perceive its environment, reason about how to achieve a goal, and take actions (use tools) to accomplish that goal. Unlike a simple chatbot, an agent has a loop of "Thought -> Action -> Observation".
The Simple Recursive Agent (SRA)
Mojentic provides a SimpleRecursiveAgent pattern. This agent:
- Receives a goal.
- Thinks about the next step.
- Selects a tool to use.
- Executes the tool.
- Observes the result.
- Repeats until the goal is met.
Building an Agent
Let's build an agent that can answer questions using a web search tool.
1. Setup
You'll need the WebSearchTool (or any other tool) and a Broker.
typescript
import { LlmBroker, OllamaGateway, WebSearchTool, SimpleRecursiveAgent } from 'mojentic';
// Initialize broker
const gateway = new OllamaGateway();
const broker = new LlmBroker('qwen3:32b', gateway);
// Configure tools
const tools = [new WebSearchTool({ provider: 'tavily' })];2. Run the Agent
typescript
const goal = "Find out who won the latest Super Bowl and tell me the score.";
const result = await SimpleRecursiveAgent.run(broker, goal, tools);
console.log(`Final Answer: ${result}`);Step-by-Step Execution
When you run this, the agent enters a loop:
- Thought: "I need to search for the latest Super Bowl winner."
- Action: Calls
WebSearchToolwith query "latest Super Bowl winner score". - Observation: Receives search results (e.g., "Kansas City Chiefs defeated San Francisco 49ers 25-22...").
- Thought: "I have the information. I can now answer the user."
- Final Answer: "The Kansas City Chiefs won the latest Super Bowl with a score of 25-22."
Customizing the Agent
You can customize the agent's behavior by:
- Adding more tools: Give it file access, calculation abilities, etc.
- System Prompt: Adjust its personality or constraints.
- Max Iterations: Limit how many steps it can take to prevent infinite loops.
typescript
await SimpleRecursiveAgent.run(broker, goal, tools, {
maxIterations: 10
});Summary
Autonomous agents allow you to solve complex, multi-step problems. By combining a reasoning loop with a set of tools, you can build systems that can interact with the world to achieve user goals.