Open In App

Fraud Detection Using CrewAI

Last Updated : 15 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Fraud detection in finance requires analyzing large volumes of transactions and identifying anomalies. CrewAI helps streamline this process by assigning specialized AI agents to collect data, detect suspicious patterns and generate a structured fraud report.

We will create a fraud detection project using CrewAI. The goal is to detect anomalies in financial transactions, summarize findings and generate 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.

  • Agent: Defines an AI agent that performs tasks.
  • Task: Defines the tasks assigned to agents.
  • Crew: Groups agents and tasks for coordinated execution.
  • FileReadTool: A tool to read CSV datasets.
Python
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.

  • role: Defines what the agent does (e.g "Data Collector").
  • goal: The outcome the agent works toward (e.g., "Load and profile the dataset").
  • backstory: Context or skills that describe the agent's abilities.
  • tools: List of tools the agent can use like FileReadTool for reading CSV files.
  • verbose: When True, the agent explains its reasoning step-by-step.
  • reasoning: Enables the agent to use logical reasoning for its decisions.
  • memory: Allows the agent to retain context during task execution.
Python
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 like 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.

  • description: Detailed instructions of what the task involves.
  • agent: The agent assigned to complete this task.
  • expected_output: What the task should produce when completed.
Python
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.

  • agents: List of all agents working in the Crew.
  • tasks: List of tasks to be executed by the agents.
  • verbose: If True, provides detailed explanations of each agent's reasoning.
  • planning: If True, the Crew manages the workflow planning.
  • process: Execution mode (Process.sequential or Process.parallel).
Python
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.

  • crew.kickoff(): Starts the execution of the crew, runs all tasks in the defined order and returns the final result.
Python
result = crew.kickoff()
print("\n=== Final Fraud Report ===\n")
print(result)

Output:

The output contains:

  • A dataset profile including the total row count, column names, data types, missing values and sample records from the file.
  • A list of suspicious transactions flagged, showing row indices, transaction types, amounts and balance inconsistencies.
  • A structured fraud detection report with an executive summary, detailed findings and actionable recommendations.

You can download source code from here.


Explore