Available event hooks
Strukt supports four event types:- onStep: Fired at major strategy milestones
- onProgress: Fired during batch processing with current/total counts
- onMessage: Fired for each LLM message (system, user, assistant)
- onTokenUsage: Fired after each LLM call with token usage
Basic usage
Pass anevents object to the extract() function:
onStep hook
Tracks major milestones in the extraction strategy.Signature
Example usage
Step labels by strategy
Simple:start: Extraction beginsextract: Processing single passcomplete: Extraction finished
start: Strategy beginsbatch 1/N,batch 2/N, …: Each batch completesmerge: Merging batch resultscomplete: Strategy finished
start: Strategy beginsbatch 1/N,batch 2/N, …: Each batch completescomplete: Strategy finished
- Same as parallel/sequential, plus:
auto-merge: Schema-aware merge stepdedupe: Deduplication step
- All steps from first pass
- All steps from second pass
onProgress hook
Tracks progress during batch processing with precise counts.Signature
Example usage
onMessage hook
Inspects all messages sent to and received from the LLM.Signature
Example usage
Debugging validation retries
TheonMessage hook is particularly useful for debugging schema validation failures:
onTokenUsage hook
Tracks token consumption for each LLM call.Signature
Example usage
Building a progress bar
CombineonStep and onProgress to build rich progress indicators:
src/cli.ts:326-408).
Async event handlers
All event hooks support async handlers:Event hook reference
| Hook | When fired | Use case |
|---|---|---|
onStep | Major strategy milestones | High-level progress tracking |
onProgress | During batch processing | Detailed progress bars |
onMessage | Each LLM message | Debugging, logging prompts |
onTokenUsage | After each LLM call | Cost tracking, usage monitoring |