DEV Community

Cover image for Introducing DriftCatch — behavioural testing Framework for AI agents
Neel Deshmane
Neel Deshmane

Posted on

Introducing DriftCatch — behavioural testing Framework for AI agents

An AI agent can return exactly the answer you expect while its decisions, tool usage and execution path have silently changed.

A prompt tweak, a model update or a tool-schema edit can all introduce a behavioural regression that output-based testing will never catch. The answer still reads correctly, so every eval passes.

Consider a change like this to a refund agent's system prompt:

  • Resolve the customer's issue.
  • Resolve the customer's issue carefully. One word. The replies still read perfectly well. But the agent may now escalate cases it used to refund — and if your assertions are about the answer, nothing catches it.

So I built DriftCatch.

pip install driftcatch
import driftcatch as drift

def test_refund():
with drift.take("refund_1182") as take:
my_agent("refund order 1182")

take.assert_tools().called_in_order("lookup_order", "issue_refund")
take.assert_invariants().hold()
Enter fullscreen mode Exit fullscreen mode

First run records. Every run after replays from disk — no network, no tokens, deterministic. It hooks httpx, so OpenAI, Anthropic, LangChain, LlamaIndex or your own loop all work with no adapter.

The part I think is actually new
Record/replay for agents already exists. What I hadn't seen is mining the rules out of the runs you've approved:

$ drift learn
Mined 13 invariant(s) from 23 approved recording(s)

lookup_order always runs before check_eligibility 23/23
issue_refund is always the last tool 18/18
escalate_to_human is offered but never called 23/23
issue_refund.args.order_id always equals
lookup_order.args.order_id 18/18
Nobody wrote those. The last one — the order you look up is the order you refund — is a real bug class nobody tests for. It breaks when someone reuses a loop variable.

This is specification mining, which Daikon has done for ordinary programs since 2001. Agent trajectories turn out to fit it well.

The hard part isn't mining, it's not mining garbage. Four filters: minimum support of 5 runs, a confidence gate of 1 - 0.5^support, the value has to actually vary across runs (two constants that match aren't a relationship), and transitive rules get dropped. That took 19 mined rules down to 15.

Rules are proposed as pending. A human enforces them, or doesn't.

When it fails, it names the cause
$ drift bisect refund_1182 --previous

golden: lookup_order -> check_eligibility -> issue_refund
candidate: lookup_order -> check_eligibility -> escalate_to_human

ROOT CAUSE: the system prompt.

- issue.
+ issue carefully.
Enter fullscreen mode Exit fullscreen mode

Four inputs are fingerprinted separately — system prompt, model id, tool schemas, matcher config — so drift gets attributed instead of shrugged at.

The rest
Behaviour coverage — which tools, paths and error branches you've actually tested, plus suggested scenarios for the gaps
What-if — swap a recorded event at replay time to drive failure and retry paths, without recording anything new
Determinism scoring — run N times live, group by trajectory, get 80% path A / 20% path B instead of a meaningless "4/5 passed"
Tool contracts — declared schemas vs real use. On the demo corpus it flagged a parameter sent in every single request that no model ever used.
Approval gate — recordings start PENDING. A recorded run can be wrong; if "recorded" meant "correct", your first bad run becomes the enforced contract.
CLI, pytest plugin, local UI, and a read-only MCP server for Cursor / Claude Code
One honest limitation
Replay cannot catch the model changing underneath you. A recording holds what the model said then, not what it would say now. That's physics, not a missing feature — which is why there's a separate live mode that makes real calls.

Any tool claiming to catch model drift from a cached response is claiming something impossible.

GitHub logo NEEL7127 / driftcatch

Catch the moment your AI agent's behaviour changes. Record once, replay deterministically, fail the build on drift.

DriftCatch

Learns what your AI agent always does — then tells you the moment it stops.

PyPI CI python tests license

A passing replay, then a regression caught and its cause named.

0.1 pre-release. The engine, CLI, pytest plugin, local UI and MCP server are implemented and tested against the real OpenAI and Anthropic SDKs — 407 tests, mypy --strict clean. Not yet on PyPI, and not yet used by anyone but its author. If you try it and something is wrong, an issue is more useful to me than a star.


The problem

You tweak one word in a prompt. Your agent quietly stops querying the database and starts web-searching. The answer still looks right, so every eval passes. You find out from a customer.

Or worse — the trajectory is identical, every tool fires in the right order, and the refund amount is wrong by a factor of a hundred.

Agents don't fail loudly. They drift.

30 seconds

import driftcatch as drift
@drift
…
Enter fullscreen mode Exit fullscreen mode

Top comments (0)