Skip to main content
This example demonstrates how to run specific hooks as background tasks using the @hook decorator, while other hooks run synchronously.
1

Create a Python file

background_hooks_decorator.py
2

Set up your virtual environment

3

Install dependencies

4

Export your OpenAI API key

5

Run the server

6

Test the endpoint

The response will be returned after log_analytics completes. Check the server logs to see log_request and send_notification executing in the background.

What Happens

  1. The agent processes the request
  2. log_analytics runs synchronously (blocks the response)
  3. The response is sent to the user
  4. log_request and send_notification run in the background
  5. The user only waits for log_analytics to complete

Comparison: Global vs Per-Hook

Use the @hook decorator when you have hooks that must complete before the response (e.g., output validation) alongside hooks that can run later (e.g., notifications).