Python API Reference¶
Top-level exports¶
agentdelta.trace.AgentTrace(run_id, nodes=list(), edges=list(), metadata=dict())
dataclass
¶
Complete execution trace for a single agent run.
Attributes:
| Name | Type | Description |
|---|---|---|
run_id |
str
|
Unique identifier for this run (e.g. |
nodes |
list[TraceNode]
|
Ordered list of trace steps. |
edges |
list[TraceEdge]
|
Directed edges connecting steps. |
metadata |
dict[str, Any]
|
Arbitrary key/value pairs stored in the trace header. |
add_node(node)
¶
add_edge(edge)
¶
save(path)
¶
Write the trace to a JSONL file at path (one record per line).
Source code in src/agentdelta/trace.py
load(path)
classmethod
¶
Load a trace from a JSONL file previously written by save.
Source code in src/agentdelta/trace.py
agentdelta.trace.TraceNode(step, node_type, content, metadata=dict(), embedding=None)
dataclass
¶
A single step in an agent execution trace.
Attributes:
| Name | Type | Description |
|---|---|---|
step |
int
|
1-based sequential position in the trace. |
node_type |
NodeType
|
Category of this step (LLM reasoning, tool call, etc.). |
content |
str
|
Human-readable text - reasoning output, |
metadata |
dict[str, Any]
|
Arbitrary key/value pairs for framework-specific data. |
embedding |
list[float] | None
|
Floating-point sentence embedding, populated by |
agentdelta.trace.TraceEdge(source_step, target_step, edge_type, label='')
dataclass
¶
A directed connection between two steps in a trace.
Attributes:
| Name | Type | Description |
|---|---|---|
source_step |
int
|
Step number of the originating node. |
target_step |
int
|
Step number of the destination node. |
edge_type |
EdgeType
|
Semantic category of this transition. |
label |
str
|
Optional human-readable label (tool name, phase, etc.). |
agentdelta.trace.NodeType
¶
Bases: str, Enum
Classification of a single step in an agent trace.
agentdelta.trace.EdgeType
¶
Bases: str, Enum
Classification of a directed edge between two trace nodes.
Diff¶
agentdelta.diff.diff_traces(trace_a, trace_b, fork_threshold=0.7, match_threshold=0.85)
¶
Compute a semantic diff between two agent traces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace_a
|
AgentTrace
|
Baseline trace. |
required |
trace_b
|
AgentTrace
|
Comparison trace. |
required |
fork_threshold
|
float
|
Similarity below this triggers a fork point. |
0.7
|
match_threshold
|
float
|
Similarity above this is considered a match. |
0.85
|
Returns:
| Type | Description |
|---|---|
DiffResult
|
DiffResult with aligned steps and the first fork point if found. |
Source code in src/agentdelta/diff.py
agentdelta.diff.DiffResult(run_id_a, run_id_b, steps=list(), fork_point=None, summary=dict())
dataclass
¶
Full diff result between two agent traces.
Attributes:
| Name | Type | Description |
|---|---|---|
run_id_a |
str
|
Run identifier of the baseline trace. |
run_id_b |
str
|
Run identifier of the candidate trace. |
steps |
list[StepDiff]
|
All aligned step pairs, in order. |
fork_point |
ForkPoint | None
|
The first divergent step, or |
summary |
dict[str, Any]
|
Pre-computed aggregate statistics (total, matched, changed, etc.). |
has_regression
property
¶
True if the traces diverged (a fork point was detected).
changed_steps
property
¶
Steps where both traces have a node but they diverged semantically.
added_steps
property
¶
Steps present only in trace B (inserted relative to baseline).
removed_steps
property
¶
Steps present only in trace A (removed relative to baseline).
agentdelta.diff.ForkPoint(step_a, step_b, node_a, node_b, similarity, description)
dataclass
¶
The first step where two traces take meaningfully different paths.
Attributes:
| Name | Type | Description |
|---|---|---|
step_a |
int
|
Step number in trace A where the fork occurred. |
step_b |
int
|
Step number in trace B where the fork occurred. |
node_a |
TraceNode
|
The divergent node from trace A. |
node_b |
TraceNode
|
The divergent node from trace B. |
similarity |
float
|
Cosine similarity between the two nodes at the fork (< fork_threshold). |
description |
str
|
Human-readable explanation of why this step diverged. |
agentdelta.diff.StepDiff(step_a, step_b, similarity, status, summary='')
dataclass
¶
A single aligned step pair with its comparison result.
Attributes:
| Name | Type | Description |
|---|---|---|
step_a |
TraceNode | None
|
Node from trace A, or |
step_b |
TraceNode | None
|
Node from trace B, or |
similarity |
float
|
Cosine similarity between the two nodes (0.0 for added/removed). |
status |
str
|
One of |
summary |
str
|
Human-readable one-line description of this diff entry. |
Embeddings¶
agentdelta.embed.embed_trace(trace, batch_size=64)
¶
Compute embeddings for all nodes in a trace (in-place) and return the trace.
Source code in src/agentdelta/embed.py
agentdelta.embed.align_traces(trace_a, trace_b, window=5, threshold=0.75)
¶
Align nodes from two traces by semantic similarity within a sliding window.
Uses greedy 1:1 matching: each node in trace_a is paired with the closest unmatched node in trace_b within ±window positions.
Returns:
| Type | Description |
|---|---|
list[tuple[TraceNode | None, TraceNode | None, float]]
|
List of |
list[tuple[TraceNode | None, TraceNode | None, float]]
|
Unmatched nodes appear as |
Source code in src/agentdelta/embed.py
agentdelta.embed.cosine_similarity(a, b)
¶
Return cosine similarity between two embedding vectors. Returns 0.0 for zero vectors.
Source code in src/agentdelta/embed.py
agentdelta.embed.find_best_match(node, candidates, threshold=0.75)
¶
Find the candidate most semantically similar to node.
Returns (best_node, score). If the best score is below threshold,
returns (None, best_score) rather than a low-confidence match.
Source code in src/agentdelta/embed.py
Instrumentation¶
agentdelta.instrument.record(output_path, run_id=None)
¶
Context manager that records an agent run and saves the trace on exit.
Usage
with agentdelta.record("run_a.jsonl") as cb: agent.invoke({"input": "..."}, config={"callbacks": [cb]})
trace_a.jsonl is now saved¶
Source code in src/agentdelta/instrument.py
agentdelta.instrument.AgentdeltaCallback(run_id=None)
¶
LangChain BaseCallbackHandler-compatible callback that records agent runs as AgentTrace objects.
Usage
callback = AgentdeltaCallback() agent.invoke({"input": "..."}, config={"callbacks": [callback]}) trace = callback.trace trace.save("run.jsonl")
Source code in src/agentdelta/instrument.py
on_llm_start(serialized, prompts, **kwargs)
¶
on_llm_end(response, **kwargs)
¶
Record an LLM generation as a NodeType.LLM trace node.
Source code in src/agentdelta/instrument.py
on_tool_start(serialized, input_str, **kwargs)
¶
Record a tool invocation as a NodeType.TOOL_CALL trace node.
Source code in src/agentdelta/instrument.py
on_tool_end(output, **kwargs)
¶
Record a tool result as a NodeType.TOOL_RETURN trace node.
Source code in src/agentdelta/instrument.py
on_chain_start(serialized, inputs, **kwargs)
¶
Record the initial chain input as a NodeType.START node (first call only).
Source code in src/agentdelta/instrument.py
on_chain_end(outputs, **kwargs)
¶
Record the final chain output as a NodeType.END node.
Source code in src/agentdelta/instrument.py
on_agent_action(action, **kwargs)
¶
on_agent_finish(finish, **kwargs)
¶
on_llm_error(error, **kwargs)
¶
on_tool_error(error, **kwargs)
¶
Report¶
agentdelta.report.print_diff(result, show_matches=False, console=None)
¶
Print a Rich-formatted diff to the terminal.
Source code in src/agentdelta/report.py
agentdelta.report.to_json(result)
¶
Serialize a DiffResult to JSON for CI/CD consumption.
Source code in src/agentdelta/report.py
agentdelta.report.to_markdown(result)
¶
Generate a GitHub PR comment in Markdown.