How to self-host your AI Agents
By David Ondrej
Key Concepts
- Self-hosting AI Agents: Running AI applications on a server that you control, as opposed to cloud-hosted or SaaS solutions.
- Virtual Private Server (VPS): A remote Linux server with dedicated RAM, CPU, and storage on a shared physical machine.
- SSH (Secure Shell): A protocol for securely accessing and managing remote computers via the terminal.
- SCP (Secure Copy Protocol): An SSH-based command-line tool for securely transferring files between local and remote machines.
- Cron Jobs: A time-based job scheduler in Unix-like operating systems used to automate tasks.
- AI Agents: Software programs that can perform tasks autonomously, often leveraging Large Language Models (LLMs).
- LLM (Large Language Model): A type of AI model trained on vast amounts of text data, capable of understanding and generating human-like text.
- One-shotting: A method of prompting an AI agent with a single, comprehensive instruction to achieve a desired outcome.
- Environment Variables (.env file): Files used to store sensitive information like API keys and configuration settings, which should be kept separate from code and version control.
- Git: A version control system used for tracking changes in code and collaborating on projects.
- Virtual Environment (venv/conda): Isolated Python environments that prevent dependency conflicts between different projects.
Building and Deploying an AI Agent on a VPS
This guide details the process of self-hosting an AI agent on a Virtual Private Server (VPS), offering greater control, affordability, and flexibility compared to cloud-hosted solutions. The process involves building a Python-based AI agent, setting up a VPS, and deploying the agent onto it.
1. Building a Simple AI Agent in Python
The first step is to create a functional AI agent using Python. The tutorial emphasizes using AI tools for coding, making it accessible even for beginners.
- Agent Idea: The agent is designed to scrape the Hacker News front page daily, identify the top 10 posts, and save them into a markdown file. This data can then be analyzed for insights.
- AI-Assisted Development: The presenter uses an AI coding assistant (Codex) to generate the project structure and Python scripts.
- Prompt Example: "build a simple project folder structure for the build idea in and then I'm going to tag the markdown file. The goal is to then deploy this agent on a VPS. So, let's use Python only."
- Generated Structure: Includes folders like
data,scripts,src, and an.envfile for environment variables.
- Hacker News Scraping: The agent is programmed to fetch content from Hacker News. The presenter notes that this scraping functionality is versatile and can be adapted to other websites like research archives or social media platforms.
- Local Testing: The agent is tested locally by:
- Activating a Conda environment (
conda activate testing). - Installing dependencies (
pip install -r requirements.txt). - Running the main script (
python scripts/run_digest.py).
- Result: The script successfully outputs the top 10 Hacker News posts.
- Activating a Conda environment (
2. Enhancing the Agent with LLM for Insight Generation
To transform the scraper into a true AI agent, an additional layer of analysis using a Large Language Model (LLM) is integrated.
- LLM Integration: The agent is enhanced to send the scraped Hacker News data to an LLM for analysis.
- LLM Choice: Perplexity is used for its balanced deep research capabilities, though other LLMs like ChatGPT, Claude, or Gemini are also viable.
- Prompt for LLM: "read the official open router docs and tell me how I can use GPT 5.1 thinking with medium reasoning effort." This research is saved into an
open_router.mdfile. - Agent Update Prompt: "read open router.mmd and update our project so that after we scrape the hacker news front page, we will run a single API call to GPT 5.1 thinking telling it to analyze these hacker news stories and extract actionable insights and trends in the AI case from that data and then we append the response from the LM into the same MD file."
- Benefits of LLM Analysis:
- Generates unique insights and trends from the scraped data.
- Provides a quick way to stay updated on AI developments by reviewing daily reports.
- The output is appended to the same markdown file, creating a comprehensive report.
- Version Control: Git is initialized (
git init) to track changes. A.gitignorefile is created to exclude sensitive files like the.envfile. Commits are made for initial setup and after adding LLM processing.- Key Argument: "You do not want to track your environment variables by git. That is a very bad habit."
- Local Testing with LLM:
- Reinstall dependencies to include new ones like the OpenAI SDK.
- Modify the code to use the
.envfile for API keys. - Configure OpenRouter API key (created via their website, treated as a password).
- Run the script again (
python scripts/run_digest.py).
- Error Handling and Debugging: An error related to missing proxy is encountered and resolved by reinstalling requirements.
- Code Tweaks:
- Reasoning effort for the LLM is set to "low" for faster execution.
- Four print statements are added to the code for better console logging and debugging.
- Final Output: The agent successfully scrapes Hacker News, generates AI insights (e.g., "AI infra consolidation," "Inference moves to the edge"), and appends them to the markdown file. The output is substantial, providing detailed analysis.
3. Self-Hosting on a Virtual Private Server (VPS)
The next phase is deploying the developed AI agent onto a self-controlled server.
- VPS Provider: Hostinger is recommended for its affordability and ease of use. The KVM2 plan is suggested as suitable for most users.
- Hostinger Offer: A 24-month plan is recommended for long-term agent operation, offering a discount and a free domain. A coupon code "David" provides an additional 10% off.
- VPS Setup:
- Select a server location closest to the user.
- Complete the checkout process and create a Hostinger account.
- Access the Hostinger panel, navigate to VPS, and click "Manage" to view server details.
- SSH Access:
- Concept: SSH (Secure Shell) is a protocol for secure remote access to a server via the terminal.
- Benefits: Remote control, secure encrypted connection, and file transfer capabilities.
- Accessing the VPS: The "root access" details from Hostinger are used to SSH into the server.
- Command Example:
ssh root@<your_vps_ip_address>(password is required).
- Command Example:
- Deploying the Agent:
- Create a Directory: A new directory for the agent is created on the VPS (
mkdir hacker_news_agent). - Install Prerequisites: Essential packages like Python 3,
venv, andpipare installed usingsudo apt updateandsudo apt install python3-venv python3-pip. The presenter notes that Hostinger pre-installs many dependencies, minimizing maintenance. - Copy Project Files: SCP is used to transfer the local project files to the VPS.
- Command Example:
scp -r /path/to/local/project root@<your_vps_ip_address>:/path/on/vps/
- Command Example:
- Set up Virtual Environment: A virtual environment is created and activated on the VPS (
python3 -m venv venv,source venv/bin/activate). - Install Dependencies on VPS:
pip install -r requirements.txtis run within the activated virtual environment on the VPS. - Test Agent on VPS: The agent is run on the VPS (
python scripts/run_digest.py) to confirm it functions correctly in the remote environment. The output confirms that the scraping and LLM analysis are happening on the VPS.
- Create a Directory: A new directory for the agent is created on the VPS (
4. Automating the Agent with Cron Jobs
To ensure the agent runs automatically on a schedule, cron jobs are configured.
- Cron Tab Editor: The
crontab -ecommand is used to open the cron job editor on the VPS. - Scheduling the Agent: A cron entry is added to run the agent daily at 9:00 AM.
- Cron Entry Example:
0 9 * * * /path/to/your/venv/bin/python /path/to/your/agent/scripts/run_digest.py >> /path/to/your/agent/logs/cron.log 2>&1(The presenter's example directly uses the Python script and assumes the API key is handled via the.envfile). - Keyboard Shortcuts:
Ctrl+Oto write and save,Ctrl+Xto exit the editor.
- Cron Entry Example:
- Verification:
crontab -lis used to list active cron jobs and confirm the entry has been saved.
Conclusion and Takeaways
The video successfully demonstrates how to build a functional AI agent using AI coding assistants, enhance it with LLM capabilities for insightful analysis, and then self-host it on a VPS for autonomous operation. The key takeaways are:
- Empowerment through Self-Hosting: Users gain full control over their data, privacy, and costs.
- Accessibility of AI Development: AI tools significantly lower the barrier to entry for building complex applications.
- Versatility of the Approach: The methodology is adaptable to various use cases beyond scraping Hacker News.
- Practicality of VPS: VPS offers a scalable and affordable solution for running AI agents continuously.
- Automation with Cron: Cron jobs enable scheduled, hands-off execution of AI agents.
The presenter emphasizes that with intention and the ability to leverage AI agents, individuals can build sophisticated solutions without being deep technical experts.
Chat with this Video
AI-PoweredHi! I can answer questions about this video "How to self-host your AI Agents". What would you like to know?