# chrislernunes/llmreplay architecture

> Architecture diagram of the GitHub repository chrislernunes/llmreplay, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chrislernunes/llmreplay
- Repository: https://github.com/chrislernunes/llmreplay

## Overview

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: [`llmreplay/core/replay.py`](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/replay.py) (Replay engine), [`llmreplay/hooks/__init__.py`](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/__init__.py) (Hooks API), [`llmreplay/core/__init__.py`](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/__init__.py) (Core API), [`llmreplay/regression.py`](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/regression.py) (Regression)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_core["Core engine"]
  node_n_core_init["Core API<br/>[__init__.py]"]
  node_n_event["Event model<br/>trace schema<br/>[event.py]"]
  node_n_context["Run context<br/>runtime state<br/>[context.py]"]
  node_n_store[("Trace store<br/>persistence<br/>[store.py]")]
  node_n_replay{{"Replay engine<br/>deterministic core<br/>[replay.py]"}}
end

subgraph group_g_hooks["Provider hooks"]
  node_n_hooks_init["Hooks API<br/>integration api<br/>[__init__.py]"]
  node_n_openai["OpenAI hook<br/>provider adapter<br/>[openai.py]"]
  node_n_anthropic["Anthropic hook<br/>provider adapter<br/>[anthropic.py]"]
  node_n_gemini["Gemini hook<br/>provider adapter<br/>[gemini.py]"]
  node_n_grok["Grok hook<br/>provider adapter<br/>[grok.py]"]
  node_n_langchain["LangChain hook<br/>framework adapter<br/>[langchain.py]"]
end

subgraph group_g_support["Support surfaces"]
  node_n_tools["Tool mocking<br/>tool simulator<br/>[tools.py]"]
  node_n_regression["Regression<br/>comparison<br/>[regression.py]"]
  node_n_report["Report<br/>analysis<br/>[report.py]"]
  node_n_cli["CLI<br/>command entry<br/>[cli.py]"]
  node_n_web["Web UI<br/>inspection ui<br/>[web.py]"]
end

subgraph group_g_tests["Validation"]
  node_n_test_core["Core tests<br/>test suite<br/>[test_core.py]"]
  node_n_test_det["Determinism tests<br/>test suite"]
  node_n_test_hooks["Hook tests<br/>test suite<br/>[test_hooks.py]"]
  node_n_test_tools["Tool regression<br/>test suite"]
end

node_n_init["Package API<br/>public api<br/>[__init__.py]"]

node_n_init -->|"exports"| node_n_core_init
node_n_init -->|"exports"| node_n_hooks_init
node_n_init -->|"entry point"| node_n_cli
node_n_core_init -->|"defines"| node_n_event
node_n_core_init -->|"defines"| node_n_context
node_n_core_init -->|"defines"| node_n_store
node_n_core_init -->|"defines"| node_n_replay
node_n_replay -->|"consumes"| node_n_event
node_n_replay -->|"uses"| node_n_context
node_n_replay -->|"loads"| node_n_store
node_n_store -->|"persists"| node_n_event
node_n_hooks_init -->|"adapts"| node_n_openai
node_n_hooks_init -->|"adapts"| node_n_anthropic
node_n_hooks_init -->|"adapts"| node_n_gemini
node_n_hooks_init -->|"adapts"| node_n_grok
node_n_hooks_init -->|"adapts"| node_n_langchain
node_n_openai -->|"records/replays"| node_n_replay
node_n_anthropic -->|"records/replays"| node_n_replay
node_n_gemini -->|"records/replays"| node_n_replay
node_n_grok -->|"records/replays"| node_n_replay
node_n_langchain -->|"records/replays"| node_n_replay
node_n_replay -->|"simulates"| node_n_tools
node_n_regression -->|"compares"| node_n_replay
node_n_regression -->|"summarizes"| node_n_report
node_n_cli -->|"drives"| node_n_replay
node_n_cli -->|"runs"| node_n_regression
node_n_web -->|"renders"| node_n_report
node_n_test_core -->|"verifies"| node_n_replay
node_n_test_det -->|"verifies"| node_n_replay
node_n_test_hooks -->|"verifies"| node_n_openai
node_n_test_hooks -->|"verifies"| node_n_langchain
node_n_test_tools -->|"verifies"| node_n_tools
node_n_test_tools -->|"verifies"| node_n_regression

