Skip to main content

Record & replay LLM API calls for deterministic agent tests.

Project description

agent-replay (Python)

Record & replay LLM API calls for deterministic agent tests.

agent-replay captures the HTTP traffic your agent makes to LLM providers (Anthropic, OpenAI, …) into a human-readable JSONL "cassette", then plays it back on subsequent runs so your tests are fast, offline, and deterministic — while still flagging when a prompt has drifted away from what was recorded.

Install

pip install agent-replay-py

The install name is agent-replay-py; the import name is agentreplay (there is an unrelated agentreplay project on PyPI, so we distinguish the distribution name).

Optional extras:

pip install "agent-replay-py[pytest]"   # pytest fixture & marker

Quick start

import agentreplay as ar

with ar.cassette("tests/cassettes/hello.jsonl", mode="auto") as c:
    client = c.httpx_client()   # a preconfigured httpx.Client
    r = client.post(
        "https://api.anthropic.com/v1/messages",
        json={"model": "claude-3", "messages": [{"role": "user", "content": "hi"}]},
    )
    print(r.json())
  • First run: the cassette file doesn't exist yet → records the real response.
  • Later runs: the file exists → serves the recorded response, no network.
  • mode accepts "record" | "replay" | "auto" | "passthrough", and can be overridden globally via AGENTREPLAY_MODE.

Async is the same shape:

async with ar.cassette("tests/cassettes/hello.jsonl") as c:
    async with c.httpx_async_client() as client:
        r = await client.post(...)

pytest

agent-replay ships a pytest plugin. Cassettes are auto-named from the test node id and stored under tests/cassettes/.

import pytest

@pytest.mark.agentreplay(mode="auto")
def test_agent(agentreplay):
    client = agentreplay.httpx_client()
    ...

CLI

agentreplay inspect tests/cassettes/hello.jsonl   # header + summary
agentreplay show    tests/cassettes/hello.jsonl 0 # pretty-print seq 0
agentreplay verify  tests/cassettes/hello.jsonl   # re-check fingerprints

Divergence

When the request your code sends no longer matches what was recorded (a prompt changed, a tool schema was edited, …), agent-replay produces a structured diff (ar.Report) instead of silently returning stale data. The divergence policy ("warn" | "error") controls whether it raises.

License

MIT — see LICENSE.

The TypeScript sibling of this package lives at @retr0hxx/agent-replay.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent_replay_py-0.1.0.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent_replay_py-0.1.0-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file agent_replay_py-0.1.0.tar.gz.

File metadata

  • Download URL: agent_replay_py-0.1.0.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for agent_replay_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 af4eda26624ba32297da6e39b64867e9c8fef8c7057cb9085e367f28ecb1d021
MD5 997b9d0e6e9ce54918e75b6560546a69
BLAKE2b-256 d14385ca3d0634945977b7c392a699ff5e56f67762573b9b531e57fb82a3ad21

See more details on using hashes here.

File details

Details for the file agent_replay_py-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_replay_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 542c1b2f0229aebb37db79896efa7105816486c0856fcf6d59efc1846dc07d4b
MD5 38db40f63ca99f9c7b7b8211196676c2
BLAKE2b-256 d7484a5623d6fc3ce52165a89f24ddd55b0afbf56eb22587cd29609c1fa441df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page