How to Host an AI Agent on a VPS
What does hosting an AI agent mean?
Section titled “What does hosting an AI agent mean?”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.
1. Create a dedicated service account
Section titled “1. Create a dedicated service account”sudo apt updatesudo apt install -y python3 python3-venvsudo useradd --system --home-dir /opt/ai-worker --create-home \ --shell /usr/sbin/nologin aiworker || truesudo -u aiworker python3 -m venv /opt/ai-worker/venvsudo -u aiworker /opt/ai-worker/venv/bin/pip install openai2. Store the API key securely
Section titled “2. Store the API key securely”Create /etc/ai-worker.env as root:
OPENAI_API_KEY=YOUR_SECRET_API_KEYOPENAI_MODEL=YOUR_AVAILABLE_MODEL_IDsudo chown root:root /etc/ai-worker.envsudo chmod 600 /etc/ai-worker.envUse a supported model ID from your API account. Keep the key out of Git, logs and public HTTP endpoints.
3. Create the worker
Section titled “3. Create the worker”Save /opt/ai-worker/worker.py:
import osfrom 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)sudo chown aiworker:aiworker /opt/ai-worker/worker.py4. Schedule it with systemd
Section titled “4. Schedule it with systemd”Create /etc/systemd/system/ai-worker.service:
[Unit]Description=Scheduled AI workerAfter=network-online.targetWants=network-online.target
[Service]Type=oneshotUser=aiworkerWorkingDirectory=/opt/ai-workerEnvironmentFile=/etc/ai-worker.envExecStart=/opt/ai-worker/venv/bin/python /opt/ai-worker/worker.pyNoNewPrivileges=trueProtectSystem=strictProtectHome=truePrivateTmp=trueCreate /etc/systemd/system/ai-worker.timer:
[Unit]Description=Run AI worker every day[Timer]OnCalendar=*-*-* 09:00:00Persistent=true[Install]WantedBy=timers.targetStart the timer and test a single run:
sudo systemctl daemon-reloadsudo systemctl enable --now ai-worker.timersudo systemctl start ai-worker.servicesudo journalctl -u ai-worker.service -n 30 --no-pager5. Make it production-ready
Section titled “5. Make it production-ready”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.