CrewAI
Data Analysis Agent with CrewAI
Build an AI data analysis agent that processes datasets, runs Python computations, generates charts, and produces insights — powered by CrewAI.
data analysisPythonchartsanalytics
Working Code
from crewai import Agent, Crew, Taskfrom langchain_openai import ChatOpenAIfrom langchain_core.tools import tool
@tooldef execute_python(code: str) -> str: """Execute Python code for data analysis. pandas and matplotlib are available.""" import subprocess result = subprocess.run( ["python", "-c", code], capture_output=True, text=True, timeout=30, ) return result.stdout or f"Error: {result.stderr}"
@tooldef read_csv_info(path: str) -> str: """Get info about a CSV file (columns, types, shape).""" import pandas as pd df = pd.read_csv(path) return f"Shape: {df.shape}\nColumns: {list(df.columns)}\nTypes:\n{df.dtypes}"
agent = Agent( role="Specialist", goal="You are a data analyst. Explore datasets using read_csv_info, then use execute_python to run analysis code. Present findings clearly with numbers.", tools=[execute_python, read_csv_info], llm=ChatOpenAI(model="gpt-4o"),)
task = Task( description="Analyze sales_data.csv and find the top performing products by revenue", expected_output="Detailed response", agent=agent,)
crew = Crew(agents=[agent], tasks=[task])result = crew.kickoff()print(result.raw)Step by Step
1
Install dependencies
Install CrewAI and the required tools for this use case.
2
Define your tools
Create the domain-specific tool functions your agent will use to interact with external services.
3
Create the agent and run
Initialize the CrewAI agent with your tools, set the system prompt, and execute a query.
Build with other frameworks
Ready to build with CrewAI?
Generate a production-ready project with CrewAI pre-configured — FastAPI + Next.js, auth, streaming, and more.
Get StartedReady to build your first production AI agent?
Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.