> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-codex-docs-audit-20260719-0149.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Post-Hook Retry

> Automatically retry a tool call from a post-hook with RetryAgentRun.

Demonstrates retry tool call from post hook.

```python retry_tool_call_from_post_hook.py theme={null}
"""
Retry Tool Call From Post Hook
=============================

Demonstrates retry tool call from post hook.
"""

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.exceptions import RetryAgentRun
from agno.models.openai import OpenAIChat
from agno.run import RunContext
from agno.tools import FunctionCall, tool
from agno.utils.log import logger

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------


def post_hook(run_context: RunContext, fc: FunctionCall):
    logger.info(f"Post-hook: {fc.function.name}")
    logger.info(f"Arguments: {fc.arguments}")

    if run_context.session_state is None:
        run_context.session_state = {}

    shopping_list = (
        run_context.session_state.get("shopping_list", [])
        if run_context.session_state
        else []
    )
    if len(shopping_list) < 3:
        raise RetryAgentRun(
            f"Shopping list is: {shopping_list}. Minimum 3 items in the shopping list. "
            + f"Add {3 - len(shopping_list)} more items."
        )


@tool(post_hook=post_hook)
def add_item(run_context: RunContext, item: str) -> str:
    """Add an item to the shopping list."""
    if run_context.session_state is None:
        run_context.session_state = {}

    if "shopping_list" not in run_context.session_state:
        run_context.session_state["shopping_list"] = []

    run_context.session_state["shopping_list"].append(item)
    return f"The shopping list is now {run_context.session_state['shopping_list']}"


agent = Agent(
    model=OpenAIChat(id="gpt-5.2"),
    session_id="retry_tool_call_from_post_hook_session",
    db=SqliteDb(
        session_table="retry_tool_call_from_post_hook_session",
        db_file="tmp/retry_tool_call_from_post_hook.db",
    ),
    # Initialize the session state with empty shopping list
    session_state={"shopping_list": []},
    tools=[add_item],
    markdown=True,
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    agent.print_response("Add milk", stream=True)
    print(
        f"Final session state: {agent.get_session_state(session_id='retry_tool_call_from_post_hook_session')}"
    )
```

## Run the Example

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

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai sqlalchemy
    ```
  </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 `retry_tool_call_from_post_hook.py`, then run:

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

Full source: [cookbook/91\_tools/exceptions/retry\_tool\_call\_from\_post\_hook.py](https://github.com/agno-agi/agno/blob/v2.7.4/cookbook/91_tools/exceptions/retry_tool_call_from_post_hook.py)
