Append structured step data to an agent trace and produce a running summary of loop execution.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement log_step(trace, step_data) that appends a step to an agent execution trace and returns the updated summary.
trace is a list of step dicts (may be empty for the first step).
step_data is a dict with keys:
- step_number (int, required)
- thought (str, required)
- action (str, required — the tool name or "Final Answer")
- arguments (dict, optional — defaults to {})
- observation (str, optional — defaults to "")
- duration_ms (int, optional — defaults to 0)
Return a summary dict:
{
"total_steps": len(trace) + 1,
"last_step": step_number,
"last_action": action,
"tool_calls": <count of steps where action is not "Final Answer">,
"total_duration_ms": <sum of all duration_ms across trace + step_data>,
"thoughts": <list of all thought strings in order>
}
Edge cases:
- If step_data is None or missing required keys (step_number, thought, action), return None without modifying the trace.
- If trace is None, treat it as an empty list.
- Compute total_steps as the current number of steps after appending.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
def log_step(trace, step_data):
"""Append a step to an agent trace and return the updated summary."""Implement trace logging logic
Already have Pro access? Sign in