Book a free call

AI Agents in DeFi Security: How They Watch Protocols

AI agents in DeFi security watch contracts, pools and price feeds around the clock, flagging attacks before funds move. Here is how they work.

Hertiv engineering teamReviewed by a senior engineer
6 min read
AI Agents in DeFi Security: How They Watch Protocols
On this page

Short answer

AI agents in DeFi security are software agents that watch a protocol's contracts, transactions and price feeds in real time, looking for the patterns that precede an exploit: odd withdrawals, suspicious approvals, oracle price jumps and flash-loan activity. When a pattern matches, the agent alerts the team or triggers a pre-set response such as pausing a function, while a human decides what happens next.

What are AI agents in DeFi security?

AI agents in DeFi security are software agents that watch a decentralised finance protocol around the clock and flag the patterns that usually come before an exploit. They read on-chain data as it arrives: contract calls, token transfers, liquidity changes and price feed updates.

The agent does not replace an audit. An audit checks the code before launch; the agent watches the live system for behaviour the audit could not predict, such as a new attack pattern or a market event that changes the risk. The table below shows what each threat looks like to an agent.

Threat What the agent watches Typical response
Re-entrancy drain Repeated calls before balances update Alert, then pause the function
Flash-loan attack Large borrowed sums in one block Alert and freeze new borrows
Oracle manipulation Price feed jumps vs other sources Alert and switch to a backup feed
Rug pull Owner removing liquidity fast Alert and warn users
Governance attack Unusual voting power moves Alert the governance team

The agent's job is speed. A human watching a dashboard will miss a drain that finishes in seconds; an agent watching every block will not, provided its rules and models are kept current.

How do agents monitor smart contracts?

Agents monitor smart contracts by reading every call and event the contract emits, then comparing that activity against what normal use looks like. A lending contract that usually sees a few borrows an hour will stand out when it sees fifty in one block.

The agent keeps a baseline of normal behaviour for each function: who calls it, how often, with what amounts and in what order. When activity breaks that baseline, the agent scores the deviation and decides whether to alert, log or act.

Good monitoring covers the contract and the calls around it. An attacker rarely touches one function in isolation; they set up the attack across several contracts first. An agent that only watches the target contract will miss the setup.

What do agents watch in liquidity pools?

In liquidity pools, agents watch for sudden changes in balances, large single-sided trades and new pools that copy a known token. A pool that loses most of its liquidity in minutes is either under attack or being abandoned, and both need a fast alert.

The agent tracks the ratio of the two tokens, the size of recent swaps and the age of the pool. A new pool with a large first trade and no lock on the liquidity is a classic rug-pull setup, and agents flag it before users add funds.

Pool monitoring also helps honest teams. When a large trade moves the price more than expected, the agent can warn the team that slippage settings or pool depth need attention, which is a normal operations problem rather than an attack.

How do agents catch oracle and price attacks?

Oracle attacks work by moving the price a protocol trusts, so the agent compares that price against other sources: other oracles, exchange prices and the pool's own ratio. When the sources disagree by more than a set amount, the agent treats the feed as suspect.

The check has to run every block, because an oracle price that is wrong for one block is enough for an attacker to borrow against a false value. A price that is checked once a minute will miss an attack that starts and ends inside that minute.

When a feed is suspect, the agent can switch the protocol to a backup source or pause borrowing until a human checks. The pause costs the protocol some revenue, which is a small price next to a drain.

What happens when an agent flags a threat?

When an agent flags a threat, it follows a response plan that the team wrote in advance. The usual first step is an alert to the on-call team with the evidence attached: the transactions, the amounts and the pattern that matched.

For high-confidence threats, the plan may allow the agent to act on its own: pausing a function, blocking a new borrow or switching a price feed. The actions are narrow and reversible, so a wrong call costs little while a missed attack costs everything.

Every flag, action and human decision is logged. The log matters twice: it lets the team review whether the agent was right, and it gives auditors and users a record of what happened during an incident.

Teams building a new protocol can read DeFi development for the build side, including lending, staking and yield mechanics.

Where do DeFi security agents fall short?

