Skip to content
Back to skills

097 Index 152f012b

ASecurity

This section covers the core concepts of pydantic-ai-middleware.

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
devopspythonrailssecurityperformance

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 097-index_152f012b --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 097 Index 152f012b?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 097 Index 152f012b
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-097-index-152f012b/badge)](https://www.skillsdirectory.com/skills/tools-only-097-index-152f012b)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Core Concepts

This section covers the core concepts of pydantic-ai-middleware.

## Overview

pydantic-ai-middleware provides a way to intercept and modify agent behavior at various points in the execution lifecycle. This is useful for:

- **Logging** - Track agent activity
- **Security** - Block dangerous inputs or tool calls
- **Rate Limiting** - Control request frequency
- **Metrics** - Collect performance data
- **Transformations** - Modify inputs and outputs
- **Error Handling** - Catch and handle errors
- **Context Sharing** - Share data between hooks with access control

## Core Components

### AgentMiddleware

The base class for all middleware. Override the methods you need:

```python
from pydantic_ai_middleware import AgentMiddleware

class MyMiddleware(AgentMiddleware[MyDeps]):
    async def before_run(self, prompt, deps, ctx):
        # Called before agent runs
        return prompt

    async def after_run(self, prompt, output, deps, ctx):
        # Called after agent finishes
        return output
```

### MiddlewareAgent

Wraps an agent and applies middleware:

```python
from pydantic_ai_middleware import MiddlewareAgent

agent = MiddlewareAgent(
    agent=base_agent,
    middleware=[middleware1, middleware2]
)
```

### Decorators

Create middleware from simple functions:

```python
from pydantic_ai_middleware import before_run

@before_run
async def log_input(prompt, deps, ctx):
    print(f"Input: {prompt}")
    return prompt
```

## Core Concepts

- [Middleware](middleware.md) - Deep dive into creating and composing middleware
- [Hooks](hooks.md) - All available lifecycle hooks and context sharing
- [Decorators](decorators.md) - Decorator-based middleware creation

## Advanced Features

- [Middleware Chains](../advanced/middleware-chains.md) - Compose and reuse ordered pipelines
- [Parallel Execution](../advanced/parallel-execution.md) - Run middleware concurrently
- [Conditional Routing](../advanced/conditional-middleware.md) - Branch execution based on conditions
- [Async Guardrails](../advanced/async-guardrails.md) - Run guardrails alongside LLM calls
- [Config Loading](../advanced/config-loading.md) - Load middleware from JSON/YAML
- [Pipeline Spec](../advanced/pipeline-spec.md) - Build and export pipeline specs

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…