Skip to content
Python Decorators for AI Agents: Building Reusable Tool and Middleware Patterns — Python decorator for llm tool authorization
Learn Agentic AI11 min read6 views

Python Decorators for AI Agents: Building Reusable Tool and Middleware Patterns — Python decorator for llm tool authorization

Python decorator for llm tool authorization: learn how to build reusable decorators for AI agent tools including retry logic, caching, authentication, and rate limiting using functools.wraps and parametrized patterns.

Decorators as Agent Middleware

In web frameworks, middleware wraps every request with cross-cutting logic like authentication, logging, and rate limiting. AI agent frameworks need the same patterns for tool calls. Python decorators provide exactly this — they wrap functions with reusable behavior without modifying the function itself.

Every major AI framework uses decorators extensively. The OpenAI Agents SDK uses them for tool registration. LangChain uses them for chain composition. FastAPI uses them for route definition. Mastering decorators lets you build clean, composable agent architectures.

Decorator Fundamentals

A decorator is a function that takes a function and returns a new function. The @ syntax is syntactic sugar.

flowchart LR
    INPUT(["User intent"])
    PARSE["Parse plus<br/>classify"]
    PLAN["Plan and tool<br/>selection"]
    AGENT["Agent loop<br/>LLM plus tools"]
    GUARD{"Guardrails<br/>and policy"}
    EXEC["Execute and<br/>verify result"]
    OBS[("Trace and metrics")]
    OUT(["Outcome plus<br/>next action"])
    INPUT --> PARSE --> PLAN --> AGENT --> GUARD
    GUARD -->|Pass| EXEC --> OUT
    GUARD -->|Fail| AGENT
    AGENT --> OBS
    style AGENT fill:#4f46e5,stroke:#4338ca,color:#fff
    style GUARD fill:#f59e0b,stroke:#d97706,color:#1f2937
    style OBS fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
    style OUT fill:#059669,stroke:#047857,color:#fff
import functools
import time
from typing import Callable, TypeVar, ParamSpec

P = ParamSpec("P")
T = TypeVar("T")

def log_tool_call(func: Callable[P, T]) -> Callable[P, T]:
    @functools.wraps(func)
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
        print(f"[TOOL] Calling {func.__name__} with {kwargs}")
        start = time.perf_counter()
        result = func(*args, **kwargs)
        elapsed = time.perf_counter() - start
        print(f"[TOOL] {func.__name__} completed in {elapsed:.3f}s")
        return result
    return wrapper

@log_tool_call
def web_search(query: str) -> str:
    # actual search implementation
    return f"Results for: {query}"

Always use functools.wraps. Without it, the decorated function loses its name, docstring, and type hints — which breaks tool registration in agent frameworks that inspect function metadata.

Hear it before you finish reading

Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.

Try Live Demo →

Async Decorators for Agent Tools

Most AI agent tools are async. Your decorators must handle both sync and async functions.

import asyncio
import functools
from typing import Callable, Any

def retry(max_attempts: int = 3, delay: float = 1.0):
    def decorator(func: Callable) -> Callable:
        @functools.wraps(func)
        async def async_wrapper(*args, **kwargs) -> Any:
            last_error = None
            for attempt in range(1, max_attempts + 1):
                try:
                    return await func(*args, **kwargs)
                except Exception as e:
                    last_error = e
                    if attempt < max_attempts:
                        wait = delay * (2 ** (attempt - 1))
                        print(f"Attempt {attempt} failed, retrying in {wait}s")
                        await asyncio.sleep(wait)
            raise last_error

        @functools.wraps(func)
        def sync_wrapper(*args, **kwargs) -> Any:
            last_error = None
            for attempt in range(1, max_attempts + 1):
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    last_error = e
                    if attempt < max_attempts:
                        wait = delay * (2 ** (attempt - 1))
                        time.sleep(wait)
            raise last_error

        if asyncio.iscoroutinefunction(func):
            return async_wrapper
        return sync_wrapper
    return decorator

@retry(max_attempts=3, delay=0.5)
async def call_llm(prompt: str) -> str:
    # API call that might fail
    pass

Rate Limiting Decorator

When your agent calls external APIs, rate limiting prevents quota exhaustion.

import asyncio
import functools
import time
from collections import deque

def rate_limit(calls_per_minute: int = 60):
    timestamps: deque = deque()

    def decorator(func):
        @functools.wraps(func)
        async def wrapper(*args, **kwargs):
            now = time.monotonic()
            # Remove timestamps older than 60 seconds
            while timestamps and now - timestamps[0] > 60:
                timestamps.popleft()

            if len(timestamps) >= calls_per_minute:
                sleep_time = 60 - (now - timestamps[0])
                await asyncio.sleep(sleep_time)

            timestamps.append(time.monotonic())
            return await func(*args, **kwargs)
        return wrapper
    return decorator

@rate_limit(calls_per_minute=20)
async def embed_text(text: str) -> list[float]:
    # call embedding API
    pass

Tool Registration Decorator

Build a decorator that automatically registers functions as agent tools with metadata extracted from type hints and docstrings.

import functools
import inspect
from typing import get_type_hints

TOOL_REGISTRY: dict[str, dict] = {}

def agent_tool(name: str = None, description: str = None):
    def decorator(func):
        tool_name = name or func.__name__
        tool_desc = description or func.__doc__ or "No description"
        hints = get_type_hints(func)

        params = {}
        sig = inspect.signature(func)
        for param_name, param in sig.parameters.items():
            param_type = hints.get(param_name, str).__name__
            params[param_name] = {"type": param_type}

        TOOL_REGISTRY[tool_name] = {
            "function": func,
            "description": tool_desc,
            "parameters": params,
        }

        @functools.wraps(func)
        async def wrapper(*args, **kwargs):
            return await func(*args, **kwargs)
        return wrapper
    return decorator

@agent_tool(name="search", description="Search the web")
async def web_search(query: str, max_results: int = 5) -> str:
    return f"Found {max_results} results for {query}"

FAQ

Why does functools.wraps matter for AI agent tools?

Agent frameworks like the OpenAI Agents SDK inspect function metadata — the name, docstring, and type annotations — to generate tool definitions for the LLM. Without functools.wraps, the decorator replaces this metadata with the wrapper's metadata, causing the LLM to see incorrect tool names and missing descriptions.

Still reading? Stop comparing — try CallSphere live.

CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.

Can I stack multiple decorators on one tool function?

Yes. Decorators apply bottom-up, so @retry @rate_limit @log_tool_call def func means the call passes through log_tool_call first, then rate_limit, then retry wraps the entire chain. Order matters — put retry outermost so it retries the entire decorated pipeline.

How do I test decorated functions in isolation?

Access the original function via func.__wrapped__ (available when you use functools.wraps). This lets you unit test the core logic without triggering retry delays, rate limits, or logging side effects.


#Python #Decorators #DesignPatterns #AIAgents #AgenticAI #LearnAI #AIEngineering

Background and Key Concepts: Python decorator for llm tool authorization

This guide is written for engineers and operators evaluating python decorator for llm tool authorization in real production systems. Python decorator for llm tool authorization sits alongside api key in the daily work of teams shipping production AI. The notes below give a plain-language reference for terms used throughout the article.

  • api key — referenced in this guide when discussing python decorator for llm tool authorization.

For teams that want to ship python decorator for llm tool authorization in voice and chat agents this quarter, CallSphere runs 37 agents and 90+ function tools across 6 verticals on a single dashboard. Start a 14-day trial, see live demo agents, or compare tiers on /pricing.

Share

Try CallSphere AI Voice Agents

See how AI voice agents work for your industry. Live demo available -- no signup required.

Related Articles You May Like

AI Agents

Personal AI Assistant: How to Pick One for Business in 2026

A founder's guide to the personal AI assistant market: best AI assistant apps, business-grade options, and how CallSphere's voice agent fits in.

AI Agents

Free AI Agents in 2026: When Free Wins and When It Costs You

A founder's guide to free AI agents, low-code AI agent builders, and how to know when you should pay for a real platform like CallSphere.

Agentic AI

Graphiti: How Temporal Knowledge Graphs Give AI Voice Agents Persistent Memory (2026 Guide)

Graphiti is the open-source temporal knowledge graph for AI agents in 2026. Learn how bi-temporal memory beats vector RAG for voice agents and long-running LLMs.

AI Agents

Chatbot App vs ChatGPT: What's the Difference, and Which Do I Need?

Chatbot app vs ChatGPT in 2026: a founder's clear take on the difference, when to use which, and how a real AI chatbot app development works.

HVAC

Building an HVAC After-Hours Emergency Escalation System: A Complete Engineering Guide

How we built a fault-tolerant HVAC emergency triage and tech-dispatch platform on Kubernetes — three-tier CQRS, 11 micro-agents on the OpenAI Agents SDK + LangGraph, NATS JetStream, DTMF/SMS/WebSocket acceptance, circuit breakers, and an evaluation pipeline that catches regressions before they wake a tech at 3 AM.

Comparisons

Desktop AI Agents in 2026: Project Arc, Claude Cowork, OpenAI Agents Compared

The 2026 desktop AI agent landscape — ServiceNow Project Arc, Anthropic Claude offerings, OpenAI agents, and Google Mariner. A buyer's map.