LangChain vs CrewAI vs AutoGen to Build a Data Analysis Agent
In today’s data-driven world, organizations rely on data analysts to interpret complex datasets, uncover actionable insights, and drive decision-making. But what if we could enhance the efficiency and scalability of this process using AI? Enter the Data Analysis Agent, to automate analytical tasks, execute code, and adaptively respond to data queries. LangGraph, CrewAI, and AutoGen are three popular frameworks used to build AI Agents. We will be using and comparing all three in this article to build a simple data analysis agent.
Table of Contents
- Working of Data Analysis Agent
- Building a Data Analysis Agent with LangGraph
- Pre-requisites
- Steps to Build a Data Analysis Agent with LangGraph
- 1. Import the necessary libraries.
- 2. Let’s define the state.
- 3. Define the LLM and the code execution function and bind the function to the LLM.
- 4. Define the function for the agent to reply and add it as a node to the graph.
- 5. Define the ToolNode and add it to the graph.
- 6. Let us also add the memory so that we can chat with the agent.
- 7. Compile and display the graph.
- 8. Now we can start the chat. Since we have added memory, we will give each conversation a unique thread_id and start the conversation on that thread.
- Building a Data Analysis Agent with CrewAI
- 1. Import the necessary libraries.
- 2. We will build one agent for generating the code and another for executing that code.
- 3. To execute the code, we will use PythonREPL(). Define it as a crewai tool.
- 4. Define executing agent and tasks with access to repl and FileReadTool()
- 5. Build the crew with both agents and corresponding tasks.
- 6. Run the crew with the following inputs.
- Building a Data Analysis Agent with AutoGen
- 1. Import the necessary libraries.
- 2. Define the code executor and an agent to use the code executor.
- 3. Define an agent to write the code with a custom system message.
- 4. Define the problem to solve and initiate the chat.
- 5. We can also print the questions asked by us and their answers, if required, using this code.
- LangGraph vs CrewAI vs AutoGen
- Frequently Asked Questions
Working of Data Analysis Agent
The data analysis agent will first take the query from the user and generate the code to read the file and analyze the data in the file. Then the generated code will be executed using the Python repl tool. The result of the code is sent back to the agent. The agent then analyzes the result received from the code execution tool and replies to the user query. LLMs can generate arbitrary code, so we must carefully execute the LLM-generated code in a local environment.
Building a Data Analysis Agent with LangGraph
If you are new to this topic or wish to brush up on your knowledge of LangGraph, here’s an article I would recommend: What is LangGraph?
Pre-requisites
Before building agents, ensure you have the necessary API keys for the required LLMs.
Load the .env file with the API keys needed.
1 2 3 |
|
Key Libraries Required
langchain – 0.3.7
langchain-experimental – 0.3.3
langgraph – 0.2.52
crewai – 0.80.0
Crewai-tools – 0.14.0
autogen-agentchat – 0.2.38
Now that we’re all set, let’s begin building our agent.
Steps to Build a Data Analysis Agent with LangGraph
1. Import the necessary libraries.
1 2 3 4 5 6 7 8 9 10 11 |
|
2. Let’s define the state.
1 2 3 |
|
3. Define the LLM and the code execution function and bind the function to the LLM.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
4. Define the function for the agent to reply and add it as a node to the graph.
1 2 3 4 |
|
5. Define the ToolNode and add it to the graph.
1 2 3 |
|
If the LLM returns a tool call, we need to route it to the tool node; otherwise, we can end it. Let’s define a function for routing. Then we can add other edges.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
6. Let us also add the memory so that we can chat with the agent.
1 2 3 |
|
7. Compile and display the graph.
1 2 3 |
|
8. Now we can start the chat. Since we have added memory, we will give each conversation a unique thread_id and start the conversation on that thread.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
While the loop is running, we start by giving the path of the file and then asking any questions based on the data.
The output will be as follows:
As we have included memory, we can ask any questions on the dataset in the chat. The agent will generate the required code and the code will be executed. The code execution result will be sent back to the LLM. An example is shown below:
Also Read: How to Create Your Personalized News Digest Agent with LangGraph
Building a Data Analysis Agent with CrewAI
Now, we will use CrewAI for data analysis task.
1. Import the necessary libraries.
1 2 3 4 |
|
2. We will build one agent for generating the code and another for executing that code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
3. To execute the code, we will use PythonREPL(). Define it as a crewai tool.
1 2 3 4 |
|
4. Define executing agent and tasks with access to repl and FileReadTool()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
5. Build the crew with both agents and corresponding tasks.
1 2 3 4 5 |
|
6. Run the crew with the following inputs.
1 2 3 4 5 6 |
|
Here’s how the output will look like:
Also Read: Build LLM Agents on the Fly Without Code With CrewAI
Building a Data Analysis Agent with AutoGen
1. Import the necessary libraries.
1 2 |
|
2. Define the code executor and an agent to use the code executor.
1 2 3 4 5 6 7 8 9 10 |
|
3. Define an agent to write the code with a custom system message.
Take the code_writer system message from https://microsoft.github.io/autogen/0.2/docs/tutorial/code-executors/
1 2 3 4 5 6 |
|
4. Define the problem to solve and initiate the chat.
1 2 3 4 5 6 7 |
|
Once the chat starts, we can also ask any subsequent questions on the dataset mentioned above. If the code encounters any error, we can ask to modify the code. If the code is fine, we can just press ‘enter’ to continue executing the code.
5. We can also print the questions asked by us and their answers, if required, using this code.
1 2 3 4 5 6 7 8 9 10 |
|
Here’s the result:
Also Read: Hands-on Guide to Building Multi-Agent Chatbots with AutoGen
LangGraph vs CrewAI vs AutoGen
Now that you’ve learned to build a data analysis agent with all the 3 frameworks, let’s explore the differences between them, when it comes to code execution:
Framework | Key Features | Strengths | Best Suited For |
---|---|---|---|
LangGraph | – Graph-based structure (nodes represent agents/tools, edges define interactions) – Seamless integration with PythonREPL |
– Highly flexible for creating structured, multi-step workflows – Safe and efficient code execution with memory preservation across tasks |
Complex, process-driven analytical tasks that demand clear, customizable workflows |
CrewAI | – Collaboration-focused – Multiple agents working in parallel with predefined roles – Integrates with LangChain tools |
– Task-oriented design – Excellent for teamwork and role specialization – Supports safe and reliable code execution with PythonREPL |
Collaborative data analysis, code review setups, task decomposition, and role-based execution |
AutoGen | – Dynamic and iterative code execution – Conversable agents for interactive execution and debugging – Built-in chat feature |
– Adaptive and conversational workflows – Focus on dynamic interaction and debugging – Ideal for rapid prototyping and troubleshooting |
Rapid prototyping, troubleshooting, and environments where tasks and requirements evolve frequently |
Conclusion
In this article, we demonstrated how to build data analysis agents using LangGraph, CrewAI, and AutoGen. These frameworks enable agents to generate, execute, and analyze code to address data queries efficiently. By automating repetitive tasks, these tools make data analysis faster and more scalable. The modular design allows customization for specific needs, making them valuable for data professionals. These agents showcase the potential of AI to simplify workflows and extract insights from data with ease.
To know more about AI Agents, checkout our exclusive Agentic AI Pioneer Program!
Frequently Asked Questions
Q1. What are the key benefits of using AI frameworks like LangGraph, CrewAI, and AutoGen for data analysis?A. These frameworks automate code generation and execution, enabling faster data processing and insights. They streamline workflows, reduce manual effort, and enhance productivity for data-driven tasks.
Q2. Can these data analysis agents handle multiple datasets or complex queries?A. Yes, the agents can be customized to handle diverse datasets and complex analytical queries by integrating appropriate tools and adjusting their workflows.
Q3. What precautions should be taken when executing LLM-generated code?A. LLM-generated code may include errors or unsafe operations. Always validate the code in a controlled environment to ensure accuracy and security before execution.
Q4. How does memory integration enhance these data analysis agents?A. Memory integration allows agents to retain the context of past interactions, enabling adaptive responses and continuity in complex or multi-step queries.
Q5. What types of tasks can these data analysis agents automate?A. These agents can automate tasks such as reading files, performing data cleaning, generating summaries, executing statistical analyses, and answering user queries about the data.
The above is the detailed content of LangChain vs CrewAI vs AutoGen to Build a Data Analysis Agent. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Meta's Llama 3.2: A Leap Forward in Multimodal and Mobile AI Meta recently unveiled Llama 3.2, a significant advancement in AI featuring powerful vision capabilities and lightweight text models optimized for mobile devices. Building on the success o

