Architecture Diagram Generation with AI Coding Tools: A Practical Guide
This guide explains why AI coding tools produce inconsistent architecture diagrams and how to fix it. You will learn to extract style rules from a reference image, structure requirements documents, use Plan mode for step-by-step generation, and validate output for completeness, consistency, and correctness. The approach relies on constraints upfront, a controlled workflow, and a validation loop instead of repeated retries.
1. Why Architecture Diagram Generation Feels Like a Lottery
Many teams run into the same problem after adopting AI coding tools: given the same requirements document and the same model, repeated generations vary widely in quality. Sometimes the layout is clean and the color scheme is consistent; other times the module hierarchy is messy, connectors cross each other, or key components are missing. Users end up retrying over and over, often blaming the requirements document for not being well written.
The real cause is usually different. Output stability depends on three factors:
- Input completeness: whether the requirements document defines module boundaries, responsibilities, dependencies, and deployment constraints;
- Style constraint clarity: whether the target layout, color scheme, line styles, and font sizes are explicitly described rather than left for the model to guess;
- Workflow controllability: whether generation happens in one long output, or follows a plan, executes step by step, and ends with a self-check.
Addressing these three points significantly improves consistency. This article uses “reproducing an existing system module diagram” as the experiment and presents a practical, general-purpose workflow.
2. Environment Setup: Combining a Text Model with Visual Understanding
Some text-only large models cannot recognize images, while architecture diagram generation often requires “reading the picture.” A common industry approach is to attach a vision-understanding extension service in the AI coding environment, convert the image into a structured description, and pass that description to the main model.
The configuration can be expressed with the following illustrative JSON (field names and values are generic examples; follow your tool’s extension specification in practice):
{"mcpServers": {"vision-understanding": {"command": "npx","args": ["-y", "@your-org/vision-mcp-server"],"env": {"API_KEY": "YOUR_API_KEY","MODE": "VISION"},"type": "stdio"}}}
After configuration, check the extension service list in the interactive interface and confirm that the status is connected. If it is not connected, check three things first: whether the key is valid, whether the runtime environment has a package manager available, and whether the communication method (standard input/output or a local port) matches the tool’s requirements.
Note that visual transcription only turns an image into text; it does not understand business semantics. You still need to manually confirm that the transcription covers all modules and connectors.
3. Step 1: Extract Style Rules from the Reference Image
For reproduction tasks, the key is not to make the model “draw something similar,” but to write the definition of “similar” into executable constraints. Plan mode is recommended: analyze first, then act, so the model does not directly produce an uncontrollable long output.
In an interactive environment that supports pasting images (the common shortcut is Alt + V, depending on your system), give the reference image to the model and ask it to produce a style specification that includes at least:
- Layout paradigm: top-down layering, left-right columns, or center-radial;
- Hierarchy structure: number of levels, alignment of modules on the same level, and spacing rules;
- Color rules: primary, secondary, and background colors, plus the “same level, same color” mapping;
- Graphical elements: rounded rectangles, square rectangles, swimlanes, and legend placement;
- Connector semantics: solid lines for calls, dashed lines for asynchronous or data synchronization, arrow direction and label conventions;
- Text conventions: title font size hierarchy and the maximum length of module names.
Example prompt:
Analyze this reference image only; do not generate a new diagram.Output a structured style specification covering six items: layout, hierarchy, color, shapes, connectors, and text.Give a quantifiable description for each item (such as number of levels, spacing ratio, and color usage).
Save the output as a standalone file and use it as fixed context for all subsequent generation tasks, so the style does not drift across conversations.
4. Step 2: Preparing a Structured Requirements Document
Once the style is fixed, the quality of the requirements document determines content accuracy. A document suitable for architecture diagram generation should be organized as follows:
# System Module Description## 1. System boundaries and external dependencies## 2. Layering (access layer / application layer / capability layer / data layer)## 3. Module list: name, responsibility, inputs and outputs## 4. Dependencies between modules (with sync/async annotations)## 5. Non-functional constraints (performance targets, availability requirements, deployment form)
Sections 3 and 4 are the most critical. If module responsibilities are vague, the model can only invent names; if dependencies are missing, connectors can only be guessed. This is the main source of diagrams that “look right but are logically wrong.”
5. Step 3: Plan-Mode-Driven Stepwise Generation
With the style specification and requirements document ready, ask the model to produce an execution plan first, then implement it. The plan usually has four steps:
- Parse the document and extract the module list and dependency matrix;
- Determine canvas size, hierarchy, and color mapping based on the style specification;
- Generate a diagram definition file (such as Mermaid, Graphviz, or other common text-based diagram formats);
- Render to an image and self-check: whether the module count matches, whether there are isolated nodes, and whether connectors cross.
Illustrative diagram definition:
flowchart TDA[Access Layer] --> B[Application Layer]B --> C[Capability Layer]C --> D[(Data Layer)]B -.async.-> E[Message Queue]
Text-based diagram formats offer three benefits: they can be version controlled, their diffs can be reviewed, and they can be rendered in batches by scripts, avoiding binary images whose modification history cannot be traced.
6. Step 4: Result Validation and Fixing Common Defects
After generation, do not just check whether it “looks similar”; run a structured validation:
- Completeness: whether all modules in the document appear and whether the counts match;
- Consistency: whether modules on the same level share the same color and the same font size;
- Correctness: whether dependency directions match the document and whether unannotated circular dependencies exist;
- Readability: whether there are too many crossing connectors and whether the legend is missing.
Common problems and fixes:
| Defect | Root cause | Fix |
|---|---|---|
| Module names are rewritten | Vague responsibility descriptions in the document | Add a “standard module name” field in the document |
| Colors change randomly | No color mapping provided | Define an explicit “level-to-color” table |
| Wrong connector direction | Sync/async not annotated | Add interaction types to the dependencies |
| Crowded layout | No limit on levels and count | Set a maximum number of levels and a canvas ratio |
7. General Tips for Efficiency and Cost
For cost and speed, follow a few general principles: turn the style specification and requirements template into reusable context to reduce repeated explanation; split a large diagram into an “overall diagram plus submodule diagrams” generated in batches to reduce single-output length; use scripts rather than model generation for deterministic rendering steps; and build a team-level prompt and template library so successful practices can be reused.
8. Conclusion
The stability of architecture diagram generation comes from “constraints upfront + a controlled workflow + a validation loop,” not from luck. Writing the style as a specification, structuring the requirements, breaking generation into steps, and bringing results into validation turns AI coding tools from “occasionally impressive” into “reliably usable.” In related technical practices at Baidu AI Cloud, this “templated input + stepwise execution + automated validation” approach also applies to document generation, API design, and test case writing.
