Add pre and post-processing logic to your agent operations
preCompletion
: Runs before an LLM completion is generatedonCompletion
: Runs after an LLM completion is generatedpreToolExecution
: Runs before a tool is executedonToolExecution
: Runs after a tool is executedonMainFinish
: Runs after the agent has generated its last completion in the main looppostProcess
: Runs after the agent has generated its last completion in the main loop (difference between this and onMainFinish explained in code snippet)preCompletion
: The agent will not generate a completion, and the error message will be returned as if it were the agent’s response.onCompletion
: The agent will be notified of the error, and a completion will be re-generated.preToolExecution
: The agent will not execute the tool, and the error message will be returned as if it were the tool’s response.onToolExecution
: The agent will replace the tool’s response with the error message.onMainFinish
: The agent will be notified of the error, and a completion will be re-generated.postProcess
: The agent will be notified of the error, and a completion will be re-generated.critical
@middleware
decorator:
order
argument in the middleware decorator. When using order, middleware functions will be executed from least to greatest order
value.