BXUP Track progress & test yourself — free
Software Engineering · Playbook · Foundation · ~4 min read
SOFTWARE ENGINEERING · PLAYBOOK · FOUNDATION

Architecture Diagrams From Code: Make the Diagrams Stop Lying

Every team has the diagram: drawn for a design review two years ago, wrong about three services, still pasted into every new deck. Hand-maintained diagrams drift the day after they are drawn.

Tier: Foundation · Workflow area: Documentation

The fix is to make code the source: an AI agent reads the actual repo — imports, service calls, queue topics, infra definitions — and emits Mermaid or C4 text that lives in the repo, gets reviewed in PRs, and gets regenerated when reality changes. Diagrams-as-code plus AI generation turns "update the diagram" from an act of archaeology into a diff.

When to use

  • Onboarding, design reviews, or client audits keep reusing a diagram you know is wrong.
  • After a significant refactor/migration, to re-establish ground truth.
  • You need C4 container/component views of a system nobody fully remembers.
  • Pairs with pb-34 (codebase tours) — the tour's system map should be generated this way.

Skip aspirational "target architecture" diagrams — those are design artifacts, drawn from intent, and should differ from code. Never let the AI generate those from the repo; it can only draw what is.

Prerequisites

  • A repo-aware agent (Claude Code, Copilot agent mode, Cursor) with access to the code and infra definitions (docker-compose, Terraform, k8s manifests, CI configs) — service boundaries live there, not in imports.
  • Mermaid rendering wherever your docs live (GitHub/GitLab render it natively) or Structurizr DSL if your org is C4-formal.
  • A decision about altitude: one diagram per level (context, container, component), never one diagram trying to be all three.

The workflow

  1. Generate the container view from deployment truth. Start at the level infra files define:

    Read docker-compose.yml, terraform/, and .github/workflows/. Produce a
    Mermaid C4-style container diagram:
    - One node per deployable/datastore/queue actually defined (cite the file
      defining each).
    - Edges only for communication you can evidence in code or config: HTTP
      client base-URLs, queue topic names, DB connection strings. Cite the
      evidence file per edge.
    - Mark any suspected-but-unevidenced edge with a dashed line and "?".
    Output as a mermaid block plus the citation list.
    
  2. Generate component views only for the areas that need them. Per subsystem: From services/billing/ imports and package structure, produce a component diagram of its internal modules and their dependency direction. Collapse anything under 100 lines into its parent. Flag dependency-direction violations (lower layers importing upper). The violations flag routinely surfaces real architecture erosion — treat those as findings, not diagram noise.

  3. Audit the edges — that is where diagrams lie. Nodes are easy; edges are claims. For each edge, check the cited evidence: is that HTTP call still live or behind a dead feature flag? Is the queue topic still consumed? Delete or dash anything unproven. Ten minutes of edge audit is the difference between a diagram and a rumor.

  4. Commit the text, render on demand. Diagrams live as .mmd/Structurizr DSL in docs/architecture/, reviewed via PR like any code. The PR review question is precise: "does this edge exist?" — answerable, unlike "does this picture feel right?"

  5. Wire regeneration to drift. Two options by maturity: (a) lightweight — a CI job flags when files cited as diagram evidence change (git diff --name-only against the citation list) and opens an issue to regenerate; (b) full — a scheduled agent regenerates the diagram and opens a PR with the diff, which a human reviews. The rendered diff of a Mermaid change ("edge added: orders → notifications") is itself a nice architecture-change alert.

Note

Deterministic extractors (pydeps, madge, Structurizr automatic extraction, Terraform graph) beat an LLM for raw dependency edges — they cannot hallucinate. The AI's value is on top: choosing altitude, collapsing noise, naming clusters meaningfully, and drafting the Mermaid. Feed extractor output into the prompt when the tools exist for your stack.

Tool variants

  • Claude Code: default for the full loop — reads infra + code, emits Mermaid, and can run the deterministic extractors and merge their output.
  • Copilot agent mode: same flow inside the GitHub-sanctioned envelope; Mermaid renders directly in the PR.
  • Structurizr (DSL, free for workspaces-as-code): when the org wants formal C4 with multiple views from one model; have the AI emit DSL instead of Mermaid.

Verification

  • Every node maps to a real deployable/datastore; every solid edge has a cited, currently-live evidence file.
  • Someone who knows the system reviews the diagram and finds nothing to add or remove at that altitude (additions mean missed coverage; removals mean hallucinated edges).
  • The Mermaid/DSL renders correctly where your team will actually view it.
  • A deliberately introduced fake edge in a test PR gets caught by review — proving the review is real.

Pitfalls

  • Hallucinated edges. Models add "obvious" connections (service A surely calls service B). The evidence-citation rule plus edge audit exists precisely for this — no citation, no solid line.
  • One mega-diagram. Forty nodes at mixed altitude is unreadable and unreviewable. Enforce one level per diagram; link between levels.
  • Drawing dead code. Feature-flagged-off paths and deprecated consumers show up in imports. The edge audit must ask "live in production?" not "present in code?"
  • Regeneration without review. Auto-merged diagram PRs reintroduce the original problem with extra steps — unreviewed claims about architecture.

Restricted-environment variant

Infra files are the most sensitive inputs here — Terraform and compose files contain account IDs, internal hostnames, sometimes secrets that escaped the vault. Scrub or use the sanctioned in-tenancy agent. Strong offline path: deterministic extractors (madge, pydeps, terraform graph) are all local, and a local Ollama model converts their output into clean Mermaid competently — this workflow degrades gracefully because the hard evidence comes from tools, not the model. Client-facing diagrams get one extra pass: strip internal hostnames and account identifiers before any deck leaves the building.

Success criteria

  • Current container + key component diagrams live in-repo as text, with every solid edge evidence-cited.
  • The two-year-old lying diagram is deleted or stamped "historical — superseded by docs/architecture/".
  • One dependency-direction violation or dead edge surfaced by generation became a fix or a filed issue.
  • Next design review opens with a diagram nobody has to caveat verbally.
This is the free reading view. Open BXUP to track your progress, run the hands-on labs, and prove it with original, exam-pattern assessments.
© Anvi Innovate · BXUP — Be Exponentially Upskilled · BXUP is an independent learning platform, not affiliated with or endorsed by AWS, Microsoft, Google, NVIDIA, IBM, Salesforce, Databricks, Oracle or Anthropic. All trademarks belong to their respective owners. · bxup.in