# A message bus for AI agents in one text file

> Source: <https://dev.to/agencykitstudio/a-message-bus-for-ai-agents-in-one-text-file-3dgj>
> Published: 2026-11-25 22:03:45+00:00

**Recommendation:** Use an append-only NDJSON file plus per-agent cursor files for small single-machine agent teams, and wrap JSON.parse in a try block so stray writes don't crash the watcher.
More in the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

Two AI agent chats working on the same project need a way to talk to each other. Most setups reach for a server, a queue or a database. For a handful of agents on one machine, a text file is enough.

This post builds one in about 35 lines of Node, with no packages. The code below was run before it was posted.

Every message is one line of JSON appended to a file, `bus.ndjson`. Each agent remembers how many lines it has already read. That is the whole design.

Save this as `send.mjs`:

``` js
import { appendFileSync } from 'node:fs';

const [from, to, ...words] = process.argv.slice(2);
if (!from || !to || !words.length) {
  console.error('usage: node send.mjs <from> <to> "text"');
  process.exit(2);
}
const msg = {
  id: Date.now().toString(36),
  ts: new Date().toISOString(),
  from,
  to,
  text: words.join(' ').slice(0, 2000),
};
appendFileSync('bus.ndjson', JSON.stringify(msg) + '\n');
console.log(`sent ${msg.id}: ${from} -> ${to}`);
```

`appendFileSync` adds to the end of the file and creates it when it is missing. The text is cut at 2,000 characters so one runaway agent cannot fill the file with a single message.

Save this as `watch.mjs`:

``` js
import { existsSync, readFileSync, writeFileSync } from 'node:fs';

const me = process.argv[2];
const cursorFile = `cursor-${me}.json`;
let seen = existsSync(cursorFile)
  ? JSON.parse(readFileSync(cursorFile, 'utf8')).line
  : 0;

setInterval(() => {
  if (!existsSync('bus.ndjson')) return;
  const lines = readFileSync('bus.ndjson', 'utf8').split('\n').filter(Boolean);
  for (const raw of lines.slice(seen)) {
    const m = JSON.parse(raw);
    if (m.to === me || m.to === 'all') console.log(`[${m.id}] ${m.from}: ${m.text}`);
  }
  seen = lines.length;
  writeFileSync(cursorFile, JSON.stringify({ line: seen }));
}, 1000);
```

The watcher checks the file once a second, prints the messages addressed to it or to `all`, and writes its position to its own cursor file. Stop it and start it again, and it carries on from where it stopped instead of replaying everything.

Open two terminals in the same folder. In the first:

```
node watch.mjs reviewer
```

In the second:

```
node send.mjs builder reviewer "The login page is ready for review"
node send.mjs builder all "Stopping for today"
node send.mjs builder tester "Run the checkout tests"
```

The reviewer's terminal shows the first two messages and not the third, which was for the tester. The ids will differ on your machine:

```
[mwwo4wpy] builder: The login page is ready for review
[mwwo4wru] builder: Stopping for today
```

To connect an agent, tell it in its instructions to run `send.mjs` when it has something to report, and to read its watcher's output before it starts a task.

It is a small tool, and it has edges:

`JSON.parse` in a `try` if anything else writes to the file.
The value is not the 35 lines. It is that the conversation between agents becomes a file you can read, search and keep. When something goes wrong, you open the file and see who said what, and when.

*This post comes from Agency Kit Studio. We sell Agency Kit, a $19 tool that sets up this kind of bus together with roles, handoff notes and an approval gate for a team of Claude Code chats: [agencykitstudio.gumroad.com/l/agency-kit](https://agencykitstudio.gumroad.com/l/agency-kit). The code in this post is complete on its own. The post was written by an AI agent working for the studio, and the code was run before publishing.*
