Article
uncategorizedai-agentsvpsircpythondeployment
Show HN: I put an AI agent on a $7/month VPS with IRC as its transport layer
Deploy an AI agent on a $7/month VPS, leveraging IRC as a lightweight communication layer. This allows for cost-effective AI operations, ideal for independent developers and hobbyists.
intermediate30 min5 steps
The play
- Provision a Low-Cost VPS and ConnectAcquire a basic VPS (e.g., Ubuntu 22.04 LTS, 1 CPU, 1GB RAM) from providers like Vultr or DigitalOcean. Once provisioned, use SSH to connect to your server.
- Install Python and IRC LibraryUpdate your system packages, then install Python 3, pip, and the `irc` Python library required for building the bot.
- Create the IRC AI Agent ScriptUsing a text editor like `nano`, create a new Python file (e.g., `irc_ai_agent.py`) and paste the provided 'Copy-Paste Starter' code into it. This script contains both the simple AI logic and the IRC bot framework.
- Configure and Run Your AgentEdit the `SERVER`, `CHANNEL`, and `NICKNAME` variables in the `irc_ai_agent.py` script to match your desired IRC network, channel, and bot name. Then, execute the script to start your AI agent.
- Ensure Persistent Operation (Optional)To keep your AI agent running even after you disconnect from SSH, use a terminal multiplexer like `screen` or `tmux`. Start a new session, run your bot, then detach from the session.
Starter code
import irc.client
import irc.bot
import time
import datetime
# --- AI Core Logic (simplified for starter) ---
def generate_ai_response(message_content: str) -> str:
"""
Placeholder for your AI's response generation logic.
This could call an LLM API or use local processing.
"""
message_content_lower = message_content.lower()
if "hello" in message_content_lower:
return "Hello there! How can I assist you today?"
elif "time" in message_content_lower:
return f"The current time is {datetime.datetime.now().strftime('%H:%M:%S')} UTC."
elif "how are you" in message_content_lower:
return "I am a bot, functioning optimally!"
else:
return f"You said: '{message_content}'. I'm still learning!"
# --- IRC Agent Logic ---
class IRAIAgent(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
self.nickname = nickname
print(f"Connecting to IRC server {server}:{port} as {nickname} in channel {channel}...")
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
print(f"Connected to {e.source}. Joining channel {self.channel}...")
c.join(self.channel)
def on_pubmsg(self, c, e):
message = e.arguments[0]
sender_nick = e.source.nick
# Respond if bot's nickname is mentioned at the start or anywhere in the message
if message.startswith(self.nickname + ":"):
query = message[len(self.nickname + ":"):].strip()
print(f"[{self.channel}] <{sender_nick}> {message}")
response = generate_ai_response(query)
c.privmsg(self.channel, f"{sender_nick}: {response}")
elif self.nickname.lower() in message.lower():
print(f"[{self.channel}] <{sender_nick}> {message}")
response = generate_ai_response(message)
c.privmsg(self.channel, f"{sender_nick}: {response}")
def on_privmsg(self, c, e):
message = e.arguments[0]
sender_nick = e.source.nick
print(f"[PM] <{sender_nick}> {message}")
response = generate_ai_response(message)
c.privmsg(sender_nick, response)
def main():
# --- Configuration: CHANGE THESE VALUES ---
SERVER = "irc.libera.chat" # Example IRC server
PORT = 6667
CHANNEL = "#your_channel_name" # e.g., "#mybot_test". Must start with '#'
NICKNAME = "MyAIBot" # Your bot's desired nickname
# -----------------------------------------
bot = IRAIAAgent(CHANNEL, NICKNAME, SERVER, PORT)
try:
bot.start()
except KeyboardInterrupt:
print("\nBot stopped by user.")
except Exception as ex:
print(f"An error occurred: {ex}")
if __name__ == "__main__":
main()