AI と Mermaid
英語版

Mermaid Prompt Engineering for LLMs: A Practical Guide

2026年6月22日
6 分で読めます
mermaid2img

Getting an LLM to generate a correct Mermaid diagram on the first try is harder than it looks. Ask vaguely and you get broken syntax, wrong diagram types, or conversational filler mixed with code. Ask precisely and you get production-ready diagrams in seconds.

This guide teaches the Mermaid-First prompting strategy — a structured approach to prompting ChatGPT, Claude, Gemini, and other LLMs for reliable diagram generation.

The Mermaid-First Prompting Strategy

The core principle: declare the diagram type and constraints before describing the content. LLMs perform dramatically better when the output format is locked in upfront.

Bad prompt (vague)

Draw a diagram of our authentication flow.

Good prompt (Mermaid-First)

Output ONLY a Mermaid sequence diagram. No explanation, no markdown fences outside the code block.

Requirements:

  • Diagram type: sequenceDiagram
  • Actors: User, Frontend, AuthService, Database
  • Flow: User logs in → Frontend sends credentials → AuthService validates → Database lookup → JWT returned

The good prompt eliminates ambiguity about format, actors, and flow direction.

Prompt Template Library

Copy and adapt these templates for common diagram types.

Flowchart template

Output ONLY valid Mermaid code. No explanation.

Create a flowchart TD (top-down) for: [DESCRIBE PROCESS]

Nodes to include:
- [Node 1]: [description]
- [Node 2]: [description]

Decision point: [condition] → [yes path] / [no path]

Style: use descriptive labels, max 12 nodes.

Sequence diagram template

Output ONLY a Mermaid sequenceDiagram. No explanation.

Participants: [Actor1], [Actor2], [Actor3]

Interaction flow:
1. [Actor1] → [Actor2]: [message]
2. [Actor2] → [Actor3]: [message]
3. [Actor3] → [Actor2]: [response]
4. [Actor2] → [Actor1]: [final response]

Include alt/opt blocks if error handling is needed.

Class diagram template

Output ONLY a Mermaid classDiagram. No explanation.

Classes:
- [ClassName]: [properties and methods]
- [ClassName2]: [properties and methods]

Relationships:
- [Class1] --> [Class2]: [relationship type]
- [Class2] ..> [Class3]: [dependency]

Architecture / C4 template

Output ONLY a Mermaid flowchart LR. No explanation.

System: [System Name]
Components:
- [Component]: [role]
- [Component]: [role]

External systems: [list]
Data flows: [describe connections]

Keep it high-level (C4 Level 1 context diagram style).

Advanced Techniques

1. Comment-First Protocol

For complex diagrams, ask the LLM to plan with comments before generating nodes:

First, output a Mermaid diagram with ONLY comments (%% lines) describing the structure.
Then, on the next message, I'll ask you to fill in the actual syntax.

This two-step approach reduces syntax errors in diagrams with 15+ nodes.

2. Error Recovery Loop

When Mermaid fails to render, feed the error back to the LLM:

This Mermaid code failed to render with error: "[ERROR MESSAGE]"

Fix the syntax and output ONLY the corrected Mermaid code:

[PASTE BROKEN CODE]

LLMs are excellent at self-correction when given specific parser error messages.

3. Iterative Refinement

Start minimal, then expand:

  1. Round 1: "Generate a basic 4-node flowchart for user registration"
  2. Round 2: "Add email verification step between CreateAccount and Success"
  3. Round 3: "Add error handling branch for duplicate email"

Each round produces a valid diagram you can verify in Mermaid2Img before adding complexity.

4. Negative Constraints

Always include what you do NOT want:

Do NOT:
- Add styling or classDef blocks
- Use abbreviations for node names
- Include more than 10 nodes
- Add explanatory text outside the code block

Negative constraints are as important as positive instructions for reducing LLM creative drift.

Comparing LLM Performance for Mermaid

Based on community testing and practitioner reports:

ModelMermaid accuracyBest forWeakness
ClaudeHighComplex sequence & class diagramsOccasionally over-stylizes
ChatGPT (GPT-4o)HighFlowcharts, general purposeMay add conversational wrapper
GeminiMedium-HighSimple flowcharts, ER diagramsStruggles with nested subgraphs
Open-source modelsMediumSimple 5-node diagramsComplex syntax, styling errors

For production documentation, always validate output in a Mermaid renderer regardless of which model you use.

From Prompt to Published Diagram

The complete workflow:

  1. Prompt the LLM with a Mermaid-First template
  2. Copy the generated Mermaid code
  3. Paste into Mermaid2Img Editor for live preview
  4. Fix any rendering errors (feed error back to LLM if needed)
  5. Export as PNG, SVG, or PDF
  6. Embed in your documentation, wiki, or presentation

This loop takes 2–5 minutes for most diagrams — compared to 20–30 minutes in a GUI tool.

Common Prompting Mistakes

MistakeFix
Not specifying diagram typeAlways start with flowchart TD, sequenceDiagram, etc.
Asking for "a diagram" genericallyName the exact type and direction
Allowing explanatory textAdd "Output ONLY Mermaid code, no explanation"
Too many nodes at onceBreak into sub-diagrams of ≤12 nodes
Not validating outputAlways render before committing to docs
Using screenshots in AI contextGive the LLM Mermaid source, not PNG images

Frequently Asked Questions

What is the best prompt for ChatGPT to generate Mermaid?

Start with: "Output ONLY valid Mermaid code, no explanation. Create a [diagram type] for [description]." Then specify nodes, relationships, and constraints.

How do I fix broken Mermaid from an LLM?

Paste the code into Mermaid2Img to see the error message, then feed that error back to the LLM with: "Fix this Mermaid syntax error: [error]. Output ONLY corrected code."

Can I automate Mermaid generation in CI/CD?

Yes. Use the Mermaid CLI (mmdc) for batch conversion, or call an LLM API with the templates above and pipe output through Mermaid2Img or mmdc for validation.

Conclusion

Mermaid prompt engineering is a learnable skill that pays off immediately. The Mermaid-First strategy — declare type, specify constraints, demand code-only output, validate by rendering — turns LLMs from unreliable diagram guessers into consistent diagram generators.

Keep a personal library of prompt templates for your most common diagram types, and you'll never manually draw a flowchart again.