Skip to main content
This example demonstrates how to run all hooks as FastAPI background tasks by enabling run_hooks_in_background at the AgentOS level.
1

Create a Python file

background_hooks_global.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 immediately. Check the server logs to see the background hooks executing after the response is sent.

What Happens

  1. The agent processes the request
  2. The response is sent immediately to the user
  3. All pre-hooks and post-hooks run in the background
  4. The user doesn’t have to wait for these tasks to complete
With run_hooks_in_background=True on AgentOS, all hooks for all agents run in the background. Use the @hook decorator for more granular control over which hooks run in the background.