Architecture as Code 🚀 for AI Governance (2026)13 min read2,541 words

Architecture as Code 🚀 for AI Governance (2026)

Learn architecture as code for AI software architecture: automate architecture governance with ArchUnit to prevent architecture drift and speed audits—start now!

architecture as codearchitecture governanceArchUnitAI software architecturearchitecture drift
Architecture as Code 🚀 for AI Governance (2026)

Architecture Intelligence Starts with Architecture as Code

AI can help teams understand a large codebase, but it won't make architecture governance reliable on its own. If your rules are vague, your documentation is scattered, and nobody can tell when the system has drifted, adding a language model often adds noise - not control.

That matters most in large or business-critical systems, where a single architectural mistake can ripple across reliability, security, or delivery schedules for multiple teams. A model that spots a likely issue can be useful. A model that quietly decides whether a change is allowed to ship is a different story.

The practical path is to make important architecture rules explicit and testable first. Tools like ArchUnit can enforce structural constraints consistently in your development pipeline. Once those checks (and the underlying architecture knowledge) are in place, AI can help interpret unstructured information, spot patterns, and propose new rules. This article walks through how to build that foundation, where AI actually helps, and how to keep decisions auditable as the system evolves.

Why should architecture rules be deterministic before AI?

Architecture governance has two jobs. First, it enforces rules a team has already agreed to - for example, "a payment service must not query a user database directly." Second, it handles context that's harder to express as a single clean rule: a design document might imply a dependency that doesn't exist in code, or modules might be coupled through shared concepts even without direct imports.

Those jobs call for different kinds of tools. Structural rules are usually best checked with deterministic tests. Contextual interpretation can benefit from AI, as long as you treat its output as a hypothesis - not a final verdict.

The difference between a rule and a signal

A rule has a defined condition and a repeatable outcome. Given the same code and the same rule, an architecture test should behave the same way. That's why it fits naturally into a pull request check or a CI pipeline: if it fails, developers can see what broke, reproduce it, and decide whether the code or the rule needs to change.

A language model works differently. It generates a plausible response based on what it's been given. The same prompt can lead to different explanations, even when the underlying facts haven't changed. A confident answer isn't proof the explanation is correct.

This isn't an abstract philosophical point. If a CI check blocks a release, you need to know exactly which constraint was violated. If an AI-generated interpretation is involved, you also need to see what evidence it relied on - and what it might be getting wrong.

Why starting with AI often disappoints

Teams that jump straight to a broad "AI architecture review" often find the tough part isn't model setup. It's that they don't have a current map of system boundaries, agreed rules, reliable architectural documentation, or a clear definition of what "drift" even means. Then the model has to infer both the system and the standard it's supposed to evaluate.

That usually leads to familiar problems:

  • Inconsistent findings: Similar changes get different recommendations.
  • Weak evidence: The model points at a likely issue without identifying a reproducible rule violation.
  • Unclear ownership: Developers can't tell who should approve an exception.
  • False confidence: A polished explanation can make an unverified guess sound like fact.

In practice, the question isn't "Can AI review our architecture?" It's "Which decisions need a repeatable answer, and which ones are better handled through contextual analysis?" Use code-based checks for the first group. Consider AI for the second - where a human can review the reasoning.

How do you build an architecture-as-code foundation?

Architecture as Code (AaC) means expressing selected architecture constraints in a form you can check automatically. It doesn't mean every design decision has to turn into code. A system still needs judgment, discussion, and documentation. AaC just makes the testable rules visible and consistent.

Start with a small set of constraints that have clear ownership and real consequences when they're violated. Common examples include allowed dependencies between layers, prohibited reverse calls, module boundaries, and cycles between packages. If nobody understands the rule - or nobody intends to enforce it - don't use it as an early test.

Choose rules that matter in production

A useful rule ties an architectural decision to a consequence. "Keep the application layered" is hard to test. "Controllers may call application services, but may not access persistence packages directly" gives you something concrete to check.

For a Java codebase, ArchUnit can express a constraint like this:

