Build an IRC Bot with AI: Running a Local LLM with Ollama in Chat
What this article covers about AI bots in IRC
- Why Internet Relay Chat and local AI are a natural fit.
- The architecture: IRC client bot meets Ollama API.
- A complete Python boilerplate to build yourself.
- Conversation memory, moderation, and flood protection.
- Security and common pitfalls.
Introduction: Old school meets new AI
IRC bots predate most programming languages in use today. Eggdrops moderated channels before “chatbot” was even a term. What’s changed: bots used to react to fixed triggers; now a local LLM can answer free-form questions. That’s exactly what makes this interesting for BotServ.de. A KI bot in IRC is probably the most honest exercise in bot development you can do. No framework magic, no cloud lock-in, just a socket, a protocol, and a model running on your own hardware.
The practical value is obvious: support bot in a community channel, documentation assistant for a project, coding help in a developer channel. And because the model runs locally via Ollama, your community’s conversations stay off any external service.
Architecture: How the AI bot works in IRC
The principle is pleasantly simple. Your bot is a regular IRC client that connects with a nick and password, joins one or more channels, and listens for messages. When a message addresses it, the bot sends the text to the Ollama API, gets a response back, and posts it to the channel.
IRC Network <--Socket/TLS--> Your Bot (Python) <--HTTP--> Ollama (local LLM)
Three components, three responsibilities: the IRC part handles protocol and connection, the bot logic decides when to respond, and Ollama provides the intelligence. If you want to dive deeper into bot programming, IRC-Coding.de has over 500 articles on Eggdrops, clients, and chatbots, including AI topics.
Prerequisites for your AI IRC bot
- Ollama installed with a model, such as
ollama pull qwen3orllama3.2. A GPU with 8 GB VRAM is enough for smaller models; details in our Local AI Learning Path. - Python 3.10+ on the same machine or a server on your network.
- Access to an IRC network: your own server (see our article on IRC daemons) or a public network that allows bots. Some networks require bot registration.
- Registered nick for the bot so it remains identifiable.
The bot in Python: The boilerplate
IRC is a plaintext protocol; you don’t even need a library. Here’s a minimal version with standard sockets:
import socket
import requests
SERVER = "irc.your-network.de"
PORT = 6697
NICK = "kibot"
CHANNEL = "#your-channel"
OLLAMA_URL = "http://localhost:11434/api/chat"
MODEL = "qwen3"
history = {}
def ask_llm(user, text):
msgs = history.setdefault(user, [])
msgs.append({"role": "user", "content": text})
msgs = msgs[-10:]
history[user] = msgs
r = requests.post(OLLAMA_URL, json={
"model": MODEL,
"messages": [{"role": "system", "content":
"You are a helpful bot in an IRC channel. "
"Reply briefly, at most two sentences."}] + msgs,
"stream": False,
})
reply = r.json()["message"]["content"]
history[user].append({"role": "assistant", "content": reply})
return reply
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# For production: ssl.wrap_socket or ssl.create_default_context()
irc.connect((SERVER, PORT))
def send(line):
irc.send((line + "\r\n").encode())
send(f"NICK {NICK}")
send(f"USER {NICK} 0 * :AI Bot")
buf = ""
while True:
buf += irc.recv(4096).decode(errors="ignore")
lines, buf = buf.split("\r\n")[:-1], buf.split("\r\n")[-1]
for line in lines:
if line.startswith("PING"):
send("PONG " + line.split()[1])
if " 376 " in line or " 422 " in line: # MOTD end
send(f"JOIN {CHANNEL}")
if f"PRIVMSG {CHANNEL}" in line and f"!ki " in line:
user = line.split("!")[0].lstrip(":")
text = line.split("!ki ", 1)[1].strip()
send(f"PRIVMSG {CHANNEL} :{user}: {ask_llm(user, text)}")
This is intentionally minimal: connect, play PING/PONG, respond to !ki question, post the answer. For production use, pick a library like irc (pip install irc) or pydle, which handle reconnects, SASL, and TLS cleanly.
Refinements: From toy to usable bot
Trigger design. The !ki prefix prevents the bot from answering every line. Alternatively, it responds to its nick (“kibot: question”), which feels more natural but requires careful parsing.
Per-user memory. The example above keeps the last ten messages per nick. Beware of nick changes: memory ties to the nick, not the account. For production bots, map to Services accounts.
Rate limiting. Essential: at most one request per user per second, queue the rest. Otherwise your bot becomes a flood problem itself and lands on a ban list.
Cap response length. IRC messages are limited to about 512 bytes per line. Long LLM responses need splitting into lines of roughly 400 characters, or the server cuts them off mid-sentence.
Enable flood protection. Many IRCDs kick clients that send too fast. Add a short delay between lines (0.5-1s) or implement a throttling system.
Security: Securing the AI bot properly
A bot with shell access to your hardware deserves respect:
- Take prompt injection seriously. Anyone in the channel can feed text to the bot. Don’t give the bot tools, file permissions, or shell access while it listens to a public channel. Start text-only, then add whitelisted features if needed.
- Set cost and load limits. Even a local model consumes GPU time. Rate caps per hour stop a troll from blocking your card.
- Register and protect the bot nick. Otherwise someone hijacks your bot’s identity.
- Don’t log sensitive data. Channel content can be personal; keep log retention minimal.
- IRC security basics. Hardening the underlying network and bot host are covered at IRC-Security.de; our overview is in IRC Cybersecurity.
Common pitfalls with AI IRC bots
Bot doesn’t respond. Usually PING wasn’t answered and the server dropped the connection. PING/PONG is mandatory, not optional.
Garbled characters with umlauts. IRC is old; encoding is historically messy. Send and receive UTF-8, errors="ignore" is a quick fix, but clean decoding is better.
Bot floods the channel. Always rate-limit, always cap response length, and if in doubt, reply via /notice instead of to the channel.
“AI in IRC is a toy.” Only half true. The underlying tech: streaming responses, context management, throttling, is the same as in any serious chatbot backend. If you build it cleanly here, you can use it anywhere.
Further reading on IRC bots and AI
- IRC-Coding.de - Learn programming, build bots, Eggdrops, AI coding: the specialist site for bot development.
- IRC-Mania.de - Main hub for IRC content, bot tutorials, and workshops.
- IRC-Security.de - Secure bots and servers properly.
- IRC-FAQ.de - Quick answers to bot questions.
- IRC-FAQ.com - International bot and script FAQs.
- Ollama - The local LLM backend behind the bot.
Related articles on BotServ.de: IRC Commands Reference, IRC Services explained, and the IRC Learning Path.
FAQ: AI bot in IRC - Common questions
What do I need for an AI bot in IRC?
A local LLM via Ollama, a Python script as an IRC client, and access to a network. A GPU with 8 GB VRAM is enough for smaller models; CPU operation works but is slow.
Which model works best for an IRC bot?
Compact models with fast response times: Qwen3, Llama 3.2, Mistral, or Phi. Chatbots don’t need huge models; they need speed because channel conversations happen in real time.
Is an AI bot allowed in public IRC networks?
Depends on the network. Many require registration or approval, some have dedicated bot channels. On your own IRC network, you set the rules.
How do I stop the bot from spamming the channel?
Rate-limit per user (1 request/second), trim answers to 400 characters and split into single lines, plus use a trigger prefix like !ki so not every line triggers a reply.
Can the bot answer private messages?
Yes, modify the code to respond to PRIVMSG sent to the bot’s nick instead of the channel. Handy for sensitive questions and saves channel rate.
What is prompt injection with an IRC bot?
When a channel user slips instructions to the bot (“ignore all rules”). As long as the bot only returns text, it’s annoying but harmless. It becomes dangerous only when the bot has tools or shell access.
Do I need programming skills for this?
Basic Python is enough for the boilerplate. Everything beyond that, Eggdrops, Services integration, advanced bots, you learn on IRC-Coding.de.
What is BotServ again?
The classic IRC service that manages bots in channels, and the namesake of this site. Commands are in our IRC Commands Reference.


