Skip to main content
Run non-guardrail hooks as FastAPI background tasks by enabling run_hooks_in_background at the AgentOS level. Guardrail hooks remain synchronous so they can accept or reject the run before processing continues.
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. Non-guardrail pre-hooks and post-hooks run in the background
  4. Guardrail hooks run synchronously and can block the request
  5. The user doesn’t have to wait for background tasks to complete
With run_hooks_in_background=True on AgentOS, non-guardrail hooks for all agents run in the background. Guardrail hooks remain synchronous. Use the @hook decorator for per-hook control.