Building an Agent That Takes Orders from Notion
Been working on a friend's website, and as always, we wanted a simple loop: put a task on a board, have an AI agent pick it up, do the work, and report back. Just a poller, a dispatcher, and Claude.
The Setup
The whole thing is a Node.js process that polls a Notion database every 30 seconds. When it finds a card with status "Assigned to Agent", it picks it up and hands it to a coding agent, which is really just a wrapper around claude --print pointed at a local directory.
The entire system is about 200 lines across a handful of files.
How It Works
Config loads environment variables: a Notion token, a database ID, and a path to the local repo. The poller queries Notion for assigned tasks and keeps a Set of in-flight page IDs so it doesn't double-dispatch.
The dispatcher routes tasks to agents (we only have one: coding) with a concurrency limit via p-limit. The coding agent builds a prompt from the card's name and description, spawns claude --print --dangerously-skip-permissions, waits up to 10 minutes, and captures stdout.
When Claude finishes, the Notion card moves to "Done" with the output, or "Failed" with the error. That's the entire lifecycle.
Why This Shape
We considered a few alternatives early on:
- Webhooks from Notion. Notion doesn't support them natively, and third-party bridges add a dependency we didn't want. Polling every 30 seconds is simple and good enough.
- A queue (SQS, Redis). Overkill when the source of truth is already a database we're querying. The poller is the consumer.
- Agent frameworks (LangChain, CrewAI). We don't need tool orchestration or memory or multi-step planning at the orchestrator level. Claude Code already handles all of that internally. We just need to point it at a directory and give it a task.
The --print flag is key. It runs Claude in a single-shot mode: no interactive session, no TUI. It reads the prompt, does its work (reading files, writing files, running commands), and prints a summary. From the orchestrator's perspective, it's a subprocess with stdin/stdout.
What We Learned
Concurrency matters immediately. Even with two slots, you have to track in-flight tasks or the poller will keep dispatching the same card every 30 seconds. The inFlight Set was one of the first things we added.
Timeouts are non-negotiable. Claude can get stuck in loops. A 10-minute hard kill via SIGTERM keeps the system from hanging indefinitely.
Structured logging from day one. JSON logs with task names and page IDs made debugging straightforward even before we had any monitoring.
The card is the contract. Every field the agent needs (name, description, target directory, agent type) lives on the Notion card. No separate config files, no task definitions in code. Change the card, change the behavior.
What's Missing
The obvious gap is that this only works locally. Claude runs against whatever's on disk, and there's no version control in the loop. No branch, no PR, no review step. The agent makes changes directly to your working tree, which is fine for experimentation but not something you'd point at production code.
The next step is clear: clone a fresh copy, branch, commit, push, open a PR. The polling and dispatch loop doesn't need to change at all. That's the nice thing about keeping the orchestrator thin: the agent is the only part that needs to get smarter.