click node_n_init "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/__init__.py"
click node_n_core_init "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/__init__.py"
click node_n_event "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/event.py"
click node_n_context "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/context.py"
click node_n_store "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/store.py"
click node_n_replay "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/replay.py"
click node_n_hooks_init "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/__init__.py"
click node_n_openai "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/openai.py"
click node_n_anthropic "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/anthropic.py"
click node_n_gemini "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/gemini.py"
click node_n_grok "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/grok.py"
click node_n_langchain "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/langchain.py"
click node_n_tools "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/mocking/tools.py"
click node_n_regression "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/regression.py"
click node_n_report "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/report.py"
click node_n_cli "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/cli.py"
click node_n_web "https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/web.py"
click node_n_test_core "https://github.com/chrislernunes/llmreplay/blob/main/tests/test_core.py"
click node_n_test_det "https://github.com/chrislernunes/llmreplay/blob/main/tests/test_determinism.py"
click node_n_test_hooks "https://github.com/chrislernunes/llmreplay/blob/main/tests/test_hooks.py"
click node_n_test_tools "https://github.com/chrislernunes/llmreplay/blob/main/tests/test_tools_regression.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_core_init,node_n_event,node_n_context,node_n_store,node_n_replay toneBlue
class node_n_hooks_init,node_n_openai,node_n_anthropic,node_n_gemini,node_n_grok,node_n_langchain toneAmber
class node_n_tools,node_n_regression,node_n_report,node_n_cli,node_n_web toneMint
class node_n_test_core,node_n_test_det,node_n_test_hooks,node_n_test_tools toneRose
class node_n_init toneNeutral
```

## Components

### Core engine

Provider-agnostic record/replay machinery and trace model.

- [Core API](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/__init__.py): core api · Core package exports. · `llmreplay/core/__init__.py`
- [Event model](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/event.py): trace schema · Represents captured LLM/tool interactions and metadata. · `llmreplay/core/event.py`
- [Run context](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/context.py): runtime state · Carries per-run recording and replay state. · `llmreplay/core/context.py`
- [Trace store](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/store.py): persistence · Persists recorded traces and retrieved replay data. · `llmreplay/core/store.py`
- [Replay engine](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/core/replay.py): deterministic core · 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](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/__init__.py): integration api · Exports provider and framework adapters. · `llmreplay/hooks/__init__.py`
- [OpenAI hook](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/openai.py): provider adapter · Intercepts OpenAI SDK calls. · `llmreplay/hooks/openai.py`
- [Anthropic hook](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/anthropic.py): provider adapter · Intercepts Anthropic SDK calls. · `llmreplay/hooks/anthropic.py`
- [Gemini hook](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/gemini.py): provider adapter · Intercepts Gemini SDK calls. · `llmreplay/hooks/gemini.py`
- [Grok hook](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/grok.py): provider adapter · Intercepts Grok SDK calls. · `llmreplay/hooks/grok.py`
- [LangChain hook](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/hooks/langchain.py): framework adapter · Intercepts LangChain-driven model calls. · `llmreplay/hooks/langchain.py`

### Support surfaces

Tool mocking, regression reporting, CLI, and web inspection.

- [Tool mocking](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/mocking/tools.py): tool simulator · Simulates tool calls so agent workflows remain deterministic. · `llmreplay/mocking/tools.py`
- [Regression](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/regression.py): comparison · Compares replayed runs against recorded baselines. · `llmreplay/regression.py`
- [Report](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/report.py): analysis · Formats replay and regression results for inspection. · `llmreplay/report.py`
- [CLI](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/cli.py): command entry · Command-line entry point for record/replay workflows. · `llmreplay/cli.py`
- [Web UI](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/web.py): inspection ui · Lightweight web surface for viewing traces or sessions. · `llmreplay/web.py`

### Validation

Tests that exercise determinism, hooks, and regression behavior.

- [Core tests](https://github.com/chrislernunes/llmreplay/blob/main/tests/test_core.py): test suite · Exercises core replay and determinism behavior. · `tests/test_core.py`
- [Determinism tests](https://github.com/chrislernunes/llmreplay/blob/main/tests/test_determinism.py): test suite · Checks stable replay behavior across runs. · `tests/test_determinism.py`
- [Hook tests](https://github.com/chrislernunes/llmreplay/blob/main/tests/test_hooks.py): test suite · Validates provider and framework adapters. · `tests/test_hooks.py`
- [Tool regression](https://github.com/chrislernunes/llmreplay/blob/main/tests/test_tools_regression.py): test suite · Checks tool-call mocking and regression behavior. · `tests/test_tools_regression.py`

### Other components

- [Package API](https://github.com/chrislernunes/llmreplay/blob/main/llmreplay/__init__.py): public api · Top-level package exports and entry surface. · `llmreplay/__init__.py`

## Connections

- 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.
