Why Use Background Hooks?
By default, hooks used by agents and teams in your AgentOS are in the execution path and block the response:

- Agent Evaluation: Evaluate the agent’s responses without affecting the responses themselves
- Analytics and logging: Track usage patterns without affecting response time
- Notifications: Send emails, Slack messages, or webhook calls
- External API calls: Sync data with third-party services
- Non-critical data processing: Tasks that don’t affect the response
Enabling Background Tasks
There are two ways to enable background execution for hooks:Option 1: Global Setting via AgentOS
Enable background execution for all hooks across all agents and teams (even as part of a workflow):- All agents registered with AgentOS
- All teams and their member agents (including nested teams)
- All workflows and the agents/teams within their steps
Note that pre-hooks are typically used for validation or modification of the input of a run. If you use them as background tasks, they will execute after the run has already been initiated.If you have hooks that should not run as background tasks, you should use the second option and mark only the specific hooks to run in background.
Option 2: Per-Hook Setting via Decorator
Mark specific hooks to run in background using the@hook decorator:
Background tasks require AgentOS. When running agents directly (not through AgentOS), the
@hook(run_in_background=True) decorator has no effect - hooks will run synchronously.How It Works
AgentOS uses FastAPI’s BackgroundTasks to schedule hooks for execution after the response is sent. Background tasks execute sequentially after the response is sent. If you have multiple background hooks, they run one after another.Data Isolation
When hooks run in the background, AgentOS automatically creates deep copies of:run_input- The input to the agent runrun_context- The current run contextrun_output- The output from the agent
Error Handling
Errors in background tasks don’t affect the API response (since it’s already been sent). Make sure to implement proper error handling and logging in your background hooks:Developer Resources
- View Global Background Hooks Example
- View Per-Hook Background Example
- View @hook Decorator Reference
- View Hooks Overview
- View Cookbook Examples

