- Understand Behavior - See the agent’s reasoning, tool selections, and decision-making process as execution unfolds
- Diagnose Issues - Identify stalling (no tool use), looping (repeated calls), errors (failed tools), or resource exhaustion (tokens, time, cost)
- Optimize Performance - Analyze token usage, tool call patterns, and execution timing to improve efficiency
- Validate Correctness - Verify the agent completes tasks correctly within expected resource bounds
run() for basic execution and final results. Use stream() for real-time visibility during development. Use thread inspection and stop conditions for analyzing patterns, preventing loops, and enforcing resource limits. Use logging and metrics for production monitoring.
This guide covers streaming events, result inspection, common issues (stalling, looping, tool errors), stop conditions for controlled execution, thread management for testing, and independent tool validation.
Quick Start with run()
The simplest way to execute an agent is with the run() method, which returns the final AgentResult:
Streaming Events
For programmatic access to execution details, usestream():
Inspecting Results
After execution, analyze theAgentResult:
Understanding Stop Reasons
Thestop_reason indicates why the agent stopped executing. All possible values:
"finished"- Agent completed successfully (stop condition met or natural completion)"max_steps_reached"- Hit themax_stepslimit before completing"max_tool_calls_reached"- Hit themax_tool_callslimit"stalled"- Agent produced a response without tool calls and no stop conditions were met"error"- An unhandled error occurred during execution
Analyzing Tool Usage
Common Issues and Solutions
Agent Stalls Immediately
Symptom: Agent produces a response but doesn’t call any tools. Diagnosis:- Check that tools are properly passed to the agent
- Verify the model supports function calling (or use
tool_mode="xml") - Make instructions more directive: “Use the X tool to…”
- Add a
retry_with_feedbackhook forAgentStalledevents
Agent Loops on the Same Tool
Symptom: Agent calls the same tool repeatedly with identical arguments. Diagnosis:- Add
no_new_tool_usedstop condition - Create a loop-detection hook that returns
Fail - Improve tool descriptions so the agent understands when to move on
Agent Hits Max Steps
Symptom:result.stop_reason == "max_steps_reached"
Diagnosis: The agent didn’t finish within the allowed steps.
- Increase
max_stepsfor complex tasks - Use
TaskAgentwhich continues until explicit completion - Simplify the task or break it into smaller pieces
- Check if the agent is stuck (see loop detection above)
Tool Errors
Symptom: Tools return errors instead of results. Diagnosis:- Add
catch=Trueto tool decorator for graceful error handling - Validate inputs in your tool implementation
- Check tool argument types match the schema
Context Length Exceeded
Symptom: Error about context window or token limits. Solutions:- Add
summarize_when_longhook to automatically compress history - Use
token_usagestop condition to halt before hitting limits - Truncate large tool outputs with
truncateparameter
Handling Rate Limits and Errors
Symptom: Intermittent rate limit errors or API failures. Use backoff hooks to automatically retry with exponential backoff:Stop Conditions for Debugging
Stop conditions control when an agent should halt execution. Beyondmax_steps, you can use specialized conditions for precise control:
Budget and Resource Limits
Behavior-Based Conditions
Combining Stop Conditions
Stop conditions can be combined with| (OR) and & (AND):
Thread Management for Testing
The agent’s thread stores all messages and events. You can manipulate it for debugging:Resetting Between Tests
Forking Threads for A/B Testing
Create branched execution paths to test different approaches:fork() copies messages but not events. Events are execution history and don’t carry forward to forked threads.
Custom Thread Management
Logging
Enable debug logging to see internal operations:tool_metrics hook to track tool performance:
tool/total_count, tool/success_rate, and per-tool timing to the Dreadnode platform.

