Skip to main content

How to Draw a LangChain Agent as a Mermaid Diagram in Python

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

A LangChain agent is a small graph: a node that calls the model, a node that runs the tools, and arrows between them. LangChain can write that graph out as a Mermaid diagram, which you can paste into a Markdown file, a pull request or any Mermaid viewer. This guide draws one in Python, before and after the agent gets a tool.

Syntax​

Syntax
print(<agent>.get_graph().draw_mermaid())
PartWhat it is
<agent>An agent made with create_agent
.get_graph()Returns the agent's graph: its nodes and the edges between them
.draw_mermaid()Writes the graph as Mermaid text, a diagram described in plain text

create_agent returns a LangGraph graph, a CompiledStateGraph, and LangGraph's guide to visualizing graphs says: You can visualize any compiled graph that exposes get_graph(). About the Mermaid text, it adds: Call draw_mermaid() on the drawable graph to get Mermaid syntax. Paste the string into any Mermaid renderer, or keep it in docs and PRs.

The Example​

An agent on Claude Opus 5.5, drawn twice: first with no tools, then with one tool, get_weather, which returns the same sunny forecast for any city. Drawing the agent never runs it, so this guide needs no API key.

Prerequisites​

  • uv. This guide uses uv 0.13.10 with Python 3.14.10.

Step 1: Create the Project​

In a terminal, in the folder where you keep your projects, create a project, move into it and add two packages:

Terminal
uv init --python 3.14 --no-package agent-diagram
cd agent-diagram
uv add langchain langchain-anthropic
Output (shortened)
Initialized project `agent-diagram` at `/Users/you/source/agent-diagram`
Using CPython 3.14.10
Creating virtual environment at: .venv
Resolved 46 packages in 9ms
Installed 44 packages in 105ms
+ langchain==1.5.2
+ langchain-anthropic==1.7.13

The shortened output keeps the two packages you asked for: langchain, which gives you create_agent, and langchain-anthropic, which connects LangChain to Claude. --no-package makes a plain project with main.py at the top.

Step 2: Draw an Agent Without Tools​

Replace everything in main.py with:

main.py
from langchain.agents import create_agent

agent = create_agent(model="anthropic:claude-opus-5-5")

print(agent.get_graph().draw_mermaid())

Run it:

Terminal
uv run main.py
Output
---
config:
flowchart:
curve: linear
---
graph TD;
__start__([<p>__start__</p>]):::first
model(model)
__end__([<p>__end__</p>]):::last
__start__ --> model;
model --> __end__;
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc

The terminal after uv run main.py: Mermaid text with a config header, then graph TD with three nodes, start, model and end, joined by two solid arrows, and three classDef lines.

The text describes a top-down graph (graph TD) with three nodes. The lines with --> are its arrows, and the classDef lines set the colours.

In dark mode, those colours make most labels hard to read: the boxes are light, and a dark theme draws the labels light too. So the drawings on this page change two classDef lines: classDef default gets color:#333, and classDef first becomes fill:#fff. That keeps every label readable in both modes, and Fixed: LangChain draw_mermaid() Diagram Labels Unreadable in Dark Mode shows how to print the text that way. Drawn by this page's Mermaid renderer, it looks like this:

A straight line: the run starts, the model answers once, and the run ends. With no tools, there's nothing to loop over.

Step 3: Add a Tool and Draw It Again​

Replace everything in main.py with:

main.py
from langchain.agents import create_agent


def get_weather(city: str) -> str:
"""Get the weather for a city."""
return f"It's always sunny in {city}!"


agent = create_agent(model="anthropic:claude-opus-5-5", tools=[get_weather])

print(agent.get_graph().draw_mermaid())

Run it again:

Terminal
uv run main.py
Output
---
config:
flowchart:
curve: linear
---
graph TD;
__start__([<p>__start__</p>]):::first
model(model)
tools(tools<hr/><small><em>__tools = get_weather</em></small>)
__end__([<p>__end__</p>]):::last
__start__ --> model;
model -.-> __end__;
model -.-> tools;
tools -.-> model;
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc

The terminal after uv run main.py with the tool: Mermaid text with four nodes, start, model, tools labelled __tools = get_weather, and end, a solid arrow from start to model and dotted arrows from model to end, model to tools and tools to model.

Drawn, with the same two classDef changes:

The tool added a node and a loop:

  • __start__ and __end__ are where every run begins and ends.
  • model is the node that calls Claude.
  • tools is the node that runs your tools. The small text under its name lists them: get_weather.
  • The dotted arrows are conditional edges, which the agent follows only when the run calls for them. LangGraph's guide draws the conditional edges of its own example graph the same way, with -.->. After model, the agent goes to tools when the model asked for a tool, or to __end__ when it answered. From tools, it goes back to model, which reads the tool's result.

That loop is what makes it an agent. LangChain's agents guide puts it in one line: An agent is a model calling tools in a loop until a given task is complete.

The Complete File​

main.py
from langchain.agents import create_agent


def get_weather(city: str) -> str:
"""Get the weather for a city."""
return f"It's always sunny in {city}!"


agent = create_agent(model="anthropic:claude-opus-5-5", tools=[get_weather])

print(agent.get_graph().draw_mermaid())

The complete main.py in a macOS-style code window: a get_weather tool, an agent on anthropic with that tool, and a print of agent.get_graph().draw_mermaid().

Draw It as a PNG Instead​

draw_mermaid() gives you text. For an image file, LangGraph's guide offers draw_mermaid_png(), and it says that by default, draw_mermaid_png() uses the Mermaid.ink API to generate the diagram. That means it sends your graph's Mermaid text to the Mermaid.ink web service. This guide sticks to the text, which you can view in any Mermaid renderer.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting draw_mermaid() to show a pictureIt returns Mermaid text, which print shows as textPaste the text into a Mermaid renderer, or into Markdown that renders Mermaid
Expecting the diagram to show what happened in a runIt shows the agent's structure, the same for every runTo see a run, print its messages
Setting an API key just to draw the agentNot needed: both drawings in this guide ran with no API key anywhereDraw first, and add the key when you run the agent

FAQ​

Does drawing the agent call Claude?​

No. create_agent builds the graph without calling the model, and draw_mermaid() only describes it. Both drawings in this guide ran with no API key.

Where can I view the Mermaid text as a diagram?​

In any Mermaid renderer. The Mermaid Live Editor draws it in the browser, and GitHub draws Mermaid diagrams in Markdown files, issues and pull requests.

Why are there HTML tags such as <p> in the text?​

They format the node labels. Mermaid uses them for the label's layout, so the drawn diagram shows __start__, not <p>__start__</p>.

Why are the labels hard to read in dark mode?​

The text colours the boxes light and leaves the label colour to the renderer's theme, and a dark theme draws light labels. Fixed: LangChain draw_mermaid() Diagram Labels Unreadable in Dark Mode passes draw_mermaid() a NodeStyles that makes every label dark grey.

Conclusion​

agent.get_graph().draw_mermaid() wrote the agent's graph as Mermaid text. Without tools, it was a straight line from start to model to end. With one tool, a tools node appeared, joined to the model by the dotted arrows of the agent loop.

Additional Resources​