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

# Middleware System

> Hook-based middleware for model and tool calls

## Overview

The middleware system provides before/after hooks and wrap decorators for intercepting model and tool calls.

## Quick Start

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent, tool
from praisonaiagents.hooks import before_model, wrap_tool_call

@before_model
def add_context(request):
    print("Before model call")
    return request

@wrap_tool_call
def retry_on_error(tool_call, call_next):
    try:
        return call_next(tool_call)
    except Exception:
        return call_next(tool_call)  # Retry once

agent = Agent(
    name="Bot",
    instructions="Helper",
    hooks=[add_context, retry_on_error]
)
```

## Available Decorators

| Decorator          | Purpose                    |
| ------------------ | -------------------------- |
| `@before_model`    | Run before LLM call        |
| `@after_model`     | Run after LLM call         |
| `@wrap_model_call` | Wrap entire LLM call       |
| `@before_tool`     | Run before tool execution  |
| `@after_tool`      | Run after tool execution   |
| `@wrap_tool_call`  | Wrap entire tool execution |

## Types

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents.hooks import (
    InvocationContext,
    ModelRequest,
    ModelResponse,
    ToolRequest,
    ToolResponse
)
```

## Zero Overhead

When no hooks are registered, middleware adds zero overhead to execution.
