Advanced Patterns
Pipeline events, debugging tools, performance optimization, error handling, and best practices for production use.
π¬ Pipeline Events
Pipelines emit events during execution that you can intercept for monitoring, auditing, and debugging.
Available Events
beforeAIPipelineRun
Before pipeline execution
{ sequence, name, stepCount, steps, input, params, options }
afterAIPipelineRun
After pipeline execution
{ sequence, name, stepCount, steps, input, result, executionTime }
Event Interception
BoxRegisterInterceptor( {
interceptorObject: {
beforeAIPipelineRun: ( event, interceptData ) => {
println( "Pipeline starting: #interceptData.name#" )
println( "Steps: #interceptData.stepCount#" )
},
afterAIPipelineRun: ( event, interceptData ) => {
println( "Pipeline completed: #interceptData.name#" )
println( "Time: #interceptData.executionTime#ms" )
}
}
} )
pipeline = aiMessage().user( "Hello" ).toDefaultModel()
result = pipeline.run()
// Console output:
// Pipeline starting: AiRunnableSequence
// Steps: 2
// Pipeline completed: AiRunnableSequence
// Time: 1543msUse cases: performance monitoring, cost tracking (count tokens), error auditing, security logging.
π Debugging Pipelines
Print Pipeline Structure
Use .print() to see exactly what steps are in a pipeline:
Inspect Steps
Get detailed information about each step:
Step-by-Step Execution
Execute each step manually to isolate problems:
β‘ Performance Optimization
Choose the Right Model
Use cheaper/faster models for simple tasks β reserve powerful models for complex reasoning:
Minimize Transform Steps
Combine transformations where possible:
Cache Expensive Results
Avoid repeated AI calls for the same input:
π Error Handling
Try-Catch
Wrap pipeline execution to handle AI failures gracefully:
Graceful Degradation
Provide rule-based fallbacks when AI is unavailable:
Validation Steps
Validate at every critical boundary:
π Best Practices
Design Principles
β Single Responsibility β each step does one thing well β Immutability β never modify pipeline state during execution β Composition β build complex workflows from simple components β Reusability β design pipelines as parameterized templates β Explicit > Implicit β be clear about each data transformation
Common Patterns
Anti-Patterns to Avoid
β Overly long pipelines (>10 steps) β break into named sub-pipelines β Side effects in transforms β keep transforms pure (no DB writes, no external calls) β Tight coupling β don't hardcode provider-specific logic inside transforms β Missing error handling β always handle AI failures gracefully β Ignoring performance β profile expensive operations before deploying
β‘ Async Pipeline Execution
Every IAiRunnable β including full pipeline sequences β exposes runAsync(), which dispatches execution to the io-tasks virtual thread pool and returns a BoxFuture. This lets you kick off expensive AI calls without blocking the current thread.
You can also use .then() for a callback pattern:
Running Multiple Pipelines Concurrently
The real power of runAsync() is running multiple independent pipelines at the same time:
π Parallel Pipelines with aiParallel()
aiParallel() is purpose-built for fan-out scenarios: send the same input to multiple named runnables concurrently and receive all results in a single named struct.
Because AiRunnableParallel implements IAiRunnable, it composes naturally into larger pipelines with .transform() or .to():
Model Evaluation / A/B Testing
aiParallel() makes comparing outputs across providers or prompt variants trivial:
Related Pages
Building Pipelines β Methods and data flow
Transforms β Pre- and post-processing
Multi-Model Workflows β Complex multi-stage patterns
Streaming β Real-time response handling
Events Reference β Full event catalog
Last updated