AIF-54Recommended
Record every step of every run
Store each step of an agent run with its tool name, arguments, a short result, the time and the cost. Let support and the user see what the agent did.
How to do it
Give each run an ID and write one row per step. Mask personal data in arguments and results, as for prompt text. Show users a plain list of the actions taken for them, with undo. The method is in log agent actions and let users undo them. Emit each step as a trace span too, so one trace shows the request, the run and its tool calls. The OpenTelemetry conventions for generative AI name an invoke_agent span for the run and an execute_tool span for each tool call. They carry attributes such as gen_ai.agent.name and gen_ai.tool.name. The conventions are still marked Development, so record the version you follow.
Examples
TypeScript example
await db.agentSteps.insert({ runId: run.id, step, tool: call.name, args: maskPersonalData(JSON.stringify(call.args)),
resultSummary: maskPersonalData(summarise(result)).slice(0, 500), costUsd, at: new Date() });
Python example
db.agent_steps.insert(run_id=run.id, step=step, tool=call.name, args=mask_personal_data(json.dumps(call.args)),
result_summary=mask_personal_data(summarise(result))[:500], cost_usd=cost_usd, at=datetime.now(UTC))
How to check
Run a test task with three tool calls. The run's trace has three step rows with tool, arguments, result, time and cost. The user's activity page lists the same actions. The request's trace shows one invoke_agent span with three execute_tool spans under it.
Sources
Related rules
- AIF-47 Log every model call in AI features
- AIF-48 Keep prompt and answer text briefly and apart in AI features
- AGT-22 Log every agent change and return an undo handle in Agents as users
- AIF-72 Give each sub-agent no more power than the agent that started it in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/agents-in-products/agent-run-trace">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.