Hey there, Coding ninja! What coding-related tasks do you have planned for the day? Before you dive further into this blog, I want you to think about all your coding-related woes—better list those down. Done? – Let’

This week's AI landscape: A whirlwind of advancements, ethical considerations, and regulatory debates. Major players like OpenAI, Google, Meta, and Microsoft have unleashed a torrent of updates, from groundbreaking new models to crucial shifts in le

Shopify CEO Tobi Lütke's recent memo boldly declares AI proficiency a fundamental expectation for every employee, marking a significant cultural shift within the company. This isn't a fleeting trend; it's a new operational paradigm integrated into p

Introduction OpenAI has released its new model based on the much-anticipated “strawberry” architecture. This innovative model, known as o1, enhances reasoning capabilities, allowing it to think through problems mor

Introduction Imagine walking through an art gallery, surrounded by vivid paintings and sculptures. Now, what if you could ask each piece a question and get a meaningful answer? You might ask, “What story are you telling?

For those of you who might be new to my column, I broadly explore the latest advances in AI across the board, including topics such as embodied AI, AI reasoning, high-tech breakthroughs in AI, prompt engineering, training of AI, fielding of AI, AI re

SQL's ALTER TABLE Statement: Dynamically Adding Columns to Your Database In data management, SQL's adaptability is crucial. Need to adjust your database structure on the fly? The ALTER TABLE statement is your solution. This guide details adding colu
