How chrislernunes/llmreplay works
1. What this repo is
- llmreplay is a Python library for making LLM-driven systems deterministic by recording live interactions and replaying them later.
- The architecture is centered on separating LLM application logic from provider-specific integration, so the same app can run in record mode or replay mode.
2. Primary runtime boundary
- The main boundary is between your application and external LLM/tool services.
- In record mode, the library lets calls reach providers and captures traces.
- In replay mode, the library serves previously captured traces and avoids external network calls.
3. Core subsystem
- llmreplay/core/ contains the provider-agnostic engine.
- event.py suggests a trace/event model for capturing inputs, outputs, tool calls, and metadata.
- context.py likely carries per-run state for recording/replaying.
- store.py is the persistence boundary for saved traces.
- replay.py is the deterministic execution engine that matches current requests to stored events.
4. Provider integration layer
- llmreplay/hooks/ contains adapters for specific ecosystems: openai, anthropic, gemini, grok, and langchain.
- These hooks are the interception layer that turns live SDK/API calls into traceable events.
- This is where the repo touches external LLM vendors and higher-level orchestration frameworks, while keeping the replay core vendor-neutral.
5. Tool mocking boundary
- llmreplay/mocking/tools.py isolates tool-call simulation.
- This matters because agentic workflows are not just model text outputs; they also depend on side effects and tool invocation results.
- Tool mocking sits between replayed model behavior and the rest of the application, so tests can remain deterministic even when tools are normally dynamic.
6. Regression and verification layer
- regression.py and report.py suggest comparison and analysis utilities for replayed runs versus recorded baselines.
- These modules support snapshot-style testing and regression detection, turning traces into testable artifacts.
- The repository’s test suite (test_determinism.py, test_tools_regression.py, test_proof.py, etc.) reflects this verification-oriented design.
7. CLI and operational entry point
- llmreplay/cli.py provides a command-line interface for controlling record/replay workflows outside of application code.
- The package metadata in llmreplay.egg-info/entry_points.txt indicates there is an installable CLI entry point.
- This is the operational surface for developers running captures, replays, or checks in local dev and CI.
8. Optional web/UI surface
- web.py suggests a lightweight web component, likely for inspecting traces or replay sessions.
- Based on the rest of the repo, this is an auxiliary visualization/inspection boundary rather than the core execution path.
9. Data flow through the system
- The flow is: application code invokes an LLM SDK or framework → hook intercepts the call → core creates an event/context record → store persists it.
- In replay, the flow reverses: application invokes the same surface → replay engine looks up the matching trace → stored response/tool result is returned.
- The important architectural point is that the replay path must mirror the original call shape closely enough to preserve behavior.
10. Package structure and layering
- The repo is structured as a small Python package rather than a multi-service system.
- Public API is likely exposed from llmreplay/__init__.py, with core as the engine, hooks as integrations, mocking as test support, and cli/web/report as user-facing utilities.
- This layering keeps the deterministic core independent from provider SDK details and from presentation concerns.
11. Testing strategy
- The tests emphasize determinism, hook compatibility, adversarial cases, and tool regression.
- That indicates the architectural contract is not just “record data,” but “replay the same observable behavior across providers and agent workflows.”
- For an architecture diagram, the test suite is best represented as validation around the core and hook boundaries.
12. Key diagram takeaway
- The repo is best drawn as a deterministic replay engine in the center, with provider hooks on one side, trace storage underneath, and tool mocking / regression / CLI / web inspection as surrounding support layers.
- The central design goal is to decouple LLM application behavior from live provider nondeterminism while preserving enough structure to reproduce and verify runs.
Main entry points: replay.py Replay engine · hooks/__init__.py Hooks API · core/__init__.py Core API · regression.py Regression
Components 21
Core engine
Provider-agnostic record/replay machinery and trace model.
- Core API · Core package exports.
llmreplay/core/__init__.py - Event model · Represents captured LLM/tool interactions and metadata.
llmreplay/core/event.py - Run context · Carries per-run recording and replay state.
llmreplay/core/context.py - Trace store · Persists recorded traces and retrieved replay data.
llmreplay/core/store.py - Replay engine · Matches live requests to stored events and returns recorded results.
llmreplay/core/replay.py
Provider hooks
Adapters that intercept SDK/framework calls and translate them into replayable events.
- Hooks API · Exports provider and framework adapters.
llmreplay/hooks/__init__.py - OpenAI hook · Intercepts OpenAI SDK calls.
llmreplay/hooks/openai.py - Anthropic hook · Intercepts Anthropic SDK calls.
llmreplay/hooks/anthropic.py - Gemini hook · Intercepts Gemini SDK calls.
llmreplay/hooks/gemini.py - Grok hook · Intercepts Grok SDK calls.
llmreplay/hooks/grok.py - LangChain hook · Intercepts LangChain-driven model calls.
llmreplay/hooks/langchain.py
Support surfaces
Tool mocking, regression reporting, CLI, and web inspection.
- Tool mocking · Simulates tool calls so agent workflows remain deterministic.
llmreplay/mocking/tools.py - Regression · Compares replayed runs against recorded baselines.
llmreplay/regression.py - Report · Formats replay and regression results for inspection.
llmreplay/report.py - CLI · Command-line entry point for record/replay workflows.
llmreplay/cli.py - Web UI · Lightweight web surface for viewing traces or sessions.
llmreplay/web.py
Validation
Tests that exercise determinism, hooks, and regression behavior.
- Core tests · Exercises core replay and determinism behavior.
tests/test_core.py - Determinism tests · Checks stable replay behavior across runs.
tests/test_determinism.py - Hook tests · Validates provider and framework adapters.
tests/test_hooks.py - Tool regression · Checks tool-call mocking and regression behavior.
tests/test_tools_regression.py
Other components
- Package API · Top-level package exports and entry surface.
llmreplay/__init__.py
Connections 33
- Package API → Core API: exports · Public package exposes the core API.
- Package API → Hooks API: exports · Public package exposes hook adapters.
- Package API → CLI: entry point · Package installs a CLI surface.
- Core API → Event model: defines · Core API exposes trace/event primitives.
- Core API → Run context: defines · Core API exposes runtime context state.
- Core API → Trace store: defines · Core API exposes persistence boundary.
- Core API → Replay engine: defines · Core API exposes replay execution.
- Replay engine → Event model: consumes · Replay matches against stored events.
- Replay engine → Run context: uses · Replay runs within per-session context.
- Replay engine → Trace store: loads · Replay reads captured traces from storage.
- Trace store → Event model: persists · Trace store saves and retrieves event records.
- Hooks API → OpenAI hook: adapts · Hook package exposes provider adapter.
- Hooks API → Anthropic hook: adapts · Hook package exposes provider adapter.
- Hooks API → Gemini hook: adapts · Hook package exposes provider adapter.
- Hooks API → Grok hook: adapts · Hook package exposes provider adapter.
- Hooks API → LangChain hook: adapts · Hook package exposes framework adapter.
- OpenAI hook → Replay engine: records/replays · Provider calls are routed through the replay engine.
- Anthropic hook → Replay engine: records/replays · Provider calls are routed through the replay engine.
- Gemini hook → Replay engine: records/replays · Provider calls are routed through the replay engine.
- Grok hook → Replay engine: records/replays · Provider calls are routed through the replay engine.
- LangChain hook → Replay engine: records/replays · Framework calls are routed through the replay engine.
- Replay engine → Tool mocking: simulates · Replay can substitute deterministic tool results.
- Regression → Replay engine: compares · Regression checks use replayed executions.
- Regression → Report: summarizes · Comparison results are formatted into reports.
- CLI → Replay engine: drives · CLI orchestrates record and replay workflows.
- CLI → Regression: runs · CLI can invoke regression checks.
- Web UI → Report: renders · Web surface likely presents reports or trace views.
- Core tests → Replay engine: verifies · Core tests exercise deterministic replay behavior.
- Determinism tests → Replay engine: verifies · Determinism tests validate stable outputs.
- Hook tests → OpenAI hook: verifies · Hook tests cover provider adapters.
- Hook tests → LangChain hook: verifies · Hook tests cover framework adapters.
- Tool regression → Tool mocking: verifies · Tool regression tests cover deterministic tool mocking.
- Tool regression → Regression: verifies · Tool regression tests cover comparison behavior.