Skip to main content

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


Download files

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

Source Distribution

agrex-0.7.0.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

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

agrex-0.7.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

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

Hashes for agrex-0.7.0.tar.gz
Algorithm Hash digest
SHA256 8382887994d2fc5e3c0ad0d91cbb4f4b67b1b54ed9a807a0fbcd4540390d8e12
MD5 b00b9f5fb76d4193330c8774fd0e6a11
BLAKE2b-256 c1d4d162416e7a8b4d106569c3c79debb876e576ac68ff959a686f0d5e2c3330

See more details on using hashes here.

Provenance

The following attestation bundles were made for agrex-0.7.0.tar.gz:

Publisher: ci.yml on ppazosp/agrex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for agrex-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 46c130b68266673ab07ed7dc2c5163d91ab9099e5cc21ecc929a6c2053b24565
MD5 86c9405df30de847bc2df5ec202aac1a
BLAKE2b-256 fcced85605231af4ca21fd2a4a44ba24854f8edcc003fdb551969a9e96af72c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for agrex-0.7.0-py3-none-any.whl:

Publisher: ci.yml on ppazosp/agrex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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