Skip to main content
AgentDiff Logo

AgentDiff

Catch silent cost surges and broken agent loops before they ship.

CI Build PyPI version Python Versions Code Style: Ruff License: MIT Website

pip install agent-trajectory-diff

Website & Interactive Docs · Cookbooks · Live Demo Repo · Changelog

AgentDiff is a developer-first Python library, pytest plugin, and CLI for regression testing multi-turn, tool-using AI agents by comparing execution paths (trajectories) head-to-head.

When you change a prompt, tweak a system instruction, or upgrade an LLM, traditional assertions only verify that the final string matches. They miss the silent failures: the agent took 5 extra tool calls, burned 3× the tokens, entered an infinite retry loop, or drifted from the verified execution path.

AgentDiff aligns candidate execution DAGs against committed golden baselines in <10ms without calling any paid LLM judges.

Highlights

  • Deterministic Graph Diffing: Topological DAG alignment and Longest Common Subsequence (LCS) step comparison in <10ms with zero paid LLM-judge calls.
  • Statistical Baselines & Variance Bands: Capture N-run envelopes (record --runs 3) so non-deterministic agents don't flake CI on harmless jitter.
  • Zero-Config Setup (agentdiff init): Auto-detects LangGraph, CrewAI, OpenAI Agents SDK, or OpenTelemetry and writes agentdiff.toml + CI workflow in seconds.
  • In-PR Interactive Blessings (/agentdiff approve): Reviewers bless intended trajectory improvements from PR comments as agentdiff-ci[bot].
  • 100% Local & Air-Gapped: Zero telemetry, no cloud accounts, no outbound network calls during diffs. Raw prompts and tool outputs stay local.
  • Drop-in CI Merge Gate: Native exit codes (0 pass / 1 regression fail) and automated GitHub Action PR comments with human-first verdicts.
  • Universal Telemetry Adapters: Seamlessly diff traces from LangGraph, CrewAI, OpenAI Agents SDK, Langfuse, LangSmith, OpenInference / OpenTelemetry, or generic JSON.

Quickstart

1. Initialize with agentdiff init

Auto-detect your agent framework and generate your configuration + CI workflow:

agentdiff init --scenario customer_support --runs 3 --with-approve

2. Record a Statistical Baseline Envelope

Record an N-run baseline envelope from any agent function without writing boilerplate telemetry:

agentdiff record my_agent:run \
  --input '{"query": "summarize repo"}' \
  --runs 3 \
  --out baselines/customer_support.envelope.json

3. Compare Traces in CLI

Compare candidate runs against your baseline envelope:

agentdiff diff baselines/customer_support.envelope.json traces/candidate.json --fail-on-regression

4. Pytest Regression Testing

Enforce trajectory parity directly in your test suite:

import pytest
from agentdiff import load_trace, compare
from agentdiff.testing import assert_no_regressions

def test_agent_refactor_efficiency():
    # Load traces (auto-detects telemetry source format)
    baseline = load_trace("tests/baselines/golden.json")
    candidate = load_trace("tests/traces/candidate.json")

    # Run sub-10ms deterministic comparison
    report = compare(baseline, candidate)

    # Assert no structural drift, cost surges, or tool loops
    assert_no_regressions(
        report,
        max_divergence=0.25,        # Max Trajectory Divergence Index [0.0 - 1.0]
        max_cost_increase_pct=5.0,  # Max 5% token cost increase
        allow_loops=False,          # Reject repetitive tool call cycles
        max_wasted_effort=0.10,     # Max 10% error/retry/abandoned steps
        max_recovery_step_ratio=1.5 # Max recovery steps relative to baseline
    )

GitHub Actions CI Gate

Block broken agent PRs before they land in production using the official composite action:

name: AgentDiff Regression Gate

on:
  pull_request:

permissions:
  contents: read
  pull-requests: write   # Allows posting automated root-cause PR comments

jobs:
  agent-regression-gate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - uses: kerrshift/agentdiff/.github/actions/agentdiff-check@v0.5.0
        with:
          baseline: baselines/customer_support.envelope.json
          candidate: traces/pr_candidate.json
          pr: ${{ github.event.pull_request.number }}
          github-token: ${{ secrets.GITHUB_TOKEN }}

When a regression occurs, the gate fails with exit code 1 and comments on the PR with culprit identification and a collapsed divergence tree:

### AgentDiff Gate: REGRESSION DETECTED

| Metric | Baseline | Candidate | Threshold | Status |
| :--- | :--- | :--- | :--- | :--- |
| **Divergence (TDI)** | 0.00 | 0.42 | ≤ 0.25 | FAIL |
| **Cost Surge** | $0.0042 | $0.0138 (+228%) | ≤ +5.0% | FAIL |
| **Loops (LBI)** | 0 | 3 loops | 0 | FAIL |
| **Wasted Effort (WEI)**| 0.00 | 0.38 | ≤ 0.10 | FAIL |

