Hooks System - Getting Started
The Radium hooks system enables you to intercept and customize behavior at various points in the execution flow. This guide will help you get started with using hooks.
Overviewβ
Hooks allow you to:
- Add custom logging and monitoring
- Modify execution flow dynamically
- Inject telemetry or metrics collection
- Handle errors with custom logic
- Intercept tool execution
Installationβ
Hooks can be used in two ways:
- Programmatic Registration: Register hooks directly in code (recommended for custom hooks)
- Configuration-Based: Configure hooks in workspace (
.radium/hooks.toml) or extension manifest
For configuration-based hooks:
- Create hook configuration in
.radium/hooks.toml - Register hooks programmatically (config controls enable/disable state)
- Hooks are automatically discovered and enabled/disabled based on configuration
Basic Usageβ
Using Example Hooksβ
Radium includes example hook implementations:
- Logging Hook: Logs model calls with timestamps and metadata
- Metrics Hook: Aggregates telemetry data including token usage and costs
Configurationβ
Hooks are configured via TOML files. Create a .radium/hooks.toml file in your workspace:
[[hooks]]
name = "logging-hook-before"
type = "before_model"
priority = 100
enabled = true
[hooks.config]
log_level = "info"
log_format = "json"
CLI Commandsβ
List all registered hooks:
rad hooks list
Show details for a specific hook:
rad hooks info logging-hook-before
Enable or disable a hook:
rad hooks enable logging-hook-before
rad hooks disable logging-hook-before
Hook Typesβ
Radium supports the following hook types:
Model Hooksβ
before_model- Executed before model callsafter_model- Executed after model calls
Tool Hooksβ
before_tool- Executed before tool executionafter_tool- Executed after tool executiontool_selection- Executed during tool selection
Error Hooksβ
error_interception- Intercept errors before propagationerror_transformation- Transform error messageserror_recovery- Attempt error recoveryerror_logging- Log errors with custom formatting
Telemetry Hooksβ
telemetry_collection- Collect and aggregate telemetrycustom_logging- Custom logging hooksmetrics_aggregation- Aggregate metricsperformance_monitoring- Monitor performance
Common Use Casesβ
Logging Model Callsβ
Use a logging hook to track all model calls:
[[hooks]]
name = "model-logger"
type = "before_model"
priority = 100
enabled = true
Tracking Costsβ
Use a metrics hook to track token usage and costs:
[[hooks]]
name = "cost-tracker"
type = "telemetry_collection"
priority = 100
enabled = true
Error Handlingβ
Use error hooks to implement custom error handling:
[[hooks]]
name = "error-handler"
type = "error_interception"
priority = 200
enabled = true
[hooks.config]
notify_on_error = true
retry_count = 3
Hook Priorityβ
Hooks execute in priority order (higher priority = executes first). Default priority is 100.
- High priority (200+): Critical hooks that must run first
- Medium priority (100-199): Standard hooks
- Low priority (<100): Optional hooks
Next Stepsβ
- See Hook Development Guide to create your own hooks
- See API Reference for complete API documentation
- Check out example implementations in
examples/hooks/