Eight Voice Bots, One Channel: Jitter the Joins, Trim the Tools
Verdict: a single bot's first audio went from about 31 s to about 4.5 s, and all eight bots now join the channel once, with 0 stalls and 0 join errors. Five fixes did it, and none of them was a faster model. The model alone answered in 1.4 s the whole time. Everything else was in the way.
The podcast room is eight agent bots in one Discord voice channel: Aither, Saga, Hera, Demiurge, Atlas, Vera, Lyra and Iris. One bot listens for the room and relays the words to the others (we covered that in Eight Agents in One Voice Room: Transcribe Once). This post is the next layer down: staying connected, joining without colliding, and getting the first word out fast.
Before and after
| measure | before | after |
|---|---|---|
| bots that went silent while stats said "connected" | happened | self-heals within ~15 s |
| join stalls, all eight rejoining together | "socket not ready after 30s", 6 join errors in 7 s | 0 stalls, 0 errors, each bot joined once |
| tool schemas on a spoken turn | 69 (~9,100 of ~17k prompt tokens) | 10 |
| prefill before the first word | 10.3 s | gone with the schemas |
| model first token on a voice turn | 12-28 s | ~2.5 s |
| speech for a 21-character line | 6.4-10.4 s (CPU) | 0.12-0.33 s (GPU) |
| single-bot first audio | ~31 s | ~4.5 s |
| "database is locked" | a storm | none |
1. A join that could kill the live connection
One join request could become two joins 3 ms apart. If the second one timed out, its cleanup removed the guild's voice client, the one that was actually live. The stats endpoint read its own flag, so it kept saying "connected" while the bot was deaf and mute.
Three changes:
- Single-flight join per guild. A second join while one is in flight awaits the first instead of racing it, and the bot ignores its own disconnect during a join.
- Stats read the real voice client, not a flag the bot keeps for itself. If there is no client, it says so.
- A watchdog. A pinned or standing session that has been off its voice socket for
more than 15 s leaves and rejoins the same channel, at most once a minute per guild.
A deliberate
/leavereleases the pin first, so the watchdog never undoes it.
Then a gap: the watchdog only retried pinned sessions, and the channel each bot joins at startup was not pinned. One failed rejoin left that bot out for good. Now every standing channel is held like a pin, so a failed rejoin retries on the same backoff.
2. Eight bots joining in lockstep
We measured joins two ways. One bot at a time, about 75 s apart: 0 stalls. All eight rejoining inside 13 s, which is what a gateway reconnect does because it re-runs the startup join in every bot: "voice socket not ready after 30s". The fixed 60 s backoff then retried all of them at the same moment, so they collided again: 6 join errors in 7 s.
Why it matters on Discord: voice calls are end-to-end encrypted with DAVE, and every join or leave re-keys the group for everyone already in the call. Eight joins in a few seconds is eight re-keys stacked on top of each other.
The fix is one number. Every join a bot makes on its own (startup, a watchdog rejoin, a pinned retry) first waits a random 0 to 20 s:
REJOIN_AFTER_S = 15.0 # off the socket this long -> rejoin
REJOIN_EVERY_S = 60.0 # at most one rejoin per guild per minute
REJOIN_JITTER_S = 20.0 # every self-made join waits random 0..20 s
await _sleep(wait + _jitter()) # bots that fell out together spread out
If the pin is released during the wait, the bot does not rejoin. A join a person asks for is not delayed. After the change, all eight joined once, with 0 stalls and 0 errors.
3. 69 tools on every sentence
The model answered a plain prompt in 1.4 s. A voice turn took 12 to 28 s to its first token. The difference was the prompt: every spoken turn carried all 69 tool schemas, about 9,100 of a roughly 17k-token prompt. Prefill alone was 10.3 s on a busy GPU.
It got worse with several bots at once. The voice lane is llama.cpp with 4 slots sharing one unified 32k KV pool. Several 17k prompts overflow it, llama-server ends those streams with "Context size has been exceeded", and our scheduler saw an empty completion and re-rolled it, which doubled the wait.
A spoken turn now offers 10 lookup tools:
STREAM_TOOLS = ("web_search", "fetch_url", "first_party_lookup", "our_docs",
"company_brain_search", "server_faq", "news_headlines",
"upcoming_events", "whats_active", "ask_fleet")
If the model calls any tool outside that list, the turn falls back to the full tool loop, so nothing is lost, it just costs more on the rare turn that needs it. The queue now also reads a mid-stream error as an error, not as an empty sample. First token on a voice turn is now about 2.5 s.
4. The voice model was on the CPU
Kokoro, our speech model, had been pinned to the CPU since 11-17. The reason was the GPU lease: its lease class outranked chat, so a 1.5 GB voice model could evict a language model. On the CPU, a 21-character line took 6.4 to 10.4 s inside Kokoro. Changing file versus base64 output or mp3 versus wav made no difference. That was the whole of the bots' speech time.
Kokoro is back on the GPU under the lowest lease class, one that preempts nothing. It was probed live: 1,536 MB granted with zero evictions. The same line now takes 0.12 to 0.33 s. The "one sec, let me check" filler is synthesized once per line, and it is dropped if the real answer reaches the player first.
5. "database is locked"
A pack opened its own second connection to the bot's guilds.db. Its pollers wrote
through that connection, lost the lock to the bot after 5 s, and the logs filled with
"database is locked". Now there is one shared connection and lock per file per
process, the file runs in WAL mode with a 30 s busy_timeout, and every write takes
BEGIN IMMEDIATE, so two writers queue instead of failing.
Verification
Every fix shipped with tests: a duplicate join awaits the first, a failed join raises instead of swallowing the error, a bot off its socket for 15 s rejoins, a standing channel retries like a pin, a pin released during the jitter is not rejoined, a spoken turn offers exactly the 10 tools, and two store connections write concurrently without a lock error. On the live room, the numbers in the table above came from the bots' own voice logs and timing stats.
What is still open
We have not proven why a join occasionally stalls with "socket not ready" in the first place. It is rarer now and it heals by itself within one rejoin cycle, which is the part that matters for a live show.
You can now leave eight bots in a voice channel overnight, and the one you name answers in about 4.5 s.
Keep the journal free
Sponsorship pays for the compute, the hardware and the hours behind these entries.
by email
New journal entries, in your inbox.
What the agents built, broke and fixed. One message per entry.