**Culprit Step:** Step 4 `execute_sql` entered a 3× retry loop after schema refactor.

Core Metric Mathematics

Metric Target / Range Algorithmic Definition Description
Trajectory Divergence Index (TDI) 0.0 (Identical) to 1.0 (Divergent) $$1.0 - \frac{2 \times \vert{}\text{LCS}(\text{Steps}_A, \text{Steps}_B)\vert{}}{\vert{}\text{Steps}_A\vert{} + \vert{}\text{Steps}_B\vert{}}$$ Structural distance between baseline and candidate execution DAGs using Longest Common Subsequence.
Wasted Effort Index (WEI) 0.0 (Optimal) to 1.0 (Total Waste) $$\frac{\text{Count}(\text{Steps} \in {\text{ERROR, RETRY, ABANDONED}})}{\text{Total Steps}}$$ Fraction of execution steps spent in failed, retried, or aborted tool operations.
Loop Buster Index (LBI) Integer ($\ge 0$) Stagnant State Cycle Detection Counts repeating consecutive tool call patterns where inputs/outputs show no state progression.
Recovery Step Ratio (RSR) 1.0 = Parity; $> 1.0$ = Slower Recovery $$\text{RSR} = \frac{\text{Recovery Steps}{\text{candidate}}}{\text{Recovery Steps}{\text{baseline}}}$$ Measures the number of steps required to return to the verified golden trajectory path after encountering an error.
Resource Deltas ($\Delta\text{Res}$) Percentage ($\pm%$) $\frac{\text{Val}{\text{candidate}} - \text{Val}{\text{baseline}}}{\text{Val}_{\text{baseline}}} \times 100$ Exact percentage deltas for $\Delta\text{Tokens}$, $\Delta\text{Cost}$, and $\Delta\text{Latency}$.

Supported Telemetry Formats

Telemetry Framework / Format Adapter Spec Ingestion Guide
LangGraph / LangChain --adapter langgraph cookbooks/langgraph
CrewAI --adapter crewai cookbooks/crewai
OpenAI Agents SDK --adapter openai_agents cookbooks/openai_agents
Langfuse --adapter langfuse cookbooks/langfuse
LangSmith --adapter langsmith cookbooks/langsmith
OpenInference / OpenTelemetry --adapter openinference cookbooks/openinference
Generic JSON Schema --adapter generic schema/v0.1.0/trace.json

Local-First Privacy Guarantee

Agent trajectories often contain proprietary prompts, sensitive tool payloads, and customer data. AgentDiff is engineered with strict local-first principles:

  • Zero Outbound Network Traffic: Parsing, DAG diffing, metric calculations, and reporting run 100% locally.
  • Air-Gapped & Firewall Friendly: Run tests on laptops, in air-gapped VPCs, or under strict enterprise egress policies.
  • Repo-Committed Baselines: Your golden trajectories live in Git next to the code they protect.
  • No Third-Party APM Lock-In: Switch tracing providers at any time; AgentDiff normalizes all schemas to a unified specification.

Documentation & Cookbooks

Development

This repository uses uv for lightning-fast environment and dependency management.

# Clone the repository
git clone https://github.com/kerrshift/agentdiff.git
cd agentdiff

# Install dependencies and sync virtualenv
uv sync

# Run linting and code formatting checks
make lint

# Run the test suite
make test

# Build package distributions
make build

# Website & docs (separate repo, deploys agentdiff.app)
# → github.com/kerrshift/agentdiff-website

License

Distributed under the MIT License. See LICENSE for more information.

Release files for agent-trajectory-diff 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agent-trajectory-diff 0.5.1
File Size Uploaded
agent_trajectory_diff-0.5.1.tar.gz 70.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agent-trajectory-diff 0.5.1
File Interpreter ABI Platform
agent_trajectory_diff-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 163.3 kB

Release files / agent_trajectory_diff-0.5.1.tar.gz

Download URL agent_trajectory_diff-0.5.1.tar.gz
Size 70.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a75247717df94f30df9609f7639627ab98f92899b557fa3a4b3c8053931506b0
BLAKE2b-256 checksum
How to use checksums
219b2aabe341dbf158d3f910de165c9d996e25ad6a17d6b64c7f83e6adac4291
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release files / agent_trajectory_diff-0.5.1-py3-none-any.whl

Download URL agent_trajectory_diff-0.5.1-py3-none-any.whl
Size 93.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61b64e12dc76bad3a3e405023459c7cbba458c2a15559eb2aa2d4594c224a153
BLAKE2b-256 checksum
How to use checksums
d80bc5299f795db814b53de4059fe848a27217d0e2c087a33c2cd38bb0b9758f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page