Execution Traces
Execution traces capture the complete, step-by-step history of an agent run — including the agent's reasoning, tool calls, observations, and decisions.
8 min•By Priygop Team•Updated 2026
What a Trace Contains
- Step index: the sequential number of this step in the run
- Agent reasoning: the agent's chain-of-thought before selecting an action
- Action selected: the tool name and arguments the agent decided to call
- Observation: the raw result returned by the tool
- State snapshot: what the state looked like before and after this step
- Timing: how long the reasoning and tool call each took
- Cost: how many tokens were consumed in this step
Trace Capture
Trace Capture
# Execution trace capture
from datetime import datetime, timezone
import time
class ExecutionTracer:
def __init__(self, task_id: str):
self.task_id = task_id
self.trace = {
"task_id": task_id,
"started_at": datetime.now(timezone.utc).isoformat(),
"steps": []
}
def record_step(
self,
step: int,
reasoning: str,
action: str,
args: dict,
observation: dict,
tokens_used: int = 0
):
"""Record a complete step in the execution trace."""
step_record = {
"step": step,
"timestamp": datetime.now(timezone.utc).isoformat(),
"reasoning": reasoning[:500], # Truncate long reasoning
"action": action,
"args": args,
"observation": {k: str(v)[:200] for k, v in observation.items()},
"tokens_used": tokens_used,
}
self.trace["steps"].append(step_record)
return step_record
def complete(self, outcome: str, result: dict = None):
self.trace["completed_at"] = datetime.now(timezone.utc).isoformat()
self.trace["outcome"] = outcome
self.trace["result"] = result
self.trace["total_steps"] = len(self.trace["steps"])
self.trace["total_tokens"] = sum(s["tokens_used"] for s in self.trace["steps"])
def get_summary(self) -> dict:
"""Return a condensed trace summary for review."""
return {
"task_id": self.task_id,
"outcome": self.trace.get("outcome"),
"total_steps": self.trace.get("total_steps", 0),
"total_tokens": self.trace.get("total_tokens", 0),
"actions": [s["action"] for s in self.trace["steps"]],
}
# Usage
tracer = ExecutionTracer("TASK-001")
tracer.record_step(
step=1,
reasoning="I need to find the order first before I can process the refund.",
action="get_order",
args={"order_id": "ORD-123"},
observation={"status": "success", "total": 49.99, "status_": "delivered"},
tokens_used=250
)
tracer.record_step(
step=2,
reasoning="Order found and eligible. Issuing the refund now.",
action="issue_refund",
args={"order_id": "ORD-123", "amount": 49.99},
observation={"status": "success", "refund_id": "REF-789"},
tokens_used=200
)
tracer.complete("success", {"refund_id": "REF-789"})
print("Trace summary:", tracer.get_summary())Key Takeaways
- Execution traces capture the complete, step-by-step history of an agent run — including the agent's reasoning, tool calls, observations, and decisions.
- Step index: the sequential number of this step in the run
- Agent reasoning: the agent's chain-of-thought before selecting an action
- Action selected: the tool name and arguments the agent decided to call