DEV Community

Cover image for One import, three chances: keeping coding agents inside your architecture
Vibhu
Vibhu

Posted on Originally published at archbeagle.com

One import, three chances: keeping coding agents inside your architecture

It's Tuesday afternoon. A coding agent is asked to add saved cards to checkout. It finds the card vault, sees a
function that does exactly what it needs, and writes one line:

from billing.internal.vault import store_card
Enter fullscreen mode Exit fullscreen mode

The tests pass. The feature works. And the team decided two years ago that only payments may touch
billing.internal, because that's where the card data lives. The decision is in ADR-0012, and the agent never saw it.

Your team probably already wrote the rule down for agents, in a CLAUDE.md or an AGENTS.md. That helps, but a
prompt file has two limits. It can't hold every rule for every component of a real system, and the longer it gets, the
less of it the agent follows. And it's read once, at the start. An hour of edits later, when the agent writes that
import, the rule is a hundred thousand tokens behind it.

An agent doesn't need a longer description of the architecture. It needs to be able to ask the architecture at the
moment it's about to change it. There are three such moments, and this is what each one looks like.

An animation in three steps. The agent plans an import from billing.internal; ArchBeagle answers not allowed, ARCH-DEP-3, read ADR-0012. The agent replans through payments.api. Before the edit, the hook checks it: allowed. On the pull request, the architecture check reports 0 findings.

Chance 1: the agent asks while it plans

ArchBeagle keeps a model of your architecture: the components, which may depend on which,
and the ADR behind each rule. It serves that model to agents over MCP, so an agent can ask before it writes anything.

Each agent gets its own token, made in Settings and kept in your environment, never in the repository:

export ARCH_MCP_TOKEN='arch_…'
Enter fullscreen mode Exit fullscreen mode

For Claude Code, a .mcp.json at the root of the repository connects it:

{
  "mcpServers": {
    "architecture": {
      "type": "http",
      "url": "https://app.archbeagle.com/mcp",
      "headers": { "Authorization": "Bearer ${ARCH_MCP_TOKEN}" }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Codex takes the same server in ~/.codex/config.toml and Cursor in ~/.cursor/mcp.json; the
agents guide has all three. Two tools do most of the work:

  • architecture_context(path): the component that owns a file, its layer, the dependencies the model declares, its ADRs and its open findings. The agent calls it before editing a file it hasn't touched.
  • check_planned_change(repo, changes): for each import the agent plans to add, whether the model allows it. If not, the rule it would break (ARCH-DEP-3, an undeclared dependency between components) and the ADRs to read.

In our example, the agent asks about checkout → billing.internal, gets "not allowed, read ADR-0012", reads it, and
goes through payments.api instead. Nothing was written, so nothing needs undoing.

The answer is a guide, not a gate. And the text these tools return (ADRs, docs, notes) was written by people and
other agents, so the agent treats it as data, never as instructions.

Chance 2: a hook checks each edit before it lands

Agents told to ask usually do. "Usually" is why there's a second chance.

A hook runs before the agent acts: a PreToolUse hook in Claude Code and Codex, preToolUse and its siblings in
Cursor. It sends ArchBeagle the repository, the paths the edit writes and, for Python, the imports it adds. It never
sends a file's contents or a prompt. It waits two seconds at most for the answer:

The hook's three answers. An edit's paths and imports go to ArchBeagle, which answers allow (the edit goes ahead), warn (it goes ahead, and the agent sees the rule) or deny (it is stopped, with the rule, the reason and who may approve it once). Every answer is recorded against the agent's token.

  • An allow is remembered for five minutes, so a busy agent isn't slowed down. A deny never is, so an approval works on the next try.
  • If ArchBeagle can't answer, the edit goes ahead, marked "not checked" in the hook's log, unless you choose to fail closed.
  • Every answer is recorded against the agent's token, and a deny is kept as evidence. Afterwards you can see what the agent tried, not only what it committed.

Chance 3: the pull request is checked against the model

The last chance is the one that has to be there, because it catches everything, including an agent with no MCP and no
hook. On every pull request, ArchBeagle compares the code with the model and reports each dependency the model doesn't
allow, each component it doesn't know, and each breaking change to an OpenAPI, AsyncAPI, JSON Schema, Protobuf or MCP
contract.

A finding keeps its identity across runs, so you can see when a break appeared, when it was fixed and whether it came
back. And when the agent reports its own work ("all tests pass, no architecture changes"), the report is labelled
self-reported and shown next to what the check found:

Pull request 412 side by side. The agent's report, labelled self-reported, says all tests pass and there were no architecture changes. ArchBeagle's check shows 214 tests passed from CI and a new dependency from checkout to billing.internal that the model doesn't allow.

Turning it on without slowing anyone down

You don't need all three chances on day one, and nothing should block until it has earned the right to.

Four steps in order: write the model down, turn on the pull request check, give agents a token, add the hook. Below, a rule's modes for agents: monitor, then warn, then block, moved one rule at a time once its warnings have been right.

  1. Write the model down. If you have one in LikeC4, Structurizr, C4-PlantUML or Backstage, ArchBeagle reads it. If not, it drafts one from the code, and a person approves it before it counts.
  2. Turn on the pull request check. On the free plan it reports and never blocks, so it's safe on every repository.
  3. Give each agent a token and the MCP config. A few lines per agent, and it's where most breaks stop happening.
  4. Add the hook last. Every rule starts in monitor or warn for agents. Move a rule to block only once its warnings have been right for a while. (Blocking beyond CI is a Pro feature.)

Keep your AGENTS.md. It's still the best place for how your team works. Just stop asking it to be the architecture.

ArchBeagle is in early access. Request an invite, or start with the agents guide.

The components, ADR and pull request in the pictures are sample data.

Top comments (0)