DeFi security agents fall short where the attack is new. An agent trained on past exploits will recognise a re-entrancy drain, but it may miss a novel pattern that uses a mechanism nobody has exploited before. New attacks are, by definition, outside the training data.

Agents also struggle with judgement calls. A large withdrawal by a known team wallet looks like a drain to a simple rule, and an agent that pauses the protocol on every large move will annoy users and cost the team trust.

The fix is to keep a human in the loop for anything that is not a clear, high-confidence threat, and to update the agent's rules after every incident. An agent that never changes is an agent that gets beaten by the next new attack.

Example (hypothetical): a lending protocol stops a drain

Example (hypothetical): a lending protocol processes about 12,000 transactions a day. Its security agent flags 45 of them as suspicious, which is less than one in two hundred. A reviewer checks each flag, taking about 10 minutes per flag, so the daily review takes 45 x 10 = 450 minutes, or about 7.5 hours of staff time.

Of the 45 flags, 40 turn out to be normal activity: large but genuine borrows by known users. The other 5 are real: an attacker probing the borrow function with small loans before a larger attempt. The team pauses borrowing, patches the function and reopens it the same day.

The arithmetic is the point. The agent turns 12,000 transactions into 45 checks, and the checks turn into one stopped attack. Without the agent, the team would have found the probe only after the larger attempt, when the funds were already gone.

What are the common mistakes?

The most common mistakes are trusting the agent too much, training it on too little data and giving it actions it should not have.

  • No human in the loop. An agent that can pause the protocol without a human check will eventually pause it for a normal event.
  • Stale rules. Attack patterns change; rules written at launch and never updated stop catching new exploits.
  • Watching one contract only. Attacks are set up across contracts, so monitoring must cover the protocol and its neighbours.
  • Too many alerts. An agent that flags everything trains the team to ignore it. Tune the threshold until alerts are rare and worth reading.
  • No response plan. An alert without a written plan wastes the minutes that matter most.
  • Skipping the audit. An agent watches live behaviour; it does not find bugs in code that has not been audited.

For the broader view of securing AI systems, security for AI covers the controls that apply beyond DeFi.

A smart contract audit remains the first step before any protocol goes live.

Key takeaways

  • Agents watch contracts, pools and price feeds every block, which humans cannot do.
  • The agent's job is speed: flag the pattern, alert the team and pause the risky function.
  • Oracle checks must compare several sources, because one wrong block is enough for an attack.
  • New attacks are outside the training data, so a human must stay in the loop.
  • Rules need updating after every incident, or the agent falls behind the attackers.

Frequently asked questions

What is an AI agent in DeFi security?

An AI agent in DeFi security is software that watches a protocol's contracts, transactions and price feeds in real time, looking for patterns that precede an exploit. It alerts the team or triggers a pre-set response, such as pausing a function, while a human decides what happens next.

Can AI agents replace a smart contract audit?

No. An audit checks the code before launch for bugs and logic errors. An agent watches the live system for behaviour the audit could not predict, such as a new attack pattern or a market event. A protocol needs both, and the audit comes first.

What is a flash-loan attack?

A flash-loan attack borrows a large sum inside a single transaction, uses it to move a price or drain a pool, and repays the loan in the same transaction. Because the whole attack happens in one block, it needs an agent watching every block to catch it in time.

How do agents detect oracle manipulation?

The agent compares the price feed the protocol trusts against other sources, such as other oracles, exchange prices and the pool's own token ratio. When the sources disagree by more than a set amount, the agent treats the feed as suspect and can switch to a backup source.

Do DeFi security agents stop rug pulls?

They can flag the setup: a new pool, a large first trade and liquidity that is not locked. The agent warns the team and users before funds are added. It cannot stop an owner from removing liquidity once the pool exists, so the warning is the main protection.

Who should run a DeFi security agent?

Any team running a live protocol with real funds in it. The agent needs an on-call person to read its alerts and a written response plan for the threats it flags. Without that, the alerts pile up unread and the agent adds no safety.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: AI and agent systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a DeFi protocol and want agents watching it around the clock? Talk to Hertiv.

A senior engineer replies within one business day. Or see our DeFi Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call