Subscribe API
The Subscribe API (codename: Synapse) provides a lightweight, imperative way to bind and unbind agent hooks without decorators.Overview
While decorators (@agent.hooks.*) work great for permanent hooks, the Subscribe API is designed for:
- Dynamic hook registration: Register/unregister hooks at runtime
- Temporary subscriptions: Use context managers for scoped hooks
- Conditional monitoring: Enable hooks only when needed
- Bulk operations: Register multiple hooks as a group
Core Methods
agent.on()
Register a single hook by friendly name.name: Friendly hook name (e.g.,"stream:chunk","tool:pre_exec")fn: Callback function (sync or async)
- Subscription ID string for later unsubscription
agent.subscribe()
Register multiple hooks at once.mapping: Dictionary of{friendly_name: callback_function}
- Group subscription ID for unsubscribing all at once
agent.unsubscribe()
Unregister a subscription (single or group).sub_id: Subscription ID fromon()orsubscribe()
- Idempotent: Safe to call multiple times
- No errors: Never raises exception for non-existent IDs
- Group aware: Unsubscribes all hooks in a group
agent.subscription()
Create a context manager for temporary subscriptions.name_or_mapping: Either a friendly name (str) or a mapping dictfn: Callback function (required if name_or_mapping is str)
- Context manager that auto-unsubscribes on exit
Available Hook Names
All hook names follow the formatcategory:event.
Streaming Hooks
Tool Hooks
Context Hooks
Message Hooks
Scaffold Hooks
Usage Patterns
Pattern 1: Dynamic Monitoring
Enable/disable logging based on conditions:Pattern 2: Temporary Streaming
Stream responses for interactive sessions only:Pattern 3: Testing and Debugging
Capture hook data for assertions:Pattern 4: Rate Limiting
Implement custom rate limiting:Pattern 5: Progressive Enhancement
Add hooks incrementally:Integration with Decorator Hooks
Subscribe API and decorator hooks work together seamlessly:- Permanent hooks fire first (in registration order)
- Subscribe API hooks fire second (in registration order)
Error Handling
Unknown Hook Names
Helpful suggestions when hook name is misspelled:Non-Callable Functions
Safe Unsubscription
Best Practices
Use context managers for temporary hooks
Use context managers for temporary hooks
Group related hooks with subscribe()
Group related hooks with subscribe()
Store subscription IDs for later cleanup
Store subscription IDs for later cleanup
Use decorators for permanent hooks
Use decorators for permanent hooks
Performance Considerations
- Minimal overhead: Subscribe API is a thin layer over decorator system
- No double-firing: Same hooks registered twice are tracked separately
- Idempotent unsubscribe: Safe to call multiple times
- Context manager overhead: Negligible (less than 1ms per subscription)
Comparison: Subscribe API vs Decorators
| Feature | Subscribe API | Decorators |
|---|---|---|
| Registration | Runtime | Definition time |
| Unsubscription | Yes (anytime) | No |
| Context manager | Yes | No |
| Conditional hooks | Easy | Requires logic in hook |
| Testing | Excellent | Good |
| Bulk operations | subscribe() | Multiple decorators |
| Friendly names | Yes | No (use attributes) |
| Best for | Dynamic, temporary | Permanent, application-wide |

