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

# Message History In Hooks

> Access the current run's message history inside tool pre/post hooks via run_context.messages.

```python message_history_in_hooks.py theme={null}
"""
Message History In Hooks
=============================

Access the current run's message history inside tool pre/post hooks
via run_context.messages.
"""

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.run.base import RunContext
from agno.tools import FunctionCall, tool

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


def pre_hook(run_context: RunContext, fc: FunctionCall):
    msgs = run_context.messages
    count = len(msgs) if msgs else 0
    print(f"[pre-hook] {fc.function.name} - {count} messages in run")


def post_hook(run_context: RunContext, fc: FunctionCall):
    msgs = run_context.messages
    count = len(msgs) if msgs else 0
    print(
        f"[post-hook] {fc.function.name} returned '{fc.result}' - {count} messages in run"
    )


@tool(pre_hook=pre_hook, post_hook=post_hook)
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"Sunny, 72F in {city}"


agent = Agent(
    model=OpenAIChat(id="gpt-5.6-luna"),
    tools=[get_weather],
    instructions=["Use the tools to help the user."],
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    agent.print_response("What is the weather in San Francisco?")
```

## Run the Example

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

**1. Install dependencies**

```bash theme={null}
uv pip install -U agno openai
```

**2. Export your OpenAI API key**

```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"
```

**3. Run the example**

Save the code above as `message_history_in_hooks.py`, then run:

```bash theme={null}
python message_history_in_hooks.py
```

Full source: [cookbook/91\_tools/tool\_hooks/message\_history\_in\_hooks.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/91_tools/tool_hooks/message_history_in_hooks.py)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.