Running a self-hosted AI for a Discord bot on a CPU-only VPS (and the thread trick that made it 70% faster)
I wanted my Discord bot, Stellar, to answer questions with AI: a /ask command, plus a first reply when someone opens a support ticket. I didn't want to pay per message or send people's questions to a third party. So the
I wanted my Discord bot, Stellar, to answer questions with AI: a /ask command, plus a first reply when someone opens a support ticket. I didn't want to pay per message or send people's questions to a third party. So the model runs on the same 6-core VPS as the bot. No GPU.
The setup
- Ollama running Qwen 2.5 3B, reachable only from the bot's Docker network, never from the internet.
- Capped at 4 of the 6 cores (systemd
CPUQuota=400%), so the bot itself stays fast. - Answers stream into Discord as they're written, editing one message every second. People see text within a couple of seconds even though a full answer takes 10–15 seconds.
The thread trick
Ollama starts one thread per core by default. With the CPU capped at 4 cores, 6 threads were fighting over 4 cores. Setting num_thread to match the cap:
options: { num_predict: 200, temperature: 0.3, num_ctx: 2048, num_thread: 4 }
took generation from 5.1 to 8.6 tokens a second, without giving it any more CPU.
Small models and other languages
A 3B model answering Polish or Spanish directly gave vague or made-up answers, and its knowledge base (the bot's command list) is in English anyway. So questions go through three steps:
- The model names the language and translates the question to English.
- It answers in English, with the right facts from the command list.
- It translates the answer back.
In tickets, the English copy stays underneath so staff can read it. Polish answers went from useless to correct.
Keeping it fair on one CPU
- One answer at a time, with a short queue.
- A 15-second cooldown per user and a daily cap per server.
- Nothing is stored: only the answer's length and how long it took are logged.
If you run a Discord server, Stellar is free: https://stellarbot.xyz. Questions about the setup are welcome in the comments.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.