Skip to main content

Overview

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

Quick Start

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

DecoratorPurpose
@before_modelRun before LLM call
@after_modelRun after LLM call
@wrap_model_callWrap entire LLM call
@before_toolRun before tool execution
@after_toolRun after tool execution
@wrap_tool_callWrap entire tool execution

Types

from praisonaiagents.hooks import (
    InvocationContext,
    ModelRequest,
    ModelResponse,
    ToolRequest,
    ToolResponse
)

Zero Overhead

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