Record agent execution traces for the agrex viewer
Project description
agrex (Python)
Record agent execution traces for the agrex viewer. Same trace format as the TypeScript @ppazosp/agrex/trace package — write a trace from Python, drop it on the viewer, get the same scrubbing replay.
Install
uv add agrex # or: pip install agrex
Requires Python 3.10+.
Quick start
from agrex import create_tracer
trace = create_tracer()
trace.agent("root", "Researcher", metadata={"input": query})
trace.stage("Search phase")
trace.tool("s1", "web_search", parent="root", args={"q": query})
# … do the work …
trace.done("s1", output=hits)
trace.done("root", output=summary)
# Drop on agrex.ppazosp.dev:
with open("run.jsonl", "w") as f:
f.write(trace.to_jsonl())
Async scopes — span()
Wraps any block. Emits node_add on entry and done / error on settle:
with trace.span(id="s1", label="web_search", parent="root"):
hits = search(query)
# async too
async with trace.span(id="s1", label="web_search", parent="root"):
hits = await search(query)
Default node type is tool. Pass type="sub_agent" etc. to override. On exception the span emits node_update with status="error" and a serialized exception in metadata.error, then re-raises.
Streaming for long runs
Pipe events directly to a file — the viewer reads JSONL natively.
with open("run.jsonl", "w") as f:
trace = create_tracer(out=f, buffer=False)
# ... emit events ...
trace.close() # flushes and closes the sink
buffer=False skips the in-memory log. events() / to_json() / to_jsonl() raise in that mode.
API summary
| Method | Emits |
|---|---|
agent(id, label, **kw) |
node_add (type: agent) |
sub_agent(id, label, **kw) |
node_add (type: sub_agent) |
tool(id, label, args=None, **kw) |
node_add (type: tool). args folds into metadata.args. |
file(id, label, **kw) |
node_add (type: file) |
node(partial) |
node_add — escape hatch, no validation |
update(id, status=, label=, metadata=) |
node_update |
done(id, output=None, metadata=None) |
node_update (status: done). output folds into metadata.output. |
error(id, error=None, metadata=None) |
node_update (status: error). Exceptions are serialized into metadata.error. |
remove(id) |
node_remove |
edge(id=, source=, target=, type=, label=) |
edge_add |
stage(label, color=None) |
stage |
marker(kind, label=None, color=None) |
marker |
clear() |
clear |
span(id=, label=, type='tool', **kw) |
sync/async context manager — node_add + done/error |
All node helpers accept parent, reads, writes, metadata, and status as keyword-only arguments.
Loading traces back
parse_trace auto-detects three input shapes — JSONL, {events: [...]}, or {nodes, edges} snapshots:
from agrex import parse_trace
with open("run.jsonl") as f:
events = parse_trace(f.read())
Raises TraceParseError with a format attribute on parse failure.
Thread safety
emit() is wrapped in a threading.Lock — concurrent calls from a ThreadPoolExecutor produce well-formed JSONL. asyncio-only callers pay no real cost (the lock is uncontended). The on_event callback runs outside the lock so user code never serializes across all emits.
Trace format
See the trace format spec. The Python and TypeScript packages emit the same canonical shape; the cross-language fixture round-trip test in CI guards against drift.
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 agrex-0.7.0.tar.gz.
File metadata
- Download URL: agrex-0.7.0.tar.gz
- Upload date:
- Size: 50.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8382887994d2fc5e3c0ad0d91cbb4f4b67b1b54ed9a807a0fbcd4540390d8e12
|
|
| MD5 |
b00b9f5fb76d4193330c8774fd0e6a11
|
|
| BLAKE2b-256 |
c1d4d162416e7a8b4d106569c3c79debb876e576ac68ff959a686f0d5e2c3330
|
Provenance
The following attestation bundles were made for agrex-0.7.0.tar.gz:
Publisher:
ci.yml on ppazosp/agrex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agrex-0.7.0.tar.gz -
Subject digest:
8382887994d2fc5e3c0ad0d91cbb4f4b67b1b54ed9a807a0fbcd4540390d8e12 - Sigstore transparency entry: 1402849434
- Sigstore integration time:
-
Permalink:
ppazosp/agrex@926a6428efa0dedad7ea5dc9f0c28af79f1d2656 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ppazosp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@926a6428efa0dedad7ea5dc9f0c28af79f1d2656 -
Trigger Event:
push
-
Statement type:
File details
Details for the file agrex-0.7.0-py3-none-any.whl.
File metadata
- Download URL: agrex-0.7.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46c130b68266673ab07ed7dc2c5163d91ab9099e5cc21ecc929a6c2053b24565
|
|
| MD5 |
86c9405df30de847bc2df5ec202aac1a
|
|
| BLAKE2b-256 |
fcced85605231af4ca21fd2a4a44ba24854f8edcc003fdb551969a9e96af72c9
|
Provenance
The following attestation bundles were made for agrex-0.7.0-py3-none-any.whl:
Publisher:
ci.yml on ppazosp/agrex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agrex-0.7.0-py3-none-any.whl -
Subject digest:
46c130b68266673ab07ed7dc2c5163d91ab9099e5cc21ecc929a6c2053b24565 - Sigstore transparency entry: 1402849686
- Sigstore integration time:
-
Permalink:
ppazosp/agrex@926a6428efa0dedad7ea5dc9f0c28af79f1d2656 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ppazosp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@926a6428efa0dedad7ea5dc9f0c28af79f1d2656 -
Trigger Event:
push
-
Statement type: