> ## Documentation Index
> Fetch the complete documentation index at: https://phidatainc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Background Hooks Team

> Attach a non-blocking post-hook to a researcher/writer Team that logs run id and content length after the response is sent.

Use background hooks with a Team. Background hooks execute after the API response is sent, making them non-blocking.

```python background_hooks_team.py theme={null}
"""
Example: Background Hooks with Teams in AgentOS

This example demonstrates how to use background hooks with a Team.
Background hooks execute after the API response is sent, making them non-blocking.
"""

import asyncio

from agno.agent import Agent
from agno.db.sqlite import AsyncSqliteDb
from agno.hooks.decorator import hook
from agno.models.openai import OpenAIChat
from agno.os import AgentOS
from agno.run.team import TeamRunOutput
from agno.team import Team

# ---------------------------------------------------------------------------
# Create Example
# ---------------------------------------------------------------------------


@hook(run_in_background=True)
async def log_team_result(run_output: TeamRunOutput, team: Team) -> None:
    """
    Background post-hook that logs team execution results.
    Runs after the response is sent to the user.
    """
    print(f"[Background Hook] Team '{team.name}' completed run: {run_output.run_id}")
    print(f"[Background Hook] Content length: {len(str(run_output.content))} chars")

    # Simulate async work (e.g., storing metrics)
    await asyncio.sleep(2)
    print("[Background Hook] Team metrics logged successfully!")


# Create team members
researcher = Agent(
    name="Researcher",
    model=OpenAIChat(id="gpt-5.2"),
    instructions="You research topics and provide factual information.",
)

writer = Agent(
    name="Writer",
    model=OpenAIChat(id="gpt-5.2"),
    instructions="You write clear, engaging content based on research.",
)

# Create the team with background hooks
content_team = Team(
    id="content-team",
    name="ContentTeam",
    model=OpenAIChat(id="gpt-5.2"),
    members=[researcher, writer],
    instructions="Coordinate between researcher and writer to create content.",
    db=AsyncSqliteDb(db_file="tmp/team.db"),
    post_hooks=[log_team_result],
    markdown=True,
)

# Create AgentOS with background hooks enabled
agent_os = AgentOS(
    teams=[content_team],
    run_hooks_in_background=True,
)

app = agent_os.get_app()

# Example request:
# curl -X POST http://localhost:7777/teams/content-team/runs \
#   -F "message=Write a short paragraph about Python" \
#   -F "stream=false"

# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    agent_os.serve(app="background_hooks_team:app", port=7777, reload=True)
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U "agno[os]" aiosqlite openai
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `background_hooks_team.py`, then run:

    ```bash theme={null}
    python background_hooks_team.py
    ```
  </Step>
</Steps>

Full source: [cookbook/05\_agent\_os/background\_tasks/background\_hooks\_team.py](https://github.com/agno-agi/agno/blob/main/cookbook/05_agent_os/background_tasks/background_hooks_team.py)
