Debug agent execution: $ARGUMENTS
Before Starting
- Read the tracing infrastructure:
src/agents/shared/agent-tracer.ts—AgentTracerclass, event recordingsrc/agents/shared/hooks.ts— lifecycle hooks (onStart, onToolCall, onToolComplete, onComplete, onError)src/lib/database/schema.ts— search foragent_tracesandagent_metricstables
Trace Event Types
The AgentTracer records these events during execution:
| Event | When | Data |
|---|---|---|
start |
Execution begins | message length, context availability |
tool_call |
Tool invoked | toolName, params preview (truncated 200 chars) |
tool_complete |
Tool returns | toolName, duration, success flag |
handoff |
Agent delegates | target agent, reason |
stream_delta |
First text token | (recorded once only) |
complete |
Execution done | output length, duration, tool count |
error |
Failure | message, stack (truncated) |
Querying Traces
Recent traces for an agent
SELECT id, agent_name, trace_type, duration, tool_calls_count, error_message,
created_at
FROM agent_traces
WHERE agent_name = '<agent-name>'
ORDER BY created_at DESC
LIMIT 20;
Traces with errors
SELECT id, agent_name, error_message, trace_data, created_at
FROM agent_traces
WHERE error_message IS NOT NULL
ORDER BY created_at DESC
LIMIT 10;
Slow traces (over 10s)
SELECT id, agent_name, duration, tool_calls_count, trace_data, created_at
FROM agent_traces
WHERE duration > 10000
ORDER BY duration DESC
LIMIT 10;
Tool call breakdown
SELECT
trace_data->'events' AS events,
tool_calls_count,
duration
FROM agent_traces
WHERE agent_name = '<agent-name>'
AND tool_calls_count > 0
ORDER BY created_at DESC
LIMIT 5;
Agent metrics summary
SELECT agent_name, date,
usage_count, avg_response_time, success_rate,
cost_total, tokens_total, error_count
FROM agent_metrics
WHERE agent_name = '<agent-name>'
ORDER BY date DESC
LIMIT 30;
Debug Flags
Set these environment variables to enable verbose logging:
AI_LAB_DEBUG_TOOLS=true # Log tool selection & validation
AI_LAB_DEBUG_CONTEXT=true # Log context receipt & changes
AI_LAB_DEBUG_INSTRUCTIONS=true # Log assembled instructions (caution: large)
AI_LAB_DEBUG_RETRIEVAL=true # Log file_search queries, sources, scores
Analysis Patterns
Diagnosing slow responses
- Query traces with high duration
- Check
eventsarray for tool_call → tool_complete durations - Identify which tool is the bottleneck
- Check if the tool is hitting an external service (file_search, search API)
- Consider caching or timeout adjustments
Diagnosing tool failures
- Query traces where
error_messageis not null - Look at the
tool_callevent immediately before theerrorevent - Check if the tool params were valid
- Check if the external service was available
Diagnosing guardrail rejections
- Guardrail rejections show as
errorevents with the guardrail name - Check the
reasonfield for why the message was rejected - Review the input message and guardrail logic
Comparing agent performance over time
- Query
agent_metricstable for daily aggregates - Check trends in avg_response_time, success_rate, error_count
- Correlate spikes with deployment dates or config changes
Rules
- Use the Supabase MCP (
mcp__supabase__execute_sql) for querying traces directly - Never delete traces — they're the audit trail for agent behavior
- When reporting findings, include specific trace IDs for reference
- Debug flags should only be enabled temporarily — they generate large log volumes
- If traces reveal a systemic issue, recommend a fix in the relevant layer (tool, instructions, guardrail)