noClasses()

.that().resideInAPackage("..payment..")

.should().dependOnClassesThat()

.resideInAPackage("..user.persistence..");

The exact package patterns depend on your codebase. What matters is that the team can read the rule, understand what it's trying to protect, and immediately see what happens when a change breaks it. If the constraint doesn't fit a package-level check, you may need a different test or analysis approach.

Prioritize by risk and frequency. A dependency that crosses a security boundary should come earlier than a naming rule with little practical impact. Start with a few high-value rules, then expand once developers understand how the checks behave.

Put checks in the development flow

If a rule runs only in an occasional architecture review, it won't stop routine drift. Run the relevant checks in the workflow developers already use - like the pull request pipeline. For critical constraints, surface failures before a change is merged instead of burying them in a report that nobody feels responsible for reviewing.

Be cautious about strict enforcement when you introduce checks into a mature system. If hundreds of violations already exist, failing every build right away can become an obstacle teams learn to work around. A safer rollout is to record the baseline, prevent new violations in targeted areas, and agree on a plan for existing debt. Tighten enforcement as the backlog shrinks.

Document why each rule exists

Keep architecture decision records (ADRs), system boundaries, and technology choices in structured, maintained documentation. Markdown is often enough - what matters is that the material is easy to find, someone owns it, and it stays current. Link each important automated rule back to its intent in a nearby comment or in the documentation.

Track progress without turning metrics into targets. Useful signals include new rule violations, recurring exceptions, and changes in the number of dependency cycles. A measure like module cohesion can help, but only if the team defines how it's calculated and what it should influence. Counts alone get gamed or misunderstood, so they need context and ownership.

A practical foundation checklist:

  • Select a short list of consequential, testable constraints.
  • Assign an owner to each rule and its exceptions.
  • Run checks in CI and make failures readable.
  • Record existing violations before enforcing new constraints.
  • Maintain ADRs and system boundary documentation.
  • Track trends, not just a single number.

Where can large language models improve architecture governance?

Once deterministic checks exist, AI can widen the scope. The strongest contribution usually isn't replacing a test - it's helping people deal with information that's hard to encode directly: natural-language requirements, design documents, meeting notes, older system descriptions, and change histories.

A good pattern is: let a model propose or interpret, then verify the result with a mechanism the team controls. That keeps the model in a supporting role and makes the output easier to review.

Translate architectural intent into candidate rules

A product or architecture team might say, "The payment module must not access the user database directly." A language model can help turn that statement into a candidate ArchUnit test, or at least point out what package-level information it would need first.

Treat generated code as a draft. A developer or architecture owner should check that the rule matches the intended boundary, uses the right package locations, and doesn't block legitimate dependencies. Then add it to the test suite through the normal review process. The payoff is less friction when converting a clear decision into a test - not the removal of human review.

If the statement is ambiguous, the model should ask questions instead of guessing. For example, "payment must not access user data" could mean "no access at all," or it could mean "access only through a specific API." A test generated from the wrong interpretation can bake in an architectural disagreement as if it were settled policy.

Use AI to investigate why drift happened

A deterministic test can tell you a constraint was broken. It usually can't explain how it got broken. With appropriate access controls, an AI assistant can summarize the relevant code changes, commit message, and approved requirement, then suggest possible reasons for the deviation.

That can help a retrospective surface process issues: maybe a deadline led to review being skipped, ownership was unclear, or a new requirement exposed a missing interface. Still, the model shouldn't assign blame or present speculation as fact. Ask it to separate direct evidence from assumptions, and have the people involved confirm or correct the explanation.

Find recurring patterns across history

Teams can also use AI to review architecture snapshots, change logs, and existing violation reports for repeated themes. It might point to modules that repeatedly cross the same boundary, or it might suggest that a recurring exception is really a sign the underlying rule is outdated.

This is a way to prioritize investigation, not a substitute for measuring the code. Validate any "pattern" against source changes and team knowledge before turning it into a new policy. A model can spot correlation without knowing whether a dependency is intentional, temporary, or already planned for removal.

