Fraud Detection Using CrewAI (original) (raw)

Last Updated : 14 Apr, 2026

Fraud detection in finance requires analyzing large volumes of transactions and identifying anomalies. Here we will create a fraud detection project using CrewAI that analyzes financial transactions, identifies suspicious patterns, and generates a structured fraud report.

We will be using “Synthetic Financial Datasets For Fraud Detection” dataset which can be downloaded from Kaggle.

Setting Up the Environment

Before we start, we need to install CrewAI and set up the necessary tools:

!pip install crewai
!pip install crewai_tools

We also set our OpenAI API key if external LLMs are required:

Python `

import os os.environ["OPENAI_API_KEY"] = "your-api-key-here"

`

1. Importing Required Libraries

First, we import the necessary libraries to define agents, tasks, crews and tools for reading files.

from crewai import Agent, Task, Crew, Process from crewai_tools import FileReadTool

`

2. Defining Agents

Next, we define the agents that will work on this project. Each agent has a role, goal, backstory and optionally tools.

2.1 Data Collector

This agent is responsible for reading the dataset and summarizing it.

read_csv_tool = FileReadTool(file_path='/content/Synthetic_Financial_datasets_log.csv')

data_collector = Agent( role="Data Collector", goal="Load and profile the dataset using pandas, not by hallucinating.", backstory="You are responsible for loading and summarizing the real dataset from a CSV file.", tools=[read_csv_tool], verbose=True, reasoning=True, memory=True )

`

2.2 Pattern Recognizer

This agent analyzes the dataset to detect suspicious transactions, such as high-value amounts and abnormal types like TRANSFER and CASH_OUT.

Python `

pattern_recognizer = Agent( role="Pattern Recognizer", goal="Detect suspicious transactions using the actual dataset.", backstory="You analyze high-value amounts, suspicious transaction types (TRANSFER, CASH_OUT), and balance inconsistencies.", tools=[read_csv_tool], verbose=True, reasoning=True, memory=True )

`

2.3 Fraud Reporter

This agent prepares a professional summary report based on the findings from the previous agents.

Python `

reporter = Agent( role="Fraud Reporter", goal="Generate a fraud detection report summarizing anomalies.", backstory="You prepare professional reports based on the actual dataset findings.", verbose=True, reasoning=True, memory=True )

`

3. Assigning Tasks

We now define the tasks that each agent will perform. Each task includes a description, the agent responsible, and the expected output.

3.1 Load Task

This task instructs the Data Collector to read the dataset in batches and summarize it, highlighting any anomalies.

load_task = Task( description=( "Use FileReadTool to analyze anomalies in batches of 500 rows at a time " "from the dataset. Focus on suspicious transaction types (TRANSFER, CASH_OUT), " "very large amounts, and balance inconsistencies. Summarize anomalies with row indices and amounts." ), agent=data_collector, expected_output="Dataset profile with row count, column names, dtypes, missing values, and 5 real sample rows." )

`

3.2 Detect Task

This task instructs the Pattern Recognizer to identify anomalies, providing row indices and explanations.

Python `

detect_task = Task( description=( "Analyze the loaded dataset to identify anomalies: " "very high transaction amounts, suspicious types (TRANSFER, CASH_OUT), " "and balance inconsistencies. Provide examples with row indices." ), agent=pattern_recognizer, expected_output="A list of detected anomalies with explanations." )

`

3.3 Report Task

This task instructs the Fraud Reporter to prepare a structured report summarizing findings and recommendations.

Python `

report_task = Task( description="Prepare a structured fraud detection report summarizing anomalies and recommendations.", agent=reporter, expected_output="Fraud detection report (executive summary + findings + recommendations)." )

`

4. Creating the Crew

Now, we group all agents and tasks into a Crew. The Crew coordinates the execution sequence to ensure the workflow is orderly and systematic.

crew = Crew( agents=[data_collector, pattern_recognizer, reporter], tasks=[load_task, detect_task, report_task], verbose=True, planning=True, process=Process.sequential )

`

5. Executing the Workflow

Finally, we launch the Crew to run all tasks. The result will include the dataset profile, detected anomalies and a structured fraud report.

result = crew.kickoff() print("\n=== Final Fraud Report ===\n") print(result)

`

**Output:

The output contains:

You can download source code from here.