Python SDK for ReplayAI — instrument agents, record sessions, replay in the dashboard.
Project description
replayai — Python SDK
Instrument Python agents, record every step (LLM calls, tool calls, retrievals, errors), and view sessions in the built-in dashboard where you can replay them, diff runs, and export tests.
- Stdlib only —
pip install replayai-sdkbrings no dependencies. - Built-in dashboard —
replayai uilaunches a self-contained dashboard server. No external app or database required. - Decorator + context manager —
@trace(...)orwith trace(...) as ctx:. - Framework extras —
pip install "replayai-sdk[langchain]"for auto-instrumentation.
Install
pip install replayai-sdk
# or, with the LangChain integration:
pip install "replayai-sdk[langchain]"
Windows note: If you see a warning like "The script replayai.exe is installed in … which is not on PATH", either add that directory to your PATH or use
python -m replayaiinstead — it works identically:python -m replayai ui # same as: replayai ui python -m replayai record agent.py
30-second usage
from replayai import trace, record_step
@trace("support-agent-v3", project="support-agent", tags=["production"])
def handle_support_ticket(message: str) -> str:
record_step(
type="llm_call", name="classify_intent",
model="gpt-4o-mini", tokens_in=312, tokens_out=24,
input=f"User: {message}", output="intent: billing_dispute",
status="success",
)
record_step(
type="tool_call", name="issue_refund",
input='{"charge_id":"ch_002"}',
output='{"refund_id":"ref_3391"}',
status="success",
)
return "Refund issued (ref_3391)."
handle_support_ticket("I was charged twice, refund me.")
Launching the dashboard
The SDK ships with a self-contained dashboard server that mirrors the ReplayAI website's Live Demo design. Record sessions locally, then launch the UI to view them:
# 1. Record a session (stored locally in ./ReplayAI/sessions/ by default)
replayai record my_agent.py --name "my-agent" --tags "prod"
# 2. Launch the dashboard
replayai ui
# → opens http://localhost:7373 in your browser
# → shows all locally-recorded sessions with full step timelines
The dashboard features:
- 6 stat cards (Sessions, Failed, Steps, Cost, Fail Rate, Avg Run)
- Sessions sidebar with status dots, duration, cost, step count, relative time
- Replay timeline with a clickable scrubber bar + step controls (restart, prev, next, last)
- Step detail with type badge, model, duration, tokens, offset, and input/output fields
- Auto-refresh every 5 seconds (new sessions appear instantly)
- Window chrome with traffic lights + breadcrumbs (matches the website design)
Options:
| Flag | Default | Description |
|---|---|---|
--port |
7373 |
Port to listen on |
--storage |
./ReplayAI |
Local storage path (sessions saved to {storage}/sessions/) |
--no-browser |
— | Don't auto-open the browser |
The dashboard reads sessions from {storage}/sessions/*.json and serves:
GET /— single-page dashboard UI (stats, sessions list, step timeline)GET /api/sessions— JSON session listGET /api/sessions/:id— JSON single session with stepsGET /api/stats— JSON aggregate stats
CLI commands
replayai record <script.py> [--project <slug>] [--tags a,b] # Run a script under a trace
replayai test [tests/replay/] [--live-llm] # Run replay regression tests
replayai ui [--port 7373] [--storage ./replays] # Launch the dashboard
replayai --version # Print version
Configuration
Environment variables (all optional):
| Variable | Default | Description |
|---|---|---|
REPLAYAI_PROJECT |
— | Default project slug/id |
REPLAYAI_TOKEN |
— | Cloud API token |
REPLAYAI_STORAGE |
cloud |
cloud, local, or both |
REPLAYAI_STORAGE_PATH |
./ReplayAI |
Local storage directory (used when storage includes local) |
REPLAYAI_API_URL |
http://localhost:3000 |
Cloud API base URL |
REPLAYAI_DASHBOARD_URL |
http://localhost:3000 |
Dashboard base URL |
REPLAYAI_SAMPLE_RATE |
1.0 |
Fraction of sessions to record |
REPLAYAI_STRICT |
false |
Raise on recording failures |
REPLAYAI_REDACT_PATTERNS |
built-in set | Comma-separated regexes |
REPLAYAI_REDACT_STRICT |
true |
Set false to disable entropy-based secret detection |
REPLAYAI_TIMEOUT |
30 |
Per-request HTTP timeout (seconds) |
REPLAYAI_MAX_STEPS |
200 |
Hard ceiling on steps persisted per session |
REPLAYAI_COST_RATES_URL |
— | URL to fetch current model pricing (JSON: {"model": {"in": float, "out": float}}). Falls back to built-in rates on failure. |
Programmatic override:
import replayai
replayai.configure(project="support-agent", api_url="http://localhost:3000")
replayai.set_strict_mode(True) # opt into hard failures (was: replayai.strict_mode = True)
Storage modes
The SDK supports three storage modes via REPLAYAI_STORAGE (or configure(storage=...)):
| Mode | Behavior | Use case |
|---|---|---|
cloud (default) |
POSTs sessions to the ReplayAI API at REPLAYAI_API_URL. |
Production, shared dashboards, team collaboration. |
local |
Saves sessions as JSON files to ./ReplayAI/sessions/. No network calls. |
Offline development, CI, local debugging with replayai ui. |
both |
Saves locally AND POSTs to the API. | Hybrid: local backup + cloud visibility. |
Offline mode (no API needed):
# Record locally (no API running)
REPLAYAI_STORAGE=local replayai record my_agent.py
# View in the dashboard
replayai ui
Session files are saved to ./ReplayAI/sessions/*.json with mode 0600 (owner read/write only) for security. The dashboard server reads from this directory automatically.
Comparing sessions
ReplaySession.compare() runs a live callable under a trace and diffs it against the recorded session. It uses LCS (Longest Common Subsequence) alignment by step name+type, so an inserted or removed step doesn't cascade into false divergences for every subsequent step.
from replayai import ReplaySession
replay = ReplaySession("ses_8fa1")
replay.mock("issue_refund", '{"refund_id":"ref_3391"}')
result = replay.compare(
agent_callable=lambda msg: my_agent.run(msg),
inputs="I was charged twice",
)
print(result["matches"]) # True if no divergences
print(result["step_count_loaded"]) # recorded step count
print(result["step_count_live"]) # live step count
for d in result["divergences"]:
print(f" step {d['step']}: {d['field']} (loaded={d['loaded']!r}, live={d['live']!r})")
Divergence field values: "output", "status", "model" (aligned steps that differ), "added" (live step not in recording), "removed" (recorded step not in live run).
Async
import asyncio
from replayai import atrace, arecord_step
@atrace("async-agent")
async def handle(message: str) -> str:
await arecord_step(type="llm_call", name="classify", status="success")
return "ok"
asyncio.run(handle("hello"))
Replay & export
from replayai import ReplaySession
replay = ReplaySession("ses_8fa1")
replay.mock("issue_refund", '{"refund_id":"ref_3391"}')
with replay.trace() as trace_obj:
replay.run(agent="support-agent-v3", framework="LangChain")
print(trace_obj.step_count, trace_obj.status)
print(replay.export(lang="pytest"))
LangChain integration
from replayai.integrations.langchain import trace_agent, ReplayCallbackHandler
@trace_agent("support-agent-v3", project="support-agent", tags=["production"])
def handle(message: str) -> str:
return executor.invoke({"input": message})["output"]
See examples/quickstart.py and examples/langchain_demo.py for runnable demos.
Windows
The SDK works on Windows. A few notes:
PATH warning: If you see The script replayai.exe is installed in ... which is not on PATH, use python -m replayai instead — it works identically:
python -m replayai ui
python -m replayai record my_agent.py
PowerShell env vars:
# Set env vars for the current session
$env:REPLAYAI_STORAGE = "local"
$env:REPLAYAI_STORAGE_PATH = ".\ReplayAI"
# Record + launch
python -m replayai record my_agent.py
python -m replayai ui
File permissions: Session files are created with restrictive permissions. On Windows, chmod is a no-op — files inherit the user's default ACL (typically single-user). The 0600/0700 modes are enforced on POSIX systems.
Firewall: The dashboard server listens on 0.0.0.0:7373. If Windows Firewall prompts, allow access for local development. To listen on localhost only, set --port and access via http://localhost:7373.
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file replayai_sdk-0.7.2.tar.gz.
File metadata
- Download URL: replayai_sdk-0.7.2.tar.gz
- Upload date:
- Size: 52.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8917b1f875cdb22c9dbe1fe31be5670fd31ae6258268f57bc47bb67bde810685
|
|
| MD5 |
81989dd33a1562971634f47be9c5edea
|
|
| BLAKE2b-256 |
90ee8f5192b6f8aa0ec8f004029a5f8d793a6c0a607179f66ca7f3ae62315e11
|
File details
Details for the file replayai_sdk-0.7.2-py3-none-any.whl.
File metadata
- Download URL: replayai_sdk-0.7.2-py3-none-any.whl
- Upload date:
- Size: 54.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a21600a0537edc8e14d760c5131d00ed8e7313621da92c2a23696b7870089ab
|
|
| MD5 |
866ddda32920c79a983a82d07d25b7e6
|
|
| BLAKE2b-256 |
b36b36d8d08cbbdcbd8fc0e0253d17a4687933d974dbe4889326b92edd37a8ab
|