Skip to content
UseAgent

Engineering · Slack · Integrations

An AI coding agent in Slack that answers in the thread

How our AI coding agent in Slack works: mention it in a thread, it works on its own computer and replies there. One ingress door, user mapping, durable replies.

Abhishek7 min read
On this page

Mention the agent in a Slack thread and it reads the whole thread, starts work on its own computer, and replies in that same thread when it's done. That's the entire user-facing feature. Building an AI coding agent in Slack that behaves like that every time, through deploys, retries and people nobody has let in yet, took most of the work.

An AI coding agent in Slack enters through the same door as the web app

The web composer, Slack mentions, schedules and skill runs all enter through one function that accepts a run command. Slack doesn't get its own run system. The Slack adapter turns an event into a run command and hands it over.

What comes for free by going through that door:

  • a durable FIFO lane per thread, one command dispatched at a time
  • idempotency, with the key derived from Slack's own stable event identity
  • cancellation, steering and boot recovery
  • memory capture when the run settles
  • a post-commit signal that makes a Slack-started turn appear in any open browser tab immediately, which we covered in one SSE stream per thread

That was a deliberate rule: a new channel, Teams or email or a webhook, should be a thin adapter into that function plus an outbox for its replies. Not a parallel queue with its own bugs.

Socket Mode and a durable inbox

We use Slack's Socket Mode. The backend opens an outbound WebSocket, so there's no public URL to expose and no signature to verify; events only arrive on sockets we opened. Each events_api payload is written to a durable inbox in Postgres first and acknowledged to Slack by its envelope id only after that commit. If the process dies mid-event, Slack redelivers, and the inbox key collapses the duplicate.

One trap worth knowing. Slack load-balances events across every open Socket Mode connection for an app. If a second process, a teammate's laptop, a staging box, holds a socket with the same app token, each one gets a subset of your events. The app token has exactly one owner.

The inbox also handles deploys. While a deployment holds run admission closed, an incoming mention waits out the whole closure instead of expiring, and the person gets told it'll start when service resumes. Inbox rows try at most 8 times. Finished rows have their text redacted after 24 hours and are deleted after 7 days.

What the agent listens to

The gating rules are small and strict:

  • a direct message is always for us
  • an app_mention is always for us, since Slack only sends it on a mention
  • a plain channel message that also mentions us is skipped, because the paired app_mention covers it and they share a dedupe key
  • a reply in a thread counts only if we already started that thread
  • anything starting with "(aside)" or "!aside" is ignored, it's talk for the humans
  • "mute" or "unmute" on its own flips whether a thread is heard

On a mention, the adapter gathers the whole thread and the parent message as context. A reply in a thread the agent already owns becomes the next turn of the same product thread, on the same engine and model.

Slack users map to members, never by email

The rule that matters most: a Slack user is linked to a UseAgent member by an explicit row, (Slack workspace id, Slack user id) to a member, and matching email addresses never create that link by themselves.

Mapping the workspace to the org isn't enough. Every run gets org memory, provider access and sandbox secrets, so an unmapped sender can't safely run even an apparently harmless prompt. The check happens before dedupe or any durable work. An unknown workspace fails closed.

When someone the agent doesn't know mentions it, it doesn't run anything. It replies:

I do not know you yet, so I have asked this workspace's admins to let you in. Once they do, mention me again and I will get to work.

The admins get an access request. If the person already has an account, letting them in creates the binding. If they type an email address instead, that becomes an invitation the owner of that address has to accept on the web, and the accept page names the Slack sender who is joining. A binding only counts while the membership is live, so removing someone from the team also stops their Slack mentions.

The invitations work took 17 adversarial review rounds and the Slack access requests took 20. Most findings were interleavings in check-then-write endpoints, like an invitation cancelled while it was being accepted. The settled design takes a per-organization lock for every change to people or invitations and checks everything inside it. Cancel is one atomic step, and it answers 409 when acceptance won.

Replies that survive crashes

The reply is written in the same database transaction that settles the run. When a run finalizes, the Slack delivery rows go into an outbox in that transaction, along with the run's status, memory capture and spend. Workers send them after commit:

  • at-least-once delivery, with idempotency keys per run so a retry never posts twice
  • full-jitter exponential backoff, honoring Retry-After on a 429
  • dead-letter when attempts run out or the error is permanent
  • ordered chains: if an earlier part of a long reply dead-letters, the rest of the chain does too, instead of posting a reply with a hole in the middle

Files the agent produced go back to the thread through Slack's upload sequence, as their own outbox kind. That used to depend on the model remembering to call a publish tool, and sometimes it didn't. Now the control plane harvests new files from the workspace after every turn.

What the thread looks like matters as much. One card per thread: a native task_card with a spinner, then a tick or an error, and an "Open in UseAgent" button. Each turn streams only its answer, as its own message. Slack's newer blocks fold their details without an interactivity endpoint, and a streaming message can't be updated while the stream is open (Slack answers streaming_state_conflict), so card updates wait for the stream to close and are paced to Slack's limit of one update every 3 seconds.

Markdown gets converted to Slack's mrkdwn. A link to a path in the sandbox workspace means nothing in Slack, so only its label survives.

The four-minute outage a stale binding caused

On October 12 at 17:58 UTC we connected a new Slack workspace in Settings. Connecting it revoked the old connection and deleted the old workspace's row. One thing didn't move: an env variable on the host still pinned a user binding to the old workspace.

At boot, the backend syncs env bindings into the database as a top-level await. The upsert hit the foreign key from Slack users to Slack workspaces, and the uncaught exception killed the process. Our 18:04 deploy failed on the new color. The deployer's compensation restarted the old color, which booted with the same env and crashed the same way. Production was down from 18:05 to 18:09.

The fix in code was narrow on purpose. An integrity refusal (SQLSTATE class 23) on an env binding is logged and skipped. Any other database error still fails boot, because a database that's down should stop a deploy.

Two lessons stuck. Env-pinned bindings go stale the moment someone changes a connection in the UI, so the UI should be the only source. And a failed promote leaves run admission closed: runs queue quietly behind it until someone reopens admission, which is exactly the kind of silent state the inbox's "starts when service resumes" message exists to explain.

What's still rough

A Slack thread is pinned to the engine it started on. Engines keep per-thread session state in the sandbox, and we watched a Codex reply on a thread born on OpenCode fail its runtime readiness. So a reply that asks for another engine gets told to start a new thread instead. An admin connects the Slack app once per workspace and every member uses that one app; the Slack integration page lists what's live.

Mention it in a thread: sign up, connect Slack in Settings, and ask it to fix the flaky test everyone's been ignoring.

Frequently asked questions

Can I use an AI coding agent from Slack without a public webhook URL?

Yes. We use Slack's Socket Mode: the backend opens an outbound WebSocket, events arrive only on sockets we opened, and each event is written to a durable inbox before it is acknowledged.

Does the agent respond to every message in a Slack channel?

No. It answers direct messages, mentions, and replies in threads it already started. Plain channel chatter is ignored, a message starting with (aside) is left for the humans, and "mute" silences a thread until "unmute".

What happens if someone not on the team mentions the agent?

It doesn't run anything for them. It tells them it doesn't know them yet and asks the workspace's admins to let them in. Once an admin does, they mention it again.

All posts →

Hand your next task
to a coding agent.

UseAgent runs Claude Code, Codex and OpenCode in cloud sandboxes. The core is open source, so you can read the code or host it yourself.