Skip to main content

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.
Parameters:
  • name: Friendly hook name (e.g., "stream:chunk", "tool:pre_exec")
  • fn: Callback function (sync or async)
Returns:
  • Subscription ID string for later unsubscription
Example:

agent.subscribe()

Register multiple hooks at once.
Parameters:
  • mapping: Dictionary of {friendly_name: callback_function}
Returns:
  • Group subscription ID for unsubscribing all at once
Example:

agent.unsubscribe()

Unregister a subscription (single or group).
Parameters:
  • sub_id: Subscription ID from on() or subscribe()
Important:
  • Idempotent: Safe to call multiple times
  • No errors: Never raises exception for non-existent IDs
  • Group aware: Unsubscribes all hooks in a group
Example:

agent.subscription()

Create a context manager for temporary subscriptions.
Parameters:
  • name_or_mapping: Either a friendly name (str) or a mapping dict
  • fn: Callback function (required if name_or_mapping is str)
Returns:
  • Context manager that auto-unsubscribes on exit
Supports both sync and async usage. Example (single hook):
Example (multiple hooks):
Early unsubscription:

Available Hook Names

All hook names follow the format category: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:
Execution order:
  1. Permanent hooks fire first (in registration order)
  2. 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

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

What’s Next?

Tool Hooks

Tool execution lifecycle hooks

Context Hooks

Context tree operation hooks

Streaming Hooks

Real-time content processing

Message Hooks

Message handling and modification