Mermaid Diagrams
Generate diagrams in markdown that render in GitHub, GitLab, VS Code, Obsidian, Notion. Syntax verified against Mermaid v11.16 (2026).
Quick Start
```mermaid
flowchart LR
A[Start] --> B{Decision}
B -->|Yes| C[Action]
B -->|No| D[Finish]
```
Quick Decision Tree
What to visualize?
├─ Process, algorithm, decision flow → flowchart
├─ API calls, service interactions → sequenceDiagram
├─ Database tables, relationships → erDiagram
├─ OOP, type hierarchy, domain model → classDiagram
├─ State machine, lifecycle → stateDiagram-v2
├─ System architecture, services → flowchart + subgraphs (or C4Context / architecture-beta)
├─ Project timeline, sprints → gantt
├─ Chronological events, milestones → timeline
├─ User experience, pain points → journey
├─ Git branches → gitGraph
├─ Brainstorming, concept hierarchy → mindmap
├─ Data distribution → pie
├─ Data trends (bar/line) → xychart
├─ Flow allocation (funnel, budget) → sankey
├─ Priority matrix → quadrantChart
├─ Task board → kanban
└─ Network packet layout → packet
Default to flowchart when unsure — it handles most "draw the system/process" requests. Prefer plain flowchart + subgraphs over architecture-beta/C4 unless the user asks for those specifically, since flowcharts render everywhere.
Diagram Types
| Type |
Declaration |
Best For |
Status |
| Flowchart |
flowchart LR / flowchart TB |
Processes, decisions, data flow |
Stable |
| Sequence |
sequenceDiagram |
API flows, service calls |
Stable |
| ER |
erDiagram |
Database schemas |
Stable |
| Class |
classDiagram |
Types, domain models |
Stable |
| State |
stateDiagram-v2 |
State machines |
Stable |
| Gantt |
gantt |
Project timelines |
Stable |
| Timeline |
timeline |
Chronological events |
Stable |
| Journey |
journey |
User experience mapping |
Stable |
| Mindmap |
mindmap |
Brainstorming, hierarchies |
Stable |
| Git |
gitGraph |
Branch visualization |
Stable |
| Pie |
pie |
Data distribution |
Stable |
| Quadrant |
quadrantChart |
Priority matrices |
Stable |
| XY Chart |
xychart |
Bar/line data trends |
Stable (was xychart-beta) |
| Packet |
packet |
Network protocol layouts |
Stable (was packet-beta) |
| Kanban |
kanban |
Task boards |
Stable |
| Block |
block |
Grid-positioned layouts |
Stable (was block-beta) |
| Sankey |
sankey |
Flow allocation |
Experimental (was sankey-beta) |
| C4 |
C4Context etc. |
System architecture |
Experimental |
| Architecture |
architecture-beta |
Cloud/service topology |
Beta |
| Treemap |
treemap-beta |
Hierarchical proportions |
Beta |
| Requirement |
requirementDiagram |
Requirements traceability |
Stable |
The old -beta declarations still parse in Mermaid v11 as legacy aliases. Prefer the stable keyword — but on platforms that bundle an older Mermaid (GitHub lags releases), the -beta form may be the only one that renders. When targeting a specific platform, verify with a small test diagram first.
Common Patterns
System Architecture
flowchart LR
subgraph Client
Browser & Mobile
end
subgraph Services
API --> Auth & Core
end
subgraph Data
DB[(PostgreSQL)]
end
Client --> API
Core --> DB
API Request Flow
sequenceDiagram
autonumber
Client->>+API: POST /orders
API->>Auth: Validate
Auth-->>API: OK
API->>+DB: Insert
DB-->>-API: ID
API-->>-Client: 201 Created
Database Schema
erDiagram
USER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : contains
USER {
uuid id PK
string email UK
}
ORDER {
uuid id PK
uuid user_id FK
}
State Machine
stateDiagram-v2
[*] --> Draft
Draft --> Submitted : submit()
Submitted --> Approved : approve()
Submitted --> Rejected : reject()
Approved --> [*]
Syntax Quick Reference
Flowchart Nodes
[Rectangle] (Rounded) {Diamond} [(Database)] [[Subroutine]]
((Circle)) >Asymmetric] {{Hexagon}}
Flowchart Edges
A --> B # Arrow
A --- B # Line
A -.-> B # Dotted arrow
A ==> B # Thick arrow
A -->|text| B # Labeled
Sequence Arrows
->> # Solid arrow (sync request)
-->> # Dotted arrow (response)
-x # Solid arrow with X end (failed)
-) # Open arrow (async, fire-and-forget)
ER Cardinality
||--|| # One to one
||--o{ # One to many
}o--o{ # Many to many
Gotchas That Break Rendering
These are the errors LLMs most often produce. Each one fails to parse or silently renders wrong:
end is a reserved word in flowcharts. A node named end (lowercase) breaks the parser because it terminates subgraphs. Use End, e[end], or quote it. Same caution applies to nodes named o or x directly after an edge: A---oB parses as a circle-ended edge to B, not an edge to node oB — add a space or capitalize.
Node IDs must not collide with subgraph IDs. subgraph Build containing a node with ID Build throws "would create a cycle". Give the node a different ID and put the display text in brackets: Compile[Build].
Special characters need quotes. Labels containing (, ), [, ], {, }, :, ;, or starting with a number often break parsing. Wrap the label in double quotes: A["Fetch (retry x3)"]. Inside quoted labels, escape with HTML entity codes: #quot; for ", #35; for #, #lt;/#gt; for </>.
Comments use %% on their own line. %% like this. Do not use // or #, and do not append %% comments to the end of a syntax line — inline trailing comments can break some diagram types.
One diagram per code block, declaration first. The first non-comment line must be the diagram type (flowchart LR, sequenceDiagram, ...). A bare %%{init: ...}%% directive with no diagram after it fails to render.
ER attribute blocks are line-based. One attribute per line inside ENTITY { } — semicolon-separated attributes on one line fail. Multiple key constraints are comma-separated: uuid user_id FK, UK.
Sequence participant names with spaces need aliases. Use participant A as API Gateway, then reference A in messages.
Values with hyphens in requirementDiagram must be quoted. id: REQ-001 fails; id: "REQ-001" works.
Best Practices
- Choose the right type — Use the decision tree above
- Keep focused — One concept per diagram; split diagrams over ~20 nodes
- Use meaningful labels — Not just A, B, C
- Direction matters —
LR for flows, TB for hierarchies
- Group with subgraphs — Organize related nodes
- Validate non-trivial diagrams — Paste into https://mermaid.live or run
npx -y @mermaid-js/mermaid-cli -i diagram.mmd -o out.svg
Reference Documentation
Read the matching reference before generating anything beyond a basic diagram of that type:
| Read |
Before generating |
| references/FLOWCHARTS.md |
Flowcharts with shapes, subgraphs, styling, ELK layout, animated edges |
| references/SEQUENCE.md |
Sequence diagrams with activation, alt/opt/loop/par blocks, notes, boxes |
| references/CLASS-ER.md |
Class diagrams (generics, annotations, namespaces) or ER schemas |
| references/STATE-JOURNEY.md |
State machines (composite, fork/join, choice) or user journeys |
| references/DATA-CHARTS.md |
Gantt, pie, timeline, quadrant, xychart, sankey, treemap, mindmap, gitGraph |
| references/ARCHITECTURE.md |
architecture-beta, block, C4, kanban, packet, requirement diagrams |
| references/ADVANCED.md |
Themes, init directives/frontmatter config, styling, security, troubleshooting |
| references/CHEATSHEET.md |
Quick syntax lookup across all types; platform support notes |
Resources
1---2name: mermaid-diagrams3description: Proactively suggest diagrams when explaining complex systems. Triggers on diagrams, charts, visualizations, flowcharts, sequence diagrams, architecture diagrams, ER diagrams, state machines, Gantt charts, mindmaps, C4, class diagrams, git graphs, kanban boards, sankey, timelines, quadrant charts, XY charts, packet diagrams. Use when user asks for visual representations of code, systems, processes, data structures, database schemas, workflows, or API flows. Generate Mermaid diagrams in markdown.4---5
6# Mermaid Diagrams
7
8Generate diagrams in markdown that render in GitHub, GitLab, VS Code, Obsidian, Notion. Syntax verified against Mermaid v11.16 (2026).
9
10## Quick Start
11
12````markdown
13```mermaid
14flowchart LR
15 A[Start] --> B{Decision}
16 B -->|Yes| C[Action]
17 B -->|No| D[Finish]
18```
19````
20
21## Quick Decision Tree
22
23```
24What to visualize?
25├─ Process, algorithm, decision flow → flowchart
26├─ API calls, service interactions → sequenceDiagram
27├─ Database tables, relationships → erDiagram
28├─ OOP, type hierarchy, domain model → classDiagram
29├─ State machine, lifecycle → stateDiagram-v2
30├─ System architecture, services → flowchart + subgraphs (or C4Context / architecture-beta)
31├─ Project timeline, sprints → gantt
32├─ Chronological events, milestones → timeline
33├─ User experience, pain points → journey
34├─ Git branches → gitGraph
35├─ Brainstorming, concept hierarchy → mindmap
36├─ Data distribution → pie
37├─ Data trends (bar/line) → xychart
38├─ Flow allocation (funnel, budget) → sankey
39├─ Priority matrix → quadrantChart
40├─ Task board → kanban
41└─ Network packet layout → packet
42```
43
44Default to `flowchart` when unsure — it handles most "draw the system/process" requests. Prefer plain flowchart + subgraphs over `architecture-beta`/C4 unless the user asks for those specifically, since flowcharts render everywhere.
45
46## Diagram Types
47
48| Type | Declaration | Best For | Status |
49|------|-------------|----------|--------|
50| Flowchart | `flowchart LR` / `flowchart TB` | Processes, decisions, data flow | Stable |
51| Sequence | `sequenceDiagram` | API flows, service calls | Stable |
52| ER | `erDiagram` | Database schemas | Stable |
53| Class | `classDiagram` | Types, domain models | Stable |
54| State | `stateDiagram-v2` | State machines | Stable |
55| Gantt | `gantt` | Project timelines | Stable |
56| Timeline | `timeline` | Chronological events | Stable |
57| Journey | `journey` | User experience mapping | Stable |
58| Mindmap | `mindmap` | Brainstorming, hierarchies | Stable |
59| Git | `gitGraph` | Branch visualization | Stable |
60| Pie | `pie` | Data distribution | Stable |
61| Quadrant | `quadrantChart` | Priority matrices | Stable |
62| XY Chart | `xychart` | Bar/line data trends | Stable (was `xychart-beta`) |
63| Packet | `packet` | Network protocol layouts | Stable (was `packet-beta`) |
64| Kanban | `kanban` | Task boards | Stable |
65| Block | `block` | Grid-positioned layouts | Stable (was `block-beta`) |
66| Sankey | `sankey` | Flow allocation | Experimental (was `sankey-beta`) |
67| C4 | `C4Context` etc. | System architecture | Experimental |
68| Architecture | `architecture-beta` | Cloud/service topology | Beta |
69| Treemap | `treemap-beta` | Hierarchical proportions | Beta |
70| Requirement | `requirementDiagram` | Requirements traceability | Stable |
71
72The old `-beta` declarations still parse in Mermaid v11 as legacy aliases. Prefer the stable keyword — but on platforms that bundle an older Mermaid (GitHub lags releases), the `-beta` form may be the only one that renders. When targeting a specific platform, verify with a small test diagram first.
73
74## Common Patterns
75
76### System Architecture
77
78```mermaid
79flowchart LR
80 subgraph Client
81 Browser & Mobile
82 end
83 subgraph Services
84 API --> Auth & Core
85 end
86 subgraph Data
87 DB[(PostgreSQL)]
88 end
89 Client --> API
90 Core --> DB
91```
92
93### API Request Flow
94
95```mermaid
96sequenceDiagram
97 autonumber
98 Client->>+API: POST /orders
99 API->>Auth: Validate
100 Auth-->>API: OK
101 API->>+DB: Insert
102 DB-->>-API: ID
103 API-->>-Client: 201 Created
104```
105
106### Database Schema
107
108```mermaid
109erDiagram
110 USER ||--o{ ORDER : places
111 ORDER ||--|{ LINE_ITEM : contains
112 USER {
113 uuid id PK
114 string email UK
115 }
116 ORDER {
117 uuid id PK
118 uuid user_id FK
119 }
120```
121
122### State Machine
123
124```mermaid
125stateDiagram-v2
126 [*] --> Draft
127 Draft --> Submitted : submit()
128 Submitted --> Approved : approve()
129 Submitted --> Rejected : reject()
130 Approved --> [*]
131```
132
133## Syntax Quick Reference
134
135### Flowchart Nodes
136
137```
138[Rectangle] (Rounded) {Diamond} [(Database)] [[Subroutine]]
139((Circle)) >Asymmetric] {{Hexagon}}
140```
141
142### Flowchart Edges
143
144```
145A --> B # Arrow
146A --- B # Line
147A -.-> B # Dotted arrow
148A ==> B # Thick arrow
149A -->|text| B # Labeled
150```
151
152### Sequence Arrows
153
154```
155->> # Solid arrow (sync request)
156-->> # Dotted arrow (response)
157-x # Solid arrow with X end (failed)
158-) # Open arrow (async, fire-and-forget)
159```
160
161### ER Cardinality
162
163```
164||--|| # One to one
165||--o{ # One to many
166}o--o{ # Many to many
167```
168
169## Gotchas That Break Rendering
170
171These are the errors LLMs most often produce. Each one fails to parse or silently renders wrong:
172
1731. **`end` is a reserved word in flowcharts.** A node named `end` (lowercase) breaks the parser because it terminates subgraphs. Use `End`, `e[end]`, or quote it. Same caution applies to nodes named `o` or `x` directly after an edge: `A---oB` parses as a circle-ended edge to `B`, not an edge to node `oB` — add a space or capitalize.
174
1752. **Node IDs must not collide with subgraph IDs.** `subgraph Build` containing a node with ID `Build` throws "would create a cycle". Give the node a different ID and put the display text in brackets: `Compile[Build]`.
176
1773. **Special characters need quotes.** Labels containing `(`, `)`, `[`, `]`, `{`, `}`, `:`, `;`, or starting with a number often break parsing. Wrap the label in double quotes: `A["Fetch (retry x3)"]`. Inside quoted labels, escape with HTML entity codes: `#quot;` for `"`, `#35;` for `#`, `#lt;`/`#gt;` for `<`/`>`.
178
1794. **Comments use `%%` on their own line.** `%% like this`. Do not use `//` or `#`, and do not append `%%` comments to the end of a syntax line — inline trailing comments can break some diagram types.
180
1815. **One diagram per code block, declaration first.** The first non-comment line must be the diagram type (`flowchart LR`, `sequenceDiagram`, ...). A bare `%%{init: ...}%%` directive with no diagram after it fails to render.
182
1836. **ER attribute blocks are line-based.** One attribute per line inside `ENTITY { }` — semicolon-separated attributes on one line fail. Multiple key constraints are comma-separated: `uuid user_id FK, UK`.
184
1857. **Sequence participant names with spaces need aliases.** Use `participant A as API Gateway`, then reference `A` in messages.
186
1878. **Values with hyphens in requirementDiagram must be quoted.** `id: REQ-001` fails; `id: "REQ-001"` works.
188
189## Best Practices
190
1911. **Choose the right type** — Use the decision tree above
1922. **Keep focused** — One concept per diagram; split diagrams over ~20 nodes
1933. **Use meaningful labels** — Not just A, B, C
1944. **Direction matters** — `LR` for flows, `TB` for hierarchies
1955. **Group with subgraphs** — Organize related nodes
1966. **Validate non-trivial diagrams** — Paste into https://mermaid.live or run `npx -y @mermaid-js/mermaid-cli -i diagram.mmd -o out.svg`
197
198## Reference Documentation
199
200Read the matching reference before generating anything beyond a basic diagram of that type:
201
202| Read | Before generating |
203|------|-------------------|
204| [references/FLOWCHARTS.md](references/FLOWCHARTS.md) | Flowcharts with shapes, subgraphs, styling, ELK layout, animated edges |
205| [references/SEQUENCE.md](references/SEQUENCE.md) | Sequence diagrams with activation, alt/opt/loop/par blocks, notes, boxes |
206| [references/CLASS-ER.md](references/CLASS-ER.md) | Class diagrams (generics, annotations, namespaces) or ER schemas |
207| [references/STATE-JOURNEY.md](references/STATE-JOURNEY.md) | State machines (composite, fork/join, choice) or user journeys |
208| [references/DATA-CHARTS.md](references/DATA-CHARTS.md) | Gantt, pie, timeline, quadrant, xychart, sankey, treemap, mindmap, gitGraph |
209| [references/ARCHITECTURE.md](references/ARCHITECTURE.md) | architecture-beta, block, C4, kanban, packet, requirement diagrams |
210| [references/ADVANCED.md](references/ADVANCED.md) | Themes, init directives/frontmatter config, styling, security, troubleshooting |
211| [references/CHEATSHEET.md](references/CHEATSHEET.md) | Quick syntax lookup across all types; platform support notes |
212
213## Resources
214
215- **Official Documentation**: https://mermaid.js.org
216- **Live Editor**: https://mermaid.live
217- **GitHub Repository**: https://github.com/mermaid-js/mermaid
218- **GitHub Markdown Support**: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-diagrams
219- **GitLab Markdown Support**: https://docs.gitlab.com/ee/user/markdown.html#diagrams-and-flowcharts