Skip to content

How to Host an AI Agent on a VPS

An AI agent on a VPS is typically a process that calls an AI model, receives results and performs explicitly authorized tasks on a schedule. A hosted model API does not require local GPU resources, but model calls are billed by the API provider. This beginner example creates a scheduled, text-only AI worker; it does not grant the model shell or unrestricted file access.

Requirements: Ubuntu 24.04, Python 3, SSH/sudo, and an API key issued by your model provider. Do not use a ChatGPT website password or subscription credentials as an API key.

Terminal window
sudo apt update
sudo apt install -y python3 python3-venv
sudo useradd --system --home-dir /opt/ai-worker --create-home \
--shell /usr/sbin/nologin aiworker || true
sudo -u aiworker python3 -m venv /opt/ai-worker/venv
sudo -u aiworker /opt/ai-worker/venv/bin/pip install openai

Create /etc/ai-worker.env as root:

OPENAI_API_KEY=YOUR_SECRET_API_KEY
OPENAI_MODEL=YOUR_AVAILABLE_MODEL_ID
Terminal window
sudo chown root:root /etc/ai-worker.env
sudo chmod 600 /etc/ai-worker.env

Use a supported model ID from your API account. Keep the key out of Git, logs and public HTTP endpoints.

Save /opt/ai-worker/worker.py:

import os
from openai import OpenAI
client = OpenAI()
result = client.responses.create(
model=os.environ["OPENAI_MODEL"],
instructions=("Summarize the input in plain English. "
"Never invent factual claims; say when data is missing."),
input="Daily status: backup completed; disk usage 42%.",
)
print(result.output_text)
Terminal window
sudo chown aiworker:aiworker /opt/ai-worker/worker.py

Create /etc/systemd/system/ai-worker.service:

[Unit]
Description=Scheduled AI worker
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
User=aiworker
WorkingDirectory=/opt/ai-worker
EnvironmentFile=/etc/ai-worker.env
ExecStart=/opt/ai-worker/venv/bin/python /opt/ai-worker/worker.py
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true

Create /etc/systemd/system/ai-worker.timer:

[Unit]
Description=Run AI worker every day
[Timer]
OnCalendar=*-*-* 09:00:00
Persistent=true
[Install]
WantedBy=timers.target

Start the timer and test a single run:

Terminal window
sudo systemctl daemon-reload
sudo systemctl enable --now ai-worker.timer
sudo systemctl start ai-worker.service
sudo journalctl -u ai-worker.service -n 30 --no-pager

Never place secrets or personal data in model prompts without authorization. Limit API spending and request frequency, validate outputs, and require human approval before consequential actions. Store useful results in a database or send them to a notification channel with appropriate access controls. Never let untrusted model output execute arbitrary shell commands.

To build a web-facing agent, add authentication, rate limiting, audit logs and HTTPS before exposing an API. See also deploying web applications on VPS.

Official reference: OpenAI Responses API.


Need a VPS for this project? Compare ProstoHosting VPS plans.