Build a Self-Evolving AI Agent from Scratch and Connect It to Personal WeChat
This guide walks through building a self-evolving AI agent from the ground up and connecting it to personal WeChat. It explains what "self-evolving" actually means, the four-stage loop of perception, planning and execution, reflection, and consolidation, and the environment requirements including operating systems, hardware, credentials, and context window sizing. You will find configuration examples for the model provider, message gateway, and skill library, plus practical notes on installation, compliance and account risk, security, operations, cost control, and common troubleshooting scenarios.
1. First, Define the Concept: What Is a “Self-Evolving” Agent
Over the past couple of years, AI Agents have evolved from “chatbots” into workflow execution engines that actually get work done. “Self-evolving” does not mean the model weights are rewritten locally in real time. It means that after a task finishes, the agent distills the process data into reusable skill entries and operating patterns. On later, similar tasks, it retrieves and reuses that experience first, which shows up in practice as the agent getting smoother the more you use it.
A typical self-evolving loop has four stages:
- Perception: receive task instructions from a messaging channel or a scheduled trigger;
- Planning and execution: break the task down, then call the terminal, the file system, retrieval capabilities, and external tool interfaces;
- Reflection: validate the execution result and extract the successful path as well as the reasons for failure;
- Consolidation: write the experience into the skill library, turning it into a searchable, versionable knowledge asset.
Compared with an ordinary conversational system, the key difference is that it “has hands and feet”: it can read and write local files and run scripts, it can connect to external systems through a standard tool protocol, and it supports scheduled (cron) tasks that run unattended.
2. Environment Setup and Compatibility Boundaries
Before you start, pin down the constraints of your runtime environment. This step prevents most “can’t install it, can’t run it” problems.
- Operating system: mainstream Linux distributions, macOS, and the Linux subsystem on Windows (WSL2) all work; native Windows is generally not supported directly, so you need to enter the Linux subsystem first.
- Mobile and lightweight environments: Android can run in a terminal emulator environment, which is a common lightweight deployment approach in the community.
- Hardware requirements: because the scheduling layer is decoupled from model inference, the local machine only carries the orchestration and tool execution overhead. A cheap cloud host or a small home machine is enough.
- Accounts and credentials: one instant messaging account that can log in by scanning a QR code, and one LLM API key.
On model selection, pay close attention to the context window. During planning, the agent carries the system prompt, conversation history, tool return values, and retrieved skill library fragments. A model with a context capacity of at least 64K tokens is recommended. Too small a capacity truncates early instructions, which shows up as “forgetting the rules and repeating the same mistakes.”
3. Installation and Initialization
In Linux, macOS, and Android terminal environments, you can use the official one-click install script to pull dependencies and initialize the environment. After installation, reload your shell configuration so the environment variables take effect:
# Run the official install script (illustrative)curl -fsSL <official-install-url> | bash# Apply the configuration immediatelysource ~/.bashrc # zsh users run source ~/.zshrc
Windows users should first enable the Linux subsystem as administrator:
wsl --install
After rebooting, set a username and password as prompted, then enter the Linux environment and repeat the steps above.
Once installation is done, run a quick self-check: confirm the runtime version, verify that dependencies are complete, and make sure the configuration file path is writable. If you isolate dependencies in a virtual environment, be sure to activate it explicitly in the startup script. Otherwise you get the classic mystery of “works fine when I run it manually, errors out as a background service.”
4. Connecting the LLM Capability
The configuration file usually contains three core pieces of information: the model service address, the key, and the model parameters.
model:base_url: "https://<model-service-url>/v1"api_key: "${LLM_API_KEY}"name: "<model-name>"context_window: 65536temperature: 0.3agent:max_steps: 12 # Max reasoning steps per task, prevents infinite loopstool_timeout: 30 # Tool call timeout (seconds)skill_recall_top_k: 3 # Number of skill library entries recalled
Practical recommendations:
- Inject the key through an environment variable; do not hardcode it into the repository;
- A lower temperature helps keep tool call parameters stable;
- Setting a maximum step count and a timeout is key to controlling cost and avoiding “infinite retries”;
- If you use a self-hosted inference service or a cloud model service that is compatible with the OpenAI protocol, you only need to replace
base_urland the model name.
5. Configuring the Message Gateway and Personal WeChat Access
The agent’s “reach” is provided by the message gateway layer, commonly in the form of plugin-style adapters: one adapter per channel, all normalized into a single internal message structure.
channels:wechat:enabled: truemode: "qr" # QR code login modesession_dir: "./data/session"telegram:enabled: false
The general flow for connecting personal WeChat:
- Start the gateway service, which outputs a login QR code;
- Scan the code with your phone to confirm, and the session credentials are written to a local directory;
- Wake the agent in a chat window with an @mention or a prefix command;
- Configure a whitelist so it only responds to authorized contacts and groups.
Compliance and risk control must be emphasized here: automated login to a personal account relies on third-party protocols and carries a risk of the account being restricted. It is best enabled only for small-scale personal use. Keep message frequency under control, avoid mass messaging and marketing use cases, and store session data encrypted locally.
6. Making It “Smarter with Use”: Skill Library Governance
Skill entries are best written as structured descriptions so they are easy to search and deduplicate:
{"name": "daily_report","trigger": "generate daily report","steps": ["pull data", "aggregate metrics", "render template", "send"],"success_rate": 0.92,"version": 3}
Governance essentials: periodically merge similar skills, retire low-success-rate entries, and require human confirmation for high-risk operations such as deleting files or executing scripts.
7. Security, Operations, and Cost
- Isolation: run in a container or under a dedicated user, and limit file access to the working directory;
- Auditing: log tool calls and their parameters in full so they can be traced back;
- Monitoring: alert on task success rate, average step count, and token consumption;
- Cost: schedule tasks at off-peak times and cache repeated retrieval results.
8. Frequently Asked Questions
- Disconnects after scanning the QR code: check the permissions on the session directory and the stability of your network egress;
- Tool calls failing: usually parameter validation or timeouts, so increase the timeout first and then investigate;
- “Amnesia” in answers: insufficient context, so upgrade to a model with a larger window or trim the system prompt.
Once you have followed these steps, you will have a digital assistant you can command remotely, that can operate the terminal and files, and that supports scheduled tasks. If you run it on a cloud platform such as Baidu AI Cloud, you can also combine object storage, log services, and monitoring alerts to round out persistence and observability.
