A Step-by-Step Implementation Tutorial for Building Modular AI Workflows Using Anthropic’s Claude Sonnet 3.7 through API and LangGraph
In this tutorial, we provide a practical guide for implementing LangGraph, a streamlined, graph-based AI orchestration framework, integrated seamlessly with Anthropic’s Claude API. Through detailed, executable code optimized for Google Colab, developers learn how to build and visualize AI workflows as interconnected nodes performing distinct tasks, such as generating concise answers, critically analyzing responses, and automatically composing technical blog content. The compact implementation highlights LangGraph’s intuitive node-graph architecture. It can manage complex sequences of Claude-powered natural language tasks, from basic question-answering scenarios to advanced content generation pipelines.
from getpass import getpass
import os
anthropic_key = getpassos.environ= anthropic_key
printWe securely prompt users to input their Anthropic API key using Python’s getpass module, ensuring sensitive data isn’t displayed. It then sets this key as an environment variableand confirms successful storage.
import os
import json
import requests
from typing import Dict, List, Any, Callable, Optional, Union
from dataclasses import dataclass, field
import networkx as nx
import matplotlib.pyplot as plt
from IPython.display import display, HTML, clear_output
We import essential libraries for building and visualizing structured AI workflows. It includes modules for handling data, graph creation and visualization, interactive notebook display, and type annotationsfor clarity and maintainability.
try:
import anthropic
except ImportError:
print!pip install -q anthropic
import anthropic
from anthropic import Anthropic
We ensure the anthropic Python package is available for use. It attempts to import the module and, if not found, automatically installs it using pip in a Google Colab environment. After installation, it imports the Anthropic client, essential for interacting with Claude models via the Anthropic API. 4o
@dataclass
class NodeConfig:
name: str
function: Callable
inputs: List= fieldoutputs: List= fieldconfig: Dict= fieldThis NodeConfig data class defines the structure of each node in the LangGraph workflow. Each node has a name, an executable function, optional inputs and outputs, and an optional config dictionary to store additional parameters. This setup allows for modular, reusable node definitions for graph-based AI tasks.
class LangGraph:
def __init__:
self.api_key = api_key or os.environ.getif not self.api_key:
from google.colab import userdata
try:
self.api_key = userdata.getif not self.api_key:
raise ValueErrorexcept:
printself.api_key = inputif not self.api_key:
raise ValueErrorself.client = Anthropicself.graph = nx.DiGraphself.nodes = {}
self.state = {}
def add_node:
self.nodes= node_config
self.graph.add_nodefor input_node in node_config.inputs:
if input_node in self.nodes:
self.graph.add_edgereturn self
def claude_node:
"""Convenience method to create a Claude API node"""
inputs = inputs oroutputs = outputs ordef claude_fn:
prompt = prompt_template
for k, v in state.items:
if isinstance:
prompt = prompt.replacemessage_params = {
"model": model,
"max_tokens": 1000,
"messages":}
if system_prompt:
message_params= system_prompt
response = self.client.messages.createreturn response.content.text
node_config = NodeConfigreturn self.add_nodedef transform_node:
"""Add a data transformation node"""
inputs = inputs oroutputs = outputs ornode_config = NodeConfigreturn self.add_nodedef visualize:
"""Visualize the graph"""
plt.figure)
pos = nx.spring_layoutnx.drawplt.titleplt.tight_layoutplt.showprintfor node in self.graph.nodes:
successors = list)
if successors:
print}")
else:
print")
printdef _get_execution_order:
"""Determine execution order based on dependencies"""
try:
return list)
except nx.NetworkXUnfeasible:
raise ValueErrordef execute:
"""Execute the graph in topological order"""
self.state = initial_state or {}
execution_order = self._get_execution_orderprintfor node_name in execution_order:
printnode = self.nodesinputs = {k: self.state.getfor k in node.inputs if k in self.state}
result = node.functionif len== 1:
self.state] = result
elif isinstance) and len== len:
for i, output_name in enumerate:
self.state= resultprintreturn self.state
def run_example:
"""Run an example LangGraph flow with a predefined question"""
printgraph = LangGraphdef question_provider:
return question
graph.transform_nodegraph.claude_nodegraph.claude_nodegraph.visualizeresult = graph.executeprintprintprintprint}\n")
print}\n")
print}")
printreturn graph
The LangGraph class implements a lightweight framework for constructing and executing graph-based AI workflows using Claude from Anthropic. It allows users to define modular nodes, either Claude-powered prompts or custom transformation functions, connect them via dependencies, visualize the entire pipeline, and execute them in topological order. The run_example function demonstrates this by building a simple question-answering and evaluation flow, showcasing the clarity and modularity of LangGraph’s architecture.
def run_advanced_example:
"""Run a more advanced example with multiple nodes for content generation"""
graph = LangGraphdef topic_selector:
return "Graph-based AI systems"
graph.transform_nodegraph.claude_nodegraph.claude_nodegraph.claude_nodedef assembler:
return f"# {state}\n\n{introduction}\n\n## Outline\n{outline}\n\n## Conclusion\n{conclusion}"
graph.transform_nodegraph.visualizeresult = graph.executeprintprintprintprint)
printreturn graph
The run_advanced_example function showcases a more sophisticated use of LangGraph by orchestrating multiple Claude-powered nodes to generate a complete blog post. It starts by selecting a topic, then creates an outline, an introduction, and a conclusion, all using structured Claude prompts. Finally, a transformation node assembles the content into a formatted blog post. This example demonstrates how LangGraph can automate complex, multi-step content generation tasks using modular, connected nodes in a clear and executable flow.
printquestion = "What are the three main advantages of using graph-based AI architectures?"
simple_graph = run_exampleprintadvanced_graph = run_advanced_exampleFinally, we trigger the execution of both defined LangGraph workflows. First, it runs the simple question-answering example by passing a predefined question to the run_examplefunction. Then, it initiates the more advanced blog post generation workflow using run_advanced_example. Together, these calls demonstrate the practical flexibility of LangGraph, from basic prompt-based interactions to multi-step content automation using Anthropic’s Claude API.
In conclusion, we have implemented LangGraph integrated with Anthropic’s Claude API, which illustrates the ease of designing modular AI workflows that leverage powerful language models in structured, graph-based pipelines. Through visualizing task flows and separating responsibilities among nodes, such as question processing, analytical evaluation, content outlining, and assembly, developers gain practical experience in building maintainable, scalable AI systems. LangGraph’s clear node dependencies and Claude’s sophisticated language capabilities provide an efficient solution for orchestrating complex AI processes, especially for rapid prototyping and execution in environments like Google Colab.
Check out the Colab Notebook. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter and don’t forget to join our 95k+ ML SubReddit and Subscribe to our Newsletter.
Asif RazzaqWebsite | + postsBioAsif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. His most recent endeavor is the launch of an Artificial Intelligence Media Platform, Marktechpost, which stands out for its in-depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts of over 2 million monthly views, illustrating its popularity among audiences.Asif Razzaqhttps://www.marktechpost.com/author/6flvq/Meta Researchers Introduced J1: A Reinforcement Learning Framework That Trains Language Models to Judge With Reasoned Consistency and Minimal DataAsif Razzaqhttps://www.marktechpost.com/author/6flvq/Sampling Without Data is Now Scalable: Meta AI Releases Adjoint Sampling for Reward-Driven Generative ModelingAsif Razzaqhttps://www.marktechpost.com/author/6flvq/Google AI Releases MedGemma: An Open Suite of Models Trained for Performance on Medical Text and Image ComprehensionAsif Razzaqhttps://www.marktechpost.com/author/6flvq/NVIDIA Releases Cosmos-Reason1: A Suite of AI Models Advancing Physical Common Sense and Embodied Reasoning in Real-World Environments
#stepbystep #implementation #tutorial #building #modular
A Step-by-Step Implementation Tutorial for Building Modular AI Workflows Using Anthropic’s Claude Sonnet 3.7 through API and LangGraph
In this tutorial, we provide a practical guide for implementing LangGraph, a streamlined, graph-based AI orchestration framework, integrated seamlessly with Anthropic’s Claude API. Through detailed, executable code optimized for Google Colab, developers learn how to build and visualize AI workflows as interconnected nodes performing distinct tasks, such as generating concise answers, critically analyzing responses, and automatically composing technical blog content. The compact implementation highlights LangGraph’s intuitive node-graph architecture. It can manage complex sequences of Claude-powered natural language tasks, from basic question-answering scenarios to advanced content generation pipelines.
from getpass import getpass
import os
anthropic_key = getpassos.environ= anthropic_key
printWe securely prompt users to input their Anthropic API key using Python’s getpass module, ensuring sensitive data isn’t displayed. It then sets this key as an environment variableand confirms successful storage.
import os
import json
import requests
from typing import Dict, List, Any, Callable, Optional, Union
from dataclasses import dataclass, field
import networkx as nx
import matplotlib.pyplot as plt
from IPython.display import display, HTML, clear_output
We import essential libraries for building and visualizing structured AI workflows. It includes modules for handling data, graph creation and visualization, interactive notebook display, and type annotationsfor clarity and maintainability.
try:
import anthropic
except ImportError:
print!pip install -q anthropic
import anthropic
from anthropic import Anthropic
We ensure the anthropic Python package is available for use. It attempts to import the module and, if not found, automatically installs it using pip in a Google Colab environment. After installation, it imports the Anthropic client, essential for interacting with Claude models via the Anthropic API. 4o
@dataclass
class NodeConfig:
name: str
function: Callable
inputs: List= fieldoutputs: List= fieldconfig: Dict= fieldThis NodeConfig data class defines the structure of each node in the LangGraph workflow. Each node has a name, an executable function, optional inputs and outputs, and an optional config dictionary to store additional parameters. This setup allows for modular, reusable node definitions for graph-based AI tasks.
class LangGraph:
def __init__:
self.api_key = api_key or os.environ.getif not self.api_key:
from google.colab import userdata
try:
self.api_key = userdata.getif not self.api_key:
raise ValueErrorexcept:
printself.api_key = inputif not self.api_key:
raise ValueErrorself.client = Anthropicself.graph = nx.DiGraphself.nodes = {}
self.state = {}
def add_node:
self.nodes= node_config
self.graph.add_nodefor input_node in node_config.inputs:
if input_node in self.nodes:
self.graph.add_edgereturn self
def claude_node:
"""Convenience method to create a Claude API node"""
inputs = inputs oroutputs = outputs ordef claude_fn:
prompt = prompt_template
for k, v in state.items:
if isinstance:
prompt = prompt.replacemessage_params = {
"model": model,
"max_tokens": 1000,
"messages":}
if system_prompt:
message_params= system_prompt
response = self.client.messages.createreturn response.content.text
node_config = NodeConfigreturn self.add_nodedef transform_node:
"""Add a data transformation node"""
inputs = inputs oroutputs = outputs ornode_config = NodeConfigreturn self.add_nodedef visualize:
"""Visualize the graph"""
plt.figure)
pos = nx.spring_layoutnx.drawplt.titleplt.tight_layoutplt.showprintfor node in self.graph.nodes:
successors = list)
if successors:
print}")
else:
print")
printdef _get_execution_order:
"""Determine execution order based on dependencies"""
try:
return list)
except nx.NetworkXUnfeasible:
raise ValueErrordef execute:
"""Execute the graph in topological order"""
self.state = initial_state or {}
execution_order = self._get_execution_orderprintfor node_name in execution_order:
printnode = self.nodesinputs = {k: self.state.getfor k in node.inputs if k in self.state}
result = node.functionif len== 1:
self.state] = result
elif isinstance) and len== len:
for i, output_name in enumerate:
self.state= resultprintreturn self.state
def run_example:
"""Run an example LangGraph flow with a predefined question"""
printgraph = LangGraphdef question_provider:
return question
graph.transform_nodegraph.claude_nodegraph.claude_nodegraph.visualizeresult = graph.executeprintprintprintprint}\n")
print}\n")
print}")
printreturn graph
The LangGraph class implements a lightweight framework for constructing and executing graph-based AI workflows using Claude from Anthropic. It allows users to define modular nodes, either Claude-powered prompts or custom transformation functions, connect them via dependencies, visualize the entire pipeline, and execute them in topological order. The run_example function demonstrates this by building a simple question-answering and evaluation flow, showcasing the clarity and modularity of LangGraph’s architecture.
def run_advanced_example:
"""Run a more advanced example with multiple nodes for content generation"""
graph = LangGraphdef topic_selector:
return "Graph-based AI systems"
graph.transform_nodegraph.claude_nodegraph.claude_nodegraph.claude_nodedef assembler:
return f"# {state}\n\n{introduction}\n\n## Outline\n{outline}\n\n## Conclusion\n{conclusion}"
graph.transform_nodegraph.visualizeresult = graph.executeprintprintprintprint)
printreturn graph
The run_advanced_example function showcases a more sophisticated use of LangGraph by orchestrating multiple Claude-powered nodes to generate a complete blog post. It starts by selecting a topic, then creates an outline, an introduction, and a conclusion, all using structured Claude prompts. Finally, a transformation node assembles the content into a formatted blog post. This example demonstrates how LangGraph can automate complex, multi-step content generation tasks using modular, connected nodes in a clear and executable flow.
printquestion = "What are the three main advantages of using graph-based AI architectures?"
simple_graph = run_exampleprintadvanced_graph = run_advanced_exampleFinally, we trigger the execution of both defined LangGraph workflows. First, it runs the simple question-answering example by passing a predefined question to the run_examplefunction. Then, it initiates the more advanced blog post generation workflow using run_advanced_example. Together, these calls demonstrate the practical flexibility of LangGraph, from basic prompt-based interactions to multi-step content automation using Anthropic’s Claude API.
In conclusion, we have implemented LangGraph integrated with Anthropic’s Claude API, which illustrates the ease of designing modular AI workflows that leverage powerful language models in structured, graph-based pipelines. Through visualizing task flows and separating responsibilities among nodes, such as question processing, analytical evaluation, content outlining, and assembly, developers gain practical experience in building maintainable, scalable AI systems. LangGraph’s clear node dependencies and Claude’s sophisticated language capabilities provide an efficient solution for orchestrating complex AI processes, especially for rapid prototyping and execution in environments like Google Colab.
Check out the Colab Notebook. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter and don’t forget to join our 95k+ ML SubReddit and Subscribe to our Newsletter.
Asif RazzaqWebsite | + postsBioAsif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. His most recent endeavor is the launch of an Artificial Intelligence Media Platform, Marktechpost, which stands out for its in-depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts of over 2 million monthly views, illustrating its popularity among audiences.Asif Razzaqhttps://www.marktechpost.com/author/6flvq/Meta Researchers Introduced J1: A Reinforcement Learning Framework That Trains Language Models to Judge With Reasoned Consistency and Minimal DataAsif Razzaqhttps://www.marktechpost.com/author/6flvq/Sampling Without Data is Now Scalable: Meta AI Releases Adjoint Sampling for Reward-Driven Generative ModelingAsif Razzaqhttps://www.marktechpost.com/author/6flvq/Google AI Releases MedGemma: An Open Suite of Models Trained for Performance on Medical Text and Image ComprehensionAsif Razzaqhttps://www.marktechpost.com/author/6flvq/NVIDIA Releases Cosmos-Reason1: A Suite of AI Models Advancing Physical Common Sense and Embodied Reasoning in Real-World Environments
#stepbystep #implementation #tutorial #building #modular
·75 مشاهدة