Modern AI systems are evolving beyond simple prompt-response interactions. Today’s AI agents can perform structured, multistep reasoning, decision-making and coordinate complex tasks autonomously. This emerging capability is known as an agentic workflow—a powerful shift in machine learning where agents operate through a series of logical steps to solve problems more effectively.
In this tutorial, we’ll explore how to build such AI agentic workflows by using two key tools: LangGraph, a framework for constructing graph-based reasoning paths and IBM® Granite® models, a robust model that complements this structure. Each step in the workflow—called a "node"—is handled by an agent, typically powered by large language models. These agents move between states based on model outputs or conditional logic, forming a dynamic, decision-driven graph.
To bring these agentic workflows to life, we’ll take a closer look at two essential components: LangGraph and the Granite model.
LangGraph is a powerful framework designed to streamline the development of AI-driven workflows by representing AI models as stateful agents within a computational graph. It enables developers to build scalable, modular systems where each behavior or decision point is defined as a node in the graph.
With LangGraph, you can:
Multi-agent systems and frameworks like LangGraph, when applied to generative AI (gen AI) tasks, typically structure task execution as either sequential or conditional workflows. Whether you are working with LangChain, IBM Granite models, OpenAI's GPT models or other artificial intelligence tools, LangGraph helps optimize your workflow for better scalability and performance.
LangGraph introduces a modern approach to orchestrating AI technologies by breaking down complex workflows into modular, intelligent components. Unlike traditional automation or Robotic process Automation (RPA), LangGraph enables dynamic, context-aware task execution by using real-time logic and memory. Here are the four key components that power this framework:
Together, these components allow LangGraph to transform how organizations design and execute AI-driven workflows—bridging the gap between AI tools and real-world business processes.
Granite-4.0-Tiny-Preview, developed by IBM Research®, is a lightweight yet capable open source language model designed to solve complex problems and practical natural language processing (NLP) tasks. While it’s smaller than commercial models like GPT-4, Granite is fast, efficient and fully compatible with Hugging Face—making it a great choice for developers seeking operational efficiency without sacrificing performance.
Granite excels in:
In this tutorial, the Granite model plays a key role in various stages of the agentic workflow, supporting both problem solving and content generation. Its lightweight design makes it suitable for real-world applications where human intervention might be limited, and where scalable design patterns are essential for building robust AI solutions across diverse datasets and providers.
In this tutorial, we will build an agentic workflow that acts as a creative assistant for writing short animated screenplays.
Given a story idea from the user, the agent will:
This use case is designed to showcase both the reasoning and generative capabilities of a language model, structured through LangGraph’s compositional workflow.
Each of the following steps is implemented as a LangGraph node:
These nodes are connected sequentially into a LangGraph, and the model moves through them while carrying forward a mutable state dictionary.
This workflow strikes a balance between creative generation and structural planning. It demonstrates:
It also scales well, you can easily extend it by adding revision steps, multiple scene generators or even character-based branching.
You need an IBM Cloud® account to create a watsonx.ai® project.
While you can choose from several tools, this tutorial walks you through how to set up an IBM account to use a Jupyter Notebook.
This step opens a notebook environment where you can copy the code from this tutorial. Alternatively, you can download this notebook to your local system and upload it to your watsonx.ai project as an asset. To view more Granite tutorials, check out the IBM Granite Community. This tutorial is also available on GitHub
This cell installs the core libraries required to use the IBM Granite model hosted on Hugging Face:
The
Note: If you are running this tutorial in a virtual environment and do not have langgrapg preinstalled, use pip install langgraph to install it in your local environment.
This cell imports all the core libraries needed for building and running the agentic workflow:
Together, these imports prepare the environment for model interaction, workflow structuring and output presentation.
This cell loads IBM’s
The
This step effectively initializes the Granite model as the "reasoning engine" behind our agentic workflow.
This function,
Key details:
This function will be reused throughout the agentic workflow to invoke the Granite model at various decision or generation nodes.
This function,
Input:
Prompt construction: The prompt asks the model to:
Text generation: The prompt is passed to the
Output parsing: A simple loop extracts the genre and tone from the model’s response based on line prefixes (
State update: The extracted
This node acts as the creative classifier, enabling subsequent nodes to generate contextually aligned outlines, structures and scenes by using genre and tone as foundational parameters.
The
Input: The function receives the state dictionary containing:
Prompt construction: The model is instructed to:
Text generation: The prompt is sent to
State update: The generated plot outline is added to the state under the key
This node translates abstract creative intent into a narrative sketch, providing a scaffold for the detailed three-act structure that follows. It ensures that downstream nodes work from a coherent, imaginative baseline.
The
Input: The node takes the state dictionary, which now includes:
Prompt construction: The model is instructed to:
Scene requirements:
Text generation:
State update: The generated scene is added to the state dictionary under the
It introduces narrative immersion and visual storytelling to the workflow. Instead of merely summarizing the story, this node brings it to life with sensory and emotional detail—essential for scripting animated shorts.
The
Input: The node expects state
Prompt construction: The model is guided to:
Guidelines for the dialog:
Generation: The
State update: The dialog is saved into the state under the
This helper function
Function purpose: Wrap a node (for example, generate_scene_node) with a decorator that logs:
Parameters:
Internal wrapper:
Returns: A modified version of the function that adds progress messages but otherwise behaves identically.
As workflows grow, it becomes important to track which step is being executed, especially if some steps (like generation or editing) take longer or might cause issues such as memory overload. This progress wrapper ensures transparency and is helpful for debugging and runtime diagnostics.
This cell defines the workflow logic for generating a short animated story by using LangGraph, a compositional graph-based programming framework designed for LLM workflows. Each step in the graph represents a creative task and they are executed in a specific sequence to produce the final screenplay.
Components of the workflow:
Node registration with progress tracking: Each step (genre selection, outline generation, scene writing, dialog writing) is added as a node with the with_progress() wrapper -
This approach ensures that each node logs its runtime and progress when executed.
Workflow edges (node sequencing): The sequence of the creative pipeline is clearly defined:
This structure enables a modular, readable and debuggable LLM workflow. Each stage in your creative process is isolated, can be profiled separately and can later be swapped or extended (for example, adding a “revise scene” step or “summarize output” node). The
This final code cell is where you run the complete creative workflow and display the results of each stage of story generation.
Displaying results: The final state dictionary now contains keys populated by various nodes:
This section demonstrates how user intent is transformed into a complete miniscript through a step-wise, modular LLM workflow. It’s an end-to-end creative pipeline that is interactive, interpretable and customizable.
Note: The code will take approximately 15–17 mins to run if you are using GPU or TPU. It will take around 65–70 mins time on a local virtual environment to run and generate the output based on the infrastructure used to run the cede.
Let's understand how the system transforms the user’s prompt—“I want to write a whimsical fantasy story for children about a lost dragon finding its home”—into a complete animated story. Each step builds on the previous one, guided by the workflow’s creative nodes and powered by the Granite model.
1. Genre and tone. The workflow begins by interpreting the user's original prompt: I want to write a whimsical fantasy story for children about a lost dragon finding its home. Based on this input, the select_genre_node correctly classifies the narrative as whimsical fantasy and identifies the appropriate enchanting and heartwarming tone. This outcome is accurate and contextually aligned, as the use of phrases like “whimsical,” “for children” and “lost dragon finding its home” clearly signals a magical yet gentle storytelling style. The genre and tone act as foundational parameters that shape every subsequent generation step in the workflow.
2. Plot outline and character descriptions. In the next step, the model is asked to create a plot outline based on the identified genre, tone and the user's original idea. The output not only includes a 3–5 sentence story summary but also includes bonus character descriptions, likely due to prompt leakage or retained instruction formatting from earlier iterations.
The plot outline centers around a girl named Lily who discovers a wounded dragon and helps it return to the enchanted forest with guidance from an old herbalist. This storyline mirrors the user’s intent exactly—focusing on a child-friendly magical journey with emotional undertones about healing, belonging and friendship. The character sketches of the dragon, Lily and the herbalist add depth, transforming a vague idea into a structured concept with defined roles, personalities and narrative responsibilities. This step ensures the story moves from abstract intention to a tangible structure suitable for screen adaptation.
3. Key scene. Given the full plot outline, the
The chosen moment is when Lily tends to the wounded dragon in the enchanted forest, establishing emotional rapport and mutual understanding between the characters. This moment is critical as it pivots the story toward the dragon’s homecoming. The scene is rich in imagery and emotion, adhering to the "whimsical" and "heartwarming" constraints while also being visually expressive—perfectly suited for a short animated format.
The model’s ability to maintain tone and genre consistency across stages demonstrates the value of LangGraph’s state-passing workflow and the reasoning capabilities of the Granite model.
4. Dialog in screenplay format. Finally, the
Each stage of the workflow translates the original prompt— "a whimsical fantasy story for children about a lost dragon finding its home" —into a structured, creative and expressive narrative output. From genre selection to dialog formatting, the system incrementally builds a coherent storytelling arc. The LangGraph framework ensures that transitions between tasks are logically connected, and the IBM Granite model enables context-sensitive text generation with a consistent tone. The result is a compact, screen-ready short animation story that emerges entirely from a single-line user input—demonstrating the practical power of agentic workflows in creative AI applications.
To make the storytelling experience even more engaging, here is a simple HTML-based visualization that beautifully formats the generated story elements—genre, tone, plot, scene and dialog. Plus, with just one click, you can download the entire script as a text file for future use or sharing. Let’s bring the story to life on screen!
This tutorial uses the Granite-4.0-Tiny-Preview model for text generation. While it’s one of the smaller models in the Granite family, it still requires a GPU-enabled environment to run efficiently—especially when executing multiple nodes in a LangGraph workflow.
Recommended setup:
Performance notes:
If you’re running this tutorial in a hosted notebook environment (for example, IBM watsonx.ai or Google Colab Pro), ensure that GPU is enabled in the runtime settings.
For low-resource environments, consider:
In this tutorial, we built a modular, agentic storytelling workflow by using LangGraph and IBM’s Granite-4.0-Tiny-Preview language model. Starting from a simple creative prompt, we constructed a step-by-step pipeline that classifies genre and tone, generates a plot outline, writes a key scene and finishes with screenplay-style dialog. Along the way, we demonstrated how to:
This agentic framework is not only powerful for screen writing but can be extended to a wide range of creative or task-routing use cases. With just a few nodes, you’ve built a miniature writing assistant capable of turning a whimsical idea into a script-ready story.
Whether you're a developer, storyteller or researcher—this tutorial gives you a practical foundation to explore LLM-based workflow engineering in creative domains.
Ready to build your own agents? Let the creativity flow with IBM Granite models and IBM watsonx Orchestrate®.
Build, deploy and manage powerful AI assistants and agents that automate workflows and processes with generative AI.
Build the future of your business with AI solutions that you can trust.
IBM Consulting AI services help reimagine how businesses work with AI for transformation.
1 Lang Cao. 2024. GraphReason: Enhancing Reasoning Capabilities of Large Language Models through A Graph-Based Verification Approach. In Proceedings of the 2nd Workshop on Natural Language Reasoning and Structured Explanations (@ACL 2024), pages 1–12, Bangkok, Thailand. Association for Computational Linguistics.