- Security guardrails (e.g. PII detection, prompt injection defense)
- Input validation
- Output validation
- Data preprocessing (e.g. normalizing input data)
- Data postprocessing (e.g. adding additional context to the output)
- Logging (e.g. logging the duration of the run)
- Debugging (e.g. debugging the run)
When Hooks Are Triggered
Hooks execute at specific points in the Agent/Team run lifecycle:- Pre-hooks: Execute immediately after the current session is loaded, before any processing begins. They run before the model context is prepared and before any LLM execution begins, i.e. any modifications to the input, session state, or dependencies will be applied before LLM execution.
- Post-hooks: Execute after the Agent/Team generates a response and the output is prepared, but before the response is returned to the user. In streaming responses, they run after each chunk of the response is generated.
Pre-hooks
Pre-hooks execute at the very beginning of your Agent run, giving you complete control over what reaches the LLM. They’re perfect for implementing input validation, security checks, or any data preprocessing against the input your Agent receives.Common Use Cases
Security Guardrails- Detect and prevent PII (Personally Identifiable Information) from reaching the LLM.
- Defend against prompt injection and jailbreak attempts.
- Filter NSFW or inappropriate content.
- See the Guardrails documentation for more details.
- Validate format, length, content or any other property of the input.
- Remove or mask sensitive information.
- Normalize input data.
- Transform input format or structure.
- Enrich input with additional context.
- Apply any other business logic before sending the input to the LLM.
Basic Example
Let’s create a simple pre-hook that validates the input length and raises an error if it’s too long:Pre-hook Parameters
Pre-hooks run automatically during the Agent run and receive the following parameters:run_input: The input to the Agent run that can be validated or modifiedagent: Reference to the Agent instancesession: The current agent sessionrun_context: The current run context. See the Run Context reference.debug_mode: Whether debug mode is enabled (optional)
Post-hooks
Post-hooks execute after your Agent generates a response, allowing you to validate, transform, or enrich the output before it reaches the user. They’re perfect for output filtering, compliance checks, response enrichment, or any other output transformation you need.Common Use Cases
Output Validation- Validate response format, length, and content quality.
- Remove sensitive or inappropriate information from responses.
- Ensure compliance with business rules and regulations.
- Add metadata or additional context to responses.
- Transform output format for different clients or use cases.
- Enrich responses with additional data or formatting.
Basic Example
Let’s create a simple post-hook that validates the output length and raises an error if it’s too long:Post-hook Parameters
Post-hooks run automatically during the Agent run and receive the following parameters:run_output: The output from the Agent run that can be validated or modifiedagent: Reference to the Agent instancesession: The current agent sessionrun_context: The current run context. See the Run Context reference.user_id: The user ID for the run (optional)debug_mode: Whether debug mode is enabled (optional)
Guardrails
A popular use case for hooks are Guardrails: built-in safeguards for your Agents. You can learn more about them in the Guardrails section.Developer Resources
- View Agent Examples
- View Team Examples
- View Agent Cookbook
- View Team Cookbook