How to Build a fraud detection Agent Using CrewAI in TypeScript for healthcare

By Cyprian AaronsUpdated 2026-04-21
fraud-detectioncrewaitypescripthealthcare

A healthcare fraud detection agent reviews claims, provider activity, and patient billing patterns to flag suspicious cases before money leaves the system. It matters because healthcare fraud is expensive, hard to spot manually, and tightly regulated, so your agent needs to detect anomalies without leaking protected health information or breaking audit requirements.

Architecture

  • Claims ingestion layer

    • Pulls claims from your EHR/claims platform, batch files, or queue.
    • Normalizes CPT/ICD codes, provider IDs, dates of service, and amounts.
  • Policy and rules engine

    • Applies deterministic checks first.
    • Examples: duplicate claims, impossible service dates, excessive modifiers, mismatched provider specialty.
  • CrewAI agent layer

    • Uses a Crew with specialized Agents for anomaly analysis, compliance review, and case summarization.
    • Keeps the reasoning chain explicit for auditability.
  • Tools layer

    • Wraps internal APIs for claim lookup, provider history, eligibility verification, and audit logging.
    • Restrict tools to read-only access unless you have a separate human approval path.
  • Case management output

    • Produces structured findings for investigators.
    • Stores risk score, rationale, evidence pointers, and next actions.

Implementation

1) Install CrewAI for TypeScript and define your data model

Start by modeling the minimum claim payload you need. Keep PHI out of the agent input unless it is strictly required for detection.

// types.ts
export type Claim = {
  claimId: string;
  memberId: string;
  providerId: string;
  specialty: string;
  cptCodes: string[];
  icd10Codes: string[];
  amount: number;
  dateOfService: string;
  submittedAt: string;
};

export type FraudFinding = {
  claimId: string;
  riskScore: number;
  flags: string[];
  summary: string;
};

2) Build tools that fetch evidence from internal systems

CrewAI agents work best when they can call narrow tools instead of guessing. For healthcare, keep tools read-only and log every access for audit.

// tools.ts
import { Tool } from "@crewAIInc/crewai";

export const getClaimHistoryTool = new Tool({
  name: "get_claim_history",
  description: "Fetch prior claims for a member or provider from the claims system.",
  func: async ({ memberId }: { memberId: string }) => {
    const res = await fetch(`${process.env.CLAIMS_API_URL}/history?memberId=${memberId}`, {
      headers: { Authorization: `Bearer ${process.env.CLAIMS_API_TOKEN}` },
    });

    if (!res.ok) throw new Error("Failed to fetch claim history");
    return await res.json();
  },
});

export const getProviderProfileTool = new Tool({
  name: "get_provider_profile",
  description: "Fetch provider specialty and historical billing profile.",
  func: async ({ providerId }: { providerId: string }) => {
    const res = await fetch(`${process.env.PROVIDER_API_URL}/providers/${providerId}`, {
      headers: { Authorization: `Bearer ${process.env.PROVIDER_API_TOKEN}` },
    });

    if (!res.ok) throw new Error("Failed to fetch provider profile");
    return await res.json();
  },
});

3) Create specialized agents and wire them into a crew

Use one agent for pattern detection and another for compliance review. That split keeps the output easier to explain during audits.

// crew.ts
import { Agent, Crew, Task } from "@crewAIInc/crewai";
import { getClaimHistoryTool, getProviderProfileTool } from "./tools";
import type { Claim } from "./types";

const anomalyAgent = new Agent({
  role: "Fraud Analyst",
  goal: "Detect suspicious healthcare billing patterns using claim history and provider context.",
  backstory:
    "You analyze claims for duplicate billing, upcoding signals, unusual frequency, and mismatched specialty patterns.",
});

const complianceAgent = new Agent({
  role: "Compliance Reviewer",
  goal: "Validate findings against healthcare policy constraints and produce an audit-friendly summary.",
  backstory:
    "You focus on explainability, minimum necessary data use, HIPAA-aware handling, and clear investigator notes.",
});

anomalyAgent.tools = [getClaimHistoryTool, getProviderProfileTool];

export function buildFraudCrew(claim: Claim) {
  const detectTask = new Task({
    description:
      `Analyze claim ${claim.claimId} for fraud indicators. Return flags, a risk score from 0-100,
       and cite evidence based on history and provider profile.`,
    expectedOutput:
      "JSON with claimId, riskScore, flags[], summary",
    agent: anomalyAgent,
    context: [claim],
  });

  const reviewTask = new Task({
    description:
      `Review the fraud analysis for claim ${claim.claimId}. Ensure the summary is concise,
       compliant with healthcare audit requirements, and does not expose unnecessary PHI.`,
    expectedOutput:
      "Final JSON ready for case management",
    agent: complianceAgent,
    context: [detectTask],
  });

  
import { Crew } from "@crewAIInc/crewai";

export function runFraudCrew(claimInput) {
  const crew = new Crew({
    agents: [anomalyAgent, complianceAgent],
    tasks: [detectTask as any, reviewTask as any],
    verbose: true,
    memory: false,
    

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides