AtomicAgent Hook System Example
July 12, 2026 ยท View on GitHub
This example demonstrates the powerful hook system integration in AtomicAgent, which leverages Instructor's hook system for comprehensive monitoring, error handling, and intelligent retry mechanisms.
Features Demonstrated
- ๐ Comprehensive Monitoring: Track all aspects of agent execution
- ๐ก๏ธ Robust Error Handling: Graceful handling of validation and completion errors
- ๐ Intelligent Retry Patterns: Implement smart retry logic based on error context
- ๐ Performance Metrics: Monitor response times, success rates, and error patterns
- ๐ง Easy Debugging: Detailed error information and execution flow visibility
- โก Zero Overhead: Hooks only execute when registered and enabled
Getting Started
-
Clone the main Atomic Agents repository:
git clone https://github.com/eigenwise/atomic-agents -
Navigate to the hooks-example directory:
cd atomic-agents/atomic-examples/hooks-example -
Install the dependencies using uv:
uv sync -
Set up your OpenAI API key:
export OPENAI_API_KEY="your-api-key-here" -
Run the example:
uv run python hooks_example/main.py
What This Example Shows
The example demonstrates several key hook system patterns:
Basic Hook Registration
- Simple parse error logging
- Completion monitoring and metrics collection
Advanced Error Handling
- Comprehensive validation error analysis
- Intelligent retry mechanisms with backoff strategies
- Error isolation to prevent hook failures from disrupting execution
Performance Monitoring
- Response time tracking
- Success rate calculation
- Error pattern analysis
Real-World Scenarios
- Handling malformed responses
- Network timeouts and retry logic
- Model switching on repeated failures
Key Benefits
This hook system implementation provides:
- Full Instructor Integration: All Instructor hook events are supported
- Backward Compatibility: Existing AtomicAgent code works unchanged
- Error Context: Rich error information for intelligent decision making
- Performance Insights: Detailed metrics for optimization
- Production Ready: Robust error handling suitable for production use
Hook Events Supported
parse:error- Triggered on Pydantic validation failurescompletion:kwargs- Before API calls are madecompletion:response- After API responses are receivedcompletion:error- On API or network errors
GitHub Issue Resolution
This example demonstrates the complete resolution of GitHub issue #173, showing how the AtomicAgent hook system enables:
- โ Parse error hooks triggering on validation failures
- โ Comprehensive error context for retry mechanisms
- โ Full Instructor hook event support
- โ 100% backward compatibility
- โ Robust error isolation
Next Steps
After running this example, you can:
- Experiment with different hook combinations
- Implement custom retry strategies
- Add your own monitoring and alerting logic
- Explore integration with observability platforms