Back to Blog

Building Narrative Graphs with LangGraph

How we use LangGraph to model story plots as hierarchical graphs — enabling agentic, generative storytelling in BookWriter.


Stories Are Graphs

Every compelling story has structure. Characters intersect, plotlines diverge and converge, themes echo across chapters. When we started building BookWriter, we realized that the right data structure for a plot isn’t a list or a tree — it’s a graph.

Why LangGraph?

LangGraph gives us something unique: stateful, multi-agent workflows as graphs. This maps perfectly to plot development:

  • Nodes represent plot points, scenes, or narrative beats
  • Edges represent causal or thematic connections
  • Groups cluster related nodes into arcs or chapters
  • State persists across interactions — the AI remembers what it’s already written

Instead of generating a story in one shot, BookWriter uses LangGraph to develop plot incrementally. The agent can:

  1. Propose new plot points based on existing structure
  2. Identify gaps or inconsistencies in the narrative
  3. Suggest connections between seemingly unrelated threads
  4. Generate text for specific nodes while maintaining overall coherence

The Visualization Challenge

Having a graph structure is powerful, but it’s useless if authors can’t see and interact with it. That’s why we built a custom SVG-based plot visualizer in React that supports:

  • Hierarchical layout — groups nest visually
  • Zoom and pan — for exploring complex plots
  • Selection and highlighting — click a group, see all related nodes light up
  • Real-time updates — as the backend generates new nodes, the graph updates live

What We Learned

The biggest insight from building BookWriter is that generative AI works best under constraints. A blank page is the worst prompt. But give the AI a partial graph with established characters, themes, and plot points, and it can generate surprisingly coherent continuations.

Structure doesn’t limit creativity — it enables it.


BookWriter is open source. Check it out on GitHub.