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

# middleware • AI Agent SDK

> Middleware System for PraisonAI Agents.

# middleware

<Badge color="blue">AI Agent</Badge>

Middleware System for PraisonAI Agents.

Provides wrap\_model\_call and wrap\_tool\_call patterns for intercepting
and modifying agent behavior at model/tool call boundaries.

Zero Performance Impact:

* All middleware is optional
* Empty middleware chain is O(1) fast path
* No overhead when middleware not registered

Usage:
from praisonaiagents.hooks import before\_model, wrap\_model\_call, wrap\_tool\_call

@before\_model
def add\_context(request):
request.messages.append(\{"role": "system", "content": "Extra"})
return request

@wrap\_model\_call
def retry\_on\_error(request, call\_next):
for \_ in range(3):
try:
return call\_next(request)
except Exception:
pass
raise RuntimeError("All retries failed")

agent = Agent(name="Test", hooks=\[add\_context, retry\_on\_error])

## Import

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents.hooks import middleware
```

## Classes

<CardGroup cols={2}>
  <Card title="InvocationContext" icon="brackets-curly" href="../classes/InvocationContext">
    Context passed through middleware chain.
  </Card>

  <Card title="ModelRequest" icon="brackets-curly" href="../classes/ModelRequest">
    Request data for model calls.
  </Card>

  <Card title="ModelResponse" icon="brackets-curly" href="../classes/ModelResponse">
    Response data from model calls.
  </Card>

  <Card title="ToolRequest" icon="brackets-curly" href="../classes/ToolRequest">
    Request data for tool calls.
  </Card>

  <Card title="ToolResponse" icon="brackets-curly" href="../classes/ToolResponse">
    Response data from tool calls.
  </Card>

  <Card title="MiddlewareChain" icon="brackets-curly" href="../classes/MiddlewareChain">
    Synchronous middleware chain executor.
  </Card>

  <Card title="AsyncMiddlewareChain" icon="brackets-curly" href="../classes/AsyncMiddlewareChain">
    Asynchronous middleware chain executor.
  </Card>

  <Card title="MiddlewareManager" icon="brackets-curly" href="../classes/MiddlewareManager">
    Manages middleware for an agent.
  </Card>
</CardGroup>

## Functions

<CardGroup cols={2}>
  <Card title="before_model()" icon="function" href="../functions/before_model">
    Decorator to mark a function as a before\_model hook.
  </Card>

  <Card title="after_model()" icon="function" href="../functions/after_model">
    Decorator to mark a function as an after\_model hook.
  </Card>

  <Card title="wrap_model_call()" icon="function" href="../functions/wrap_model_call">
    Decorator to mark a function as a wrap\_model\_call middleware.
  </Card>

  <Card title="before_tool()" icon="function" href="../functions/before_tool">
    Decorator to mark a function as a before\_tool hook.
  </Card>

  <Card title="after_tool()" icon="function" href="../functions/after_tool">
    Decorator to mark a function as an after\_tool hook.
  </Card>

  <Card title="wrap_tool_call()" icon="function" href="../functions/wrap_tool_call">
    Decorator to mark a function as a wrap\_tool\_call middleware.
  </Card>

  <Card title="get_hook_type()" icon="function" href="../functions/get_hook_type">
    Get the hook type of a decorated function.
  </Card>

  <Card title="is_middleware()" icon="function" href="../functions/is_middleware">
    Check if a function is a middleware (wrap\_\* type).
  </Card>

  <Card title="categorize_hooks()" icon="function" href="../functions/categorize_hooks">
    Categorize hooks by their type.
  </Card>
</CardGroup>

### Constants

| Name | Value          |
| ---- | -------------- |
| `T`  | `TypeVar('T')` |

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Middleware Feature" icon="layer-group" href="/docs/features/middleware" />
</CardGroup>
