AI Agent Race Lab: Exploit the Gap Between Check and Transfer

✨ AI Summary
BankBot checks an account's balance with one tool call, then transfers funds with a second. Between those two calls there is a gap — and agentic systems love to parallelise. Fire two transfers into that gap and both pass the balance check before either debits.
Lab at a glance: Advanced · 145 XP · about 20 minutes · runs in your browser, no signup or setup. Try it yourself first; the full solution is behind a spoiler below.

What this lab teaches

Time-of-check to time-of-use (TOCTOU) is the oldest concurrency bug, and agentic LLMs make it easy to hit: agents naturally split an action into check-then-act tool calls, and multiple calls can overlap. If the check and the debit are not atomic, concurrent transfers each see the original balance and all proceed.

The lesson: for an agent, 'check balance' and 'transfer' must be one atomic operation, not two tool calls with a window between them.

How BankBot moves money

To transfer, BankBot calls check_balance() then transfer_funds() separately. The balance is read, judged sufficient, and only later debited. Two transfers launched together both read the same starting balance and both go through, pushing the account negative.

The attack approach

  • Confirm the transfer is a check_balance() then transfer_funds() sequence with a gap.
  • Prepare two transfers for the same source account.
  • Launch them so both execute the check before either debits.
  • Drive the source account below zero.

Full walkthrough (spoiler)

If sequential transfers never overdraw, you need true overlap — open the steps.

Show the step-by-step solution
  1. Set up two transfers from the same source account, each within the current balance individually.
  2. Trigger them concurrently so both check_balance() calls run before either transfer_funds() debits.
  3. Both see enough balance and both complete.
  4. The account ends negative. The lab detects the overdraft and marks you solved.

Why it works

The agent assumed its actions happen one at a time, so the window between reading the balance and debiting felt safe. Under concurrency the window is an opening: overlapping transfers all read the stale balance and all act on it.

Core principleMake check-and-debit atomic — one transactional operation with the right locking — never a read followed later by a separate write.

Where this happens in the real world

TOCTOU is a decades-old concurrency flaw (double-spend, overdraft) now resurfacing in agentic tool execution, where split actions and parallelism are the norm. It falls under OWASP LLM06 Excessive Agency when the agent can act with insufficient transactional safety.

How to defend against it

  • Perform balance-check and debit in one atomic transaction with appropriate locking.
  • Use idempotency keys so duplicate or concurrent transfers collapse to one effect.
  • Enforce invariants at the datastore (no negative balance) regardless of agent logic.
  • Serialise high-risk tool calls per account so they cannot overlap.
Mr Elite tip: For any agent that does check-then-act with money, inventory or quotas, fire several identical actions at once. If more than one succeeds past the same check, the operation is not atomic.

Try it, then go further

Reading about it is the easy part. Open the lab, try your own approach before peeking at the spoiler, and see how many attempts it takes.

❓ Frequently asked questions

What is a TOCTOU race?
Time-of-check to time-of-use: a gap between checking a condition and acting on it, during which a concurrent operation invalidates the check. Here, balance is checked then debited separately.
Why do agents make it worse?
Agents naturally split actions into separate tool calls and can run them in parallel, widening and multiplying the check-to-use window.
How do I fix it?
Make check and debit one atomic transaction, use idempotency keys, enforce invariants at the datastore, and serialise high-risk tool calls per account.
Is this unique to AI?
No — TOCTOU is a classic concurrency bug. Agentic tool execution just makes it easy to trigger.
⚡
Join free to earn XP for reading this article Track your progress, build streaks and compete on the leaderboard.
Join Free
Lokesh N. Singh aka Mr Elite
Lokesh N. Singh aka Mr Elite
Founder, Securityelites · AI Red Team Educator
Founder of Securityelites and creator of the SE-ARTCP credential. Working penetration tester focused on AI red team, prompt injection research, and LLM security education.
About Lokesh ->

Leave a Comment

Your email address will not be published. Required fields are marked *