How to Build a Persistent AI Agent from Scratch on Ubuntu
This guide walks through setting up a persistent AI agent on Ubuntu, covering why persistent agents outperform stateless chat tools, a breakdown of their core capabilities, environment preparation, deployment and configuration, memory and skill practices, observability, cost control, and common pitfalls. You will find practical commands, security baselines, and operational advice for running a self-hosted agent that can execute tasks, retain context, and integrate with real workflows.
1. Why Choose a Persistent Agent Instead of One-Off Conversations
Traditional conversational tools are essentially stateless Q&A: once a session ends, the context is discarded, and users have to restate the background for every new question. A Persistent Agent externalizes state to the server and turns four capabilities, memory, skills, scheduling, and communication, into reusable assets. As a result, the agent evolves the longer it is used. It is released by an open-source community under a permissive license, and its core logic can be self-hosted, which makes it suitable for teams with requirements around data boundaries and control.
The value of this kind of system is not that it chats better. It is that it connects the agent to a real working environment: it can execute commands, read and write files, search code, trigger tasks on a schedule, and push results to the channels where people communicate.
2. Breaking Down the Capability Model
- Closed-loop skill accumulation: after a task is completed, it is automatically abstracted into a reusable skill that can be called directly in similar tasks, reducing repeated prompt engineering.
- Memory and user modeling: long-term memory is carried by a local full-text search index, combined with a user profile layer that records preferences, project background, and historical context.
- Multi-channel message gateway: a single instance can mount more than ten types of channels at the same time, including instant messaging, email, smart home, and office collaboration.
- Pluggable models: switching between different vendors or local inference services is done through configuration, with no changes to business code.
- Diverse execution backends: local processes, containers, remote hosts, and serverless persistent execution are all options, balancing isolation and cost.
- Terminal interaction experience: multi-line editing, slash command completion, real-time token usage display, and streaming tool output.
- Built-in scheduling: periodic tasks are described in natural language, then triggered by the scheduler and returned through message channels.
- Protocol extension: support for the open Model Context Protocol to connect external tools and data sources.
3. Preparing the Ubuntu Environment
Ubuntu 22.04 or later is recommended. A minimum of 2 cores and 4 GB of memory is suggested; local inference requires a GPU and more memory. The basic steps are as follows (illustrative):
sudo apt update && sudo apt install -y git curl build-essential# Install the runtime and container runtimesudo apt install -y python3-venv# Create a dedicated low-privilege account to avoid running the agent as rootsudo adduser agent && sudo usermod -aG docker agent
Security baseline: open only the necessary ports, use key-based login and disable password login; apply least privilege to the agent account; keep all keys in environment variables or a key management service, never in the repository.
4. Core Deployment and Configuration
After obtaining the source code, install dependencies in an isolated environment to avoid polluting the system Python:
git clone <your-internal-repo> hermes && cd hermespython3 -m venv .venv && source .venv/bin/activatepip install -e .
The first initialization generates a configuration directory. Key fields include the model provider and key, the memory store path, the execution backend type, and message channel toggles. It is recommended to first get a minimal loop running with a “local backend + single channel”, then gradually enable container isolation and multiple channels.
Switching models usually only requires modifying the configuration or running a built-in command, for example:
hermes model set provider=compatible_api base_url=<internal-endpoint> model=<name>
5. Memory, Skills, and Scheduling in Practice
For the memory layer, mount the index files and user modeling data to a separate volume to make backup and migration easier; perform incremental backups regularly to avoid losing long-term context. Skill accumulation needs a review mechanism: spot-check automatically generated skills to prevent incorrect experience from being reused repeatedly.
Scheduled task example (natural language to schedule):
Every day at 09:00, check service health and disk usage, and push a summary to the operations group when anomalies are found
Under containerized deployment, the scheduler must be decoupled from the execution backend to ensure tasks run in an isolated environment, and logs must be desensitized to redact tokens and personal information when results are returned.
6. Observability and Cost Governance
It is recommended to integrate a general-purpose logging service and monitoring/alerting categories, and build dashboards for call volume, failure rate, token consumption, and task latency; set quotas and circuit breakers to avoid resource waste caused by abnormal loops. If elasticity is needed, deploy the stateless parts to a container platform and store memory and skill data in object storage or block storage, achieving “compute can drift, state is not lost”.
7. Common Pitfalls
Excessive permissions leading to accidental file deletion, hard-coded keys, unbounded memory store growth, duplicate and conflicting skills, message storms across channels, and scheduling offsets across time zones are all frequent problems. Before going live, be sure to complete regression testing in an isolated environment, and add secondary confirmation for high-risk operations.