How do you introduce AI without making governance less reliable?

Introduce AI in stages, and define success separately for each stage. You don't need an autonomous architecture agent to prove AI is useful. A narrowly scoped assistant - one that drafts rules for review or summarizes recurring violations - can save time while keeping enforcement in the hands of established checks.

The core safety boundary is straightforward: AI may recommend; deterministic checks and accountable people decide. You can revisit that boundary later, but only when you have evidence that a specific automated action is safe in a specific context.

Stage one: establish the baseline

First, make the core AaC checks genuinely useful on their own. Define high-priority rules, run them in CI, document existing drift, and make exceptions explicit. At the same time, organize the architecture knowledge those rules depend on - ADRs, system boundaries, ownership, and approved technology choices.

This stage delivers value without any model. It also gives later AI features the context they need. If the team can't point to the current rule or explain why it exists, the AI system won't reliably reconstruct that knowledge from a prompt.

Stage two: add AI as a reviewed assistant

Pick one low-risk workflow where a human review step is clearly part of the process. Candidate uses include drafting a structural test from a written rule, summarizing the likely cause of a failed check, or identifying where to focus an architecture review based on historical patterns.

Measure results against the existing workflow. Did the assistant reduce the time it took to create a test? Were its suggestions accepted after review? How often did it miss relevant evidence or produce explanations nobody could verify? These answers tend to be more meaningful than a generic claim that AI "improved architecture quality."

Keep the model's inputs narrow and relevant. Avoid sending secrets, personal data, or proprietary material unless the organization has approved the data handling, retention, and access model. Requirements differ by organization and jurisdiction, and teams handling UK or EU personal data should consider obligations under applicable data-protection rules, including GDPR where relevant.

Stage three: consider a controlled feedback loop

A more advanced workflow can retrieve relevant ADRs and system documentation, assess an incoming requirement, run architecture checks, and assemble a risk report. An agent could coordinate those steps and open a review task, but that still doesn't mean it should approve or block a production change by itself.

Before automating anything, define its scope, fallback behavior, and a human owner. Preserve the inputs, model version, outputs, retrieved documents, and follow-up decisions so the team can investigate later. If the model can't find relevant context, it should say so rather than filling gaps with a plausible-sounding answer.

A sensible progression is: observe → recommend → review → automate narrowly. Each step should earn the next through evidence. If recommendations are often wrong, improve the context or keep the workflow manual. More autonomy isn't inherently better governance.

Conclusion

Architecture intelligence doesn't start with a model. It starts with a system the team can describe, rules it can test, and evidence it can review. AaC tools like ArchUnit provide a repeatable way to catch defined forms of drift, while maintained architecture records give people - and later AI systems - the context behind those checks.

Build that foundation first. Then bring in AI where it can handle unstructured information, draft candidate rules, or help investigate recurring patterns. Keep recommendations reviewable, log the evidence, and leave critical enforcement to deterministic checks and accountable owners. That sequence gives you useful automation without trading away stability or explainability.

Frequently Asked Questions

Architecture as Code expresses selected software architecture constraints in a machine-checkable form. Teams can run these checks automatically to detect issues such as prohibited dependencies or cycles. It complements design documentation and human review; it does not mean every architectural decision can or should be reduced to a test.
ArchUnit can test structural rules in Java code, but it is not a complete governance process. Teams still need to agree on boundaries, document intent, manage exceptions and review changes that require judgment. Use it where the code’s structure can express the rule clearly, and choose other checks for concerns it cannot reliably assess.
No. Treat generated rules as proposals. A reviewer should confirm that each rule reflects an approved decision, uses accurate code boundaries and will not block legitimate behavior. Once reviewed, it can be added to the same test and CI process as a manually written rule.
It can flag possible concerns and explain relevant context, but its output is probabilistic and may be wrong. For constraints that must be enforced consistently, use deterministic checks. If AI contributes to a review, preserve its evidence and keep an accountable person responsible for the decision.

Related articles