Fraud rings hide in the links between records. One account looks fine. One transfer looks fine. The ring only appears when you see the shared phone, the reused device, or money moving on within minutes. Graph Hacks, FalkorDB's online hackathon in October 2026, drew 7,899 developers and 1,213 projects, and several teams built fraud detectors on a graph. On its own seeded test data of 81 accounts and 290 transactions, FraudScope AI found all 3 seeded rings with algo.WCC and no ring labels. This graph database fraud detection tutorial teaches that pattern and three others in 6 steps, with the real output of every step.
algo.WCC returns its full ring of 6. One public Wi-Fi IP pulled 3 honest customers into a ring until it was taken out as a hub. A plain path query reached 2 accounts the stolen money never touched, and a time rule dropped both. algo.betweenness ranked first a broker that shares no identifier with anyone, so ring detection can't see it.What is graph database fraud detection?
Graph database fraud detection stores accounts, phones, devices and transfers as connected nodes and edges, so software can find rings and trace money by walking the links instead of comparing records one by one.
FalkorDB fits because Cypher queries, graph algorithms and read-only access all run in one place, so an agent can call WCC or betweenness with no export to a separate library.
What does this graph database fraud detection tutorial build?
A small detector in Python, on 18 invented accounts and 20 transfers. Each step uses a pattern from a Graph Hacks project. Every output below comes from running this code on FalkorDB graph module 4.22.3 and 6.0.1.
| Step | What you add | Pattern from |
|---|---|---|
| 2 | Shared phones, devices and IPs as nodes | Diktiax |
| 3 | Ring clusters with algo.WCC | FraudScope AI |
| 4 | Time-ordered money trail | FlowTrace, TraceHold |
| 5 | Broker ranking with algo.betweenness | TraceHold, FraudScope AI |
| 6 | Read-only agent tools | Diktiax, FlowTrace, TraceHold |
Step 1: Start FalkorDB and connect
Start a local instance, install the Python client, and create the graph with an index on each lookup label.
pip install falkordb
docker run -p 6379:6379 -p 3000:3000 -it --rm falkordb/falkordb:latest
from falkordb import FalkorDB
db = FalkorDB(host="localhost", port=6379)
if "tut-fraud-demo" in db.list_graphs():
db.select_graph("tut-fraud-demo").delete() # start clean on every run
g = db.select_graph("tut-fraud-demo")
for label in ("Account", "Phone", "Device", "IP"):
g.query(f"CREATE INDEX FOR (n:{label}) ON (n.id)")
print(g.query("CALL db.indexes() YIELD label RETURN label ORDER BY label").result_set)
Output:
[['Account'], ['Device'], ['IP'], ['Phone']]
Try it. Skip Docker if you like. Create a free instance, then pass its host, port, username and password to FalkorDB(). Start free on FalkorDB Cloud.
Step 2: Turn shared phones and devices into nodes
Diktiax · Prabal Patra · See the code · Use the app · Blog
Diktiax models anything a ring reuses as its own node, merged on its value. Two accounts with one phone number then point at one Phone node, so the link exists the moment the second account is loaded.
In the builder's words. "There's no separate matching step." Prabal Patra, Diktiax
The sample data hides two rings among honest customers. Time is in minutes of the day for readability, so use epoch time in real data. Transfers merge on a transaction ID, so a repeat load adds nothing.
# account, phone, device, last part of the IP
accounts = [
# Ring A is a chain: no single identifier covers all six accounts
("A1", "0101", "a1", 11), ("A2", "0101", "a2", 12), ("A3", "0103", "a2", 13),
("A4", "0104", "a4", 13), ("A5", "0105", "a4", 15), ("S1", "0106", "a4", 16),
# Ring B shares one phone, and B3 also uses a public Wi-Fi IP (.99)
("B1", "0201", "b1", 21), ("B2", "0201", "b2", 22), ("B3", "0201", "b3", 99),
# H1 and H2 are flatmates, H3 to H5 are honest customers on that same Wi-Fi
("H1", "0301", "h1", 31), ("H2", "0302", "h2", 31), ("H3", "0303", "h3", 99),
("H4", "0304", "h4", 99), ("H5", "0305", "h5", 99),
# Broker, victim and two exchanges share nothing with anyone
("M1", "0401", "m1", 41), ("V1", "0501", "v1", 51),
("X1", "0601", "x1", 61), ("X2", "0701", "x2", 71),
]
# from, to, amount, minute of the day
transfers = [
("A1", "S1", 9900, 400), ("A2", "S1", 9700, 405), ("A3", "S1", 9800, 410),
("A4", "S1", 9600, 415), ("A5", "S1", 9500, 420), ("B1", "B2", 9400, 430),
("B3", "B2", 9200, 435), ("H1", "H2", 900, 200), # H1 pays H2 the rent
("V1", "A1", 9500, 540), ("A1", "A2", 9400, 552), ("A2", "A3", 9300, 561),
("A3", "M1", 9200, 575), ("M1", "X1", 9100, 590), # the stolen money
("S1", "M1", 38000, 620), ("A4", "M1", 2000, 600), ("A5", "M1", 2000, 605),
("B2", "M1", 18000, 610), ("M1", "X2", 50000, 650),
("A2", "H3", 1200, 300), # paid before the stolen money arrived
("A3", "H4", 800, 1300), # paid over 12 hours later
]
ADD_ACCOUNTS = """UNWIND $rows AS r
MERGE (a:Account {id: r[0]})
MERGE (p:Phone {id: '555-' + r[1]})
MERGE (d:Device {id: 'dev-' + r[2]})
MERGE (i:IP {id: '203.0.113.' + toString(r[3])})
MERGE (a)-[:HAS_PHONE]->(p)
MERGE (a)-[:USES_DEVICE]->(d)
MERGE (a)-[:USES_IP]->(i)"""
ADD_TRANSFERS = """UNWIND $rows AS r
MATCH (a:Account {id: r[1]}), (b:Account {id: r[2]})
MERGE (a)-[t:TRANSFERRED {id: r[0]}]->(b)
SET t.amount = r[3], t.ts = r[4]"""
for run in ("first load", "second load"):
a = g.query(ADD_ACCOUNTS, {"rows": accounts})
t = g.query(ADD_TRANSFERS, {"rows": [[f"T{n}", *row] for n, row in enumerate(transfers)]})
print(run, "nodes:", int(a.nodes_created), "edges:",
int(a.relationships_created + t.relationships_created))
SHARED = "MATCH (:Phone {id: '555-0201'})<-[:HAS_PHONE]-(a:Account) RETURN count(a)"
print(g.query(SHARED).result_set)
Output:
first load nodes: 61 edges: 74
second load nodes: 0 edges: 0
[[3]]
The second load created nothing, and Ring B's 3 accounts point at one Phone node.
Step 3: Cluster accounts into rings with algo.WCC
FraudScope AI · Manasi Anil Choudhari, Sainath Chavan, Deep Kale · See the code · Use the app · Blog
Start with the obvious question. Who shares anything with A1?
PAIRS = """MATCH (:Account {id: 'A1'})-[:HAS_PHONE|USES_DEVICE|USES_IP]->()
<-[:HAS_PHONE|USES_DEVICE|USES_IP]-(other:Account)
WHERE other.id <> 'A1'
RETURN DISTINCT other.id"""
print(g.query(PAIRS).result_set)
Output:
[['A2']]
That finds 1 of the 5 other accounts in Ring A. A1 shares a phone with A2, A2 shares a device with A3, and so on down the chain. A pairwise query can't see past the first link.
Weakly connected components (WCC) can, because it groups nodes joined by any path. FraudScope AI runs algo.WCC over its device and IP edges, then scores each cluster by the money moving inside it and by structuring, meaning transfers between $9,000 and $9,999. The version here is adapted from its README query and adds phones.
FIND_RINGS = """CALL algo.WCC({nodeLabels: ['Account', 'Phone', 'Device', 'IP'],
relationshipTypes: ['HAS_PHONE', 'USES_DEVICE', 'USES_IP']})
YIELD node, componentId
WITH componentId, [n IN collect(node) WHERE n:Account] AS accounts
WHERE size(accounts) > 1
UNWIND accounts AS a
OPTIONAL MATCH (a)-[t:TRANSFERRED]->(b:Account) WHERE b IN accounts
RETURN [x IN accounts | x.id] AS ring, count(t) AS inside,
sum(CASE WHEN t.amount >= 9000 AND t.amount < 10000 THEN 1 ELSE 0 END) AS structured
ORDER BY structured DESC, size(ring) DESC"""
def show_rings():
for ring, inside, structured in g.query(FIND_RINGS).result_set:
print(sorted(ring), "transfers inside:", inside, "structured:", int(structured))
show_rings()
Output:
['A1', 'A2', 'A3', 'A4', 'A5', 'S1'] transfers inside: 7 structured: 7
['B1', 'B2', 'B3', 'H3', 'H4', 'H5'] transfers inside: 2 structured: 2
['H1', 'H2'] transfers inside: 1 structured: 0
The score handles the flatmates, with 0 structured transfers. The second row has a real problem, though. WCC follows every edge, so the public Wi-Fi IP joined Ring B to 3 honest customers. Take any IP with many users out of the algorithm by changing its label. The cutoff of 4 suits this sample, so choose yours from your own data.
REMOVE_HUBS = """MATCH (a:Account)-[:USES_IP]->(i:IP)
WITH i, count(a) AS users WHERE users >= 4
SET i:SharedHub REMOVE i:IP
RETURN i.id, users"""
print(g.query(REMOVE_HUBS).result_set)
show_rings()
Output:
[['203.0.113.99', 4]]
['A1', 'A2', 'A3', 'A4', 'A5', 'S1'] transfers inside: 7 structured: 7
['B1', 'B2', 'B3'] transfers inside: 2 structured: 2
['H1', 'H2'] transfers inside: 1 structured: 0
Now there are two rings and one harmless pair. FraudScope AI's submission says WCC finds all 3 seeded rings in about 20 ms, on a dataset its README gives as 81 accounts and 290 transactions.
In the builder's words. "The agent never reads the answer key." FraudScope AI README
Step 4: Follow the money with a time-ordered path
FlowTrace · Genga K, Om Sharma · See the code · Use the app · Blog
A path through transfers isn't a money trail unless the times line up. FlowTrace's README puts the rule in a query comment: "money can only move on after it arrived". It also caps the gap between hops. The query below applies both rules with a 120-minute gap, starting from the minute the theft happened.
TRAIL = "MATCH p = (:Account {id: $id})-[:TRANSFERRED*1..5]->(end:Account)"
TIME_RULE = """
WITH p, relationships(p) AS hops
WHERE hops[0].ts >= $since AND hops[0].ts <= $since + 120
AND all(i IN range(0, size(hops) - 2)
WHERE hops[i + 1].ts >= hops[i].ts
AND hops[i + 1].ts <= hops[i].ts + 120)"""
RESULT = """
RETURN [n IN nodes(p) | n.id] AS path, [t IN relationships(p) | t.ts] AS times
ORDER BY size(path), path"""
TRACE = TRAIL + TIME_RULE + RESULT
for path, times in g.query(TRACE, {"id": "V1", "since": 540}).result_set:
print("->".join(path), times)
Output:
V1->A1 [540]
V1->A1->A2 [540, 552]
V1->A1->A2->A3 [540, 552, 561]
V1->A1->A2->A3->M1 [540, 552, 561, 575]
V1->A1->A2->A3->M1->X1 [540, 552, 561, 575, 590]
V1->A1->A2->A3->M1->X2 [540, 552, 561, 575, 650]
The money leaves V1 at minute 540 and reaches both exchanges through M1. Now the check that must return nothing. H3 and H4 both received money from accounts on the trail, so a plain path reaches them. Neither payment can be the stolen money.
TO = " WHERE end.id = $end"
for end in ("H3", "H4"):
params = {"id": "V1", "since": 540, "end": end}
print(end, "any path:", g.query(TRAIL + TO + RESULT, params).result_set)
print(end, "with time rule:", g.query(TRAIL + TO + TIME_RULE + RESULT, params).result_set)
Output:
H3 any path: [[['V1', 'A1', 'A2', 'H3'], [540, 552, 300]]]
H3 with time rule: []
H4 any path: [[['V1', 'A1', 'A2', 'A3', 'H4'], [540, 552, 561, 1300]]]
H4 with time rule: []
Both are empty for the right reason. A2 paid H3 at minute 300, before the money reached A2 at minute 552. A3 paid H4 at minute 1300, long past the 120-minute gap after minute 561. Without this rule, both customers would look like recipients of stolen money.
One query is fine for a small graph like this. FlowTrace and TraceHold trace one hop at a time, and FlowTrace also stops at hubs like merchants and payroll. Try that once accounts have many transfers, since the number of paths can grow fast with each hop.
Step 5: Rank brokers with algo.betweenness
TraceHold · Shivani Ponturu · See the code · Use the app · Blog
Betweenness counts how many shortest paths run through an account. FraudScope AI uses it to find brokers on the most money paths. The call below is TraceHold's, with a RETURN added. Its docs explain the seed: "a fixed seed gives repeatable scores".
BROKERS = """CALL algo.betweenness({nodeLabels: ['Account'], relationshipTypes: ['TRANSFERRED'], samplingSize: 100, samplingSeed: 42}) YIELD node, score
RETURN node.id AS account, score ORDER BY score DESC, account LIMIT 5"""
print("top broker:", g.query(BROKERS).result_set[0][0])
Output:
top broker: M1
My favorite part is that M1 never appeared in step 3. It shares no phone, device or IP with anyone, so WCC had nothing to cluster. The money paths give it away. Read these scores as a ranking inside one run, because samplingSize makes them estimates. The code prints only the top name because lower ranks and raw scores can change between FalkorDB versions.
Step 6: Give an agent read-only tools
TraceHold's README sets the rule: "The agents never write database queries." An agent picks a tool and passes parameters, and your code holds the Cypher. FlowTrace and Diktiax both run agent reads through GRAPH.RO_QUERY, and Diktiax adds a 5-second timeout. So the database itself refuses a write, whatever the model asks for.
TOOLS = {"find_rings": FIND_RINGS, "trace_money": TRACE, "rank_brokers": BROKERS}
def run_tool(name, **params):
return g.ro_query(TOOLS[name], params, timeout=5000).result_set
print(run_tool("trace_money", id="A3", since=561)[0])
try:
g.ro_query("MATCH (a:Account {id: 'A1'}) SET a.frozen = true")
except Exception as error:
print("refused:", error)
print(g.ro_query("MATCH (a:Account) WHERE a.frozen = true RETURN count(a)").result_set)
Output:
[['A3', 'M1'], [575]]
refused: graph.RO_QUERY is to be executed only on read-only queries
[[0]]
The tool returned the next hop from A3. The write failed with a server error, and the count confirms no account changed. When you finish, delete the sample graph.
g.delete()
print("tut-fraud-demo" in db.list_graphs())
Output:
False
All four projects serve their tools over MCP. The FalkorDB MCP Server docs cover a ready-made server, and its configuration page explains the read-only settings. That server takes queries from the model, not your fixed list. MCP isn't part of the code run for this post.
Keep a person in charge of any action. In FraudScope AI, the approve tool is deliberately not an MCP tool, so agents can only propose.
Try it. Replace the sample lists with one day of your own transfers and run find_rings.
To see how full projects scored, read how Graph Hacks teams catch fraud rings with a graph.
If you want to keep going, read how a company brain stays safe with fixed read-only queries and 5 agent memory patterns from 647 Graph Hacks projects.
Frequently asked questions
What do I need for this graph database fraud detection tutorial?
Python, the falkordb client, and either Docker or a free FalkorDB Cloud instance. The code here ran on FalkorDB graph module 4.22.3 and 6.0.1, with the same output on both.
How does algo.WCC find fraud rings?
It groups accounts joined by any chain of shared phones, devices or IPs, even when two accounts share nothing directly. In the sample data, a pairwise query linked A1 to 1 account, and WCC returned its ring of 6.
Why does tracing stolen money need a time rule?
A payment made before the money arrived can't contain it. A plain path query treats every connected account as a recipient. In the sample data, it reached 2 accounts that a time-ordered query excluded.
What does betweenness add to ring detection?
WCC finds accounts tied together by shared identifiers. Betweenness finds accounts that sit on many money paths, including brokers that share nothing with anyone. In the sample data, it ranked such a broker first.
How do I stop an AI agent from changing the graph?
Give it fixed tools that hold the Cypher, and run them through GRAPH.RO_QUERY. FalkorDB then refuses any write with a server error, whatever the model asks for.
Is this enough for real fraud detection?
No. This is 18 invented accounts, built to show each pattern. The four projects go further, and they tested on generated or synthetic data too. Real data needs tuned cutoffs, testing against known cases, and a person approving each action.
Find your first ring
Every project here runs on the same FalkorDB you can start as a free cloud instance, or with one Docker command, docker run -p 6379:6379 -p 3000:3000 -it --rm falkordb/falkordb:latest. Load your accounts with their phones and devices merged as nodes, then run one algo.WCC call.
Not ready to build yet? Join the FalkorDB Discord to get help from our engineers, share what you build, and hear about the next Graph Hacks first.