AgentLoop
TheAgentLoop class is the core processing engine of nanobot. It handles the complete agent lifecycle:
- Receives messages from the message bus
- Builds context with history, memory, and skills
- Calls the LLM provider
- Executes tool calls
- Sends responses back through the bus
Constructor
The message bus for receiving inbound messages and publishing outbound responses
The LLM provider interface for making chat completion calls
The workspace directory path for file operations, memory, and sessions
Model name to use. If None, uses the provider’s default model
Maximum number of tool call iterations per request
Temperature parameter for LLM sampling (0.0-1.0)
Maximum number of tokens in LLM response
Number of messages to keep in active memory before consolidation
Reasoning effort level for supported providers (e.g., “medium”, “high”)
API key for Brave Search integration
Proxy URL for web requests
Configuration for shell execution tool
Cron service for scheduling tasks
If True, restrict file operations to workspace directory only
Session manager for conversation persistence. Creates default if None
Configuration dict for MCP (Model Context Protocol) servers
Channel-specific configuration
Methods
run
- Connects to MCP servers on startup
- Stays responsive to
/stopcommands - Runs until
stop()is called
process_direct
The message content to process
Session identifier for conversation history
Channel name for routing
Chat identifier
Optional callback for progress updates during processing
The agent’s response text
stop
run() to exit after processing current message.
Example:
close_mcp
Built-in Tools
TheAgentLoop automatically registers these default tools:
- ReadFileTool: Read file contents
- WriteFileTool: Write files
- EditFileTool: Edit existing files
- ListDirTool: List directory contents
- ExecTool: Execute shell commands
- WebSearchTool: Search the web (requires Brave API key)
- WebFetchTool: Fetch web pages
- MessageTool: Send messages to specific channels
- SpawnTool: Spawn subagent tasks
- CronTool: Schedule cron jobs (if cron_service provided)
Slash Commands
The agent recognizes these built-in commands:/new- Start a new conversation (archives current session to memory)/stop- Stop the current task and cancel all running operations/help- Show available commands
Memory Consolidation
When the conversation exceeds thememory_window, the agent automatically:
- Consolidates old messages into long-term memory (MEMORY.md)
- Appends a summary to the history log (HISTORY.md)
- Keeps recent messages in active context
Architecture
The agent loop uses a single processing lock to ensure messages are handled sequentially, preventing race conditions in:- Session state updates
- Memory consolidation
- Tool execution with side effects