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

# Agent Middleware

## Overview

Control and customize agent execution at every step.

Middleware provides a more fine-grained way to control the internal operations of agents. Middleware is suitable for the following scenarios:

* Track agent behavior through logging, analytics, and debugging.
* Transform prompts, tool selection, and output formats.
* Add retry, fallback, and early termination logic.
* Apply rate limiting, guardrails, and PII detection.

## Agent Loop

The core agent loop involves calling the model, having the model choose which tools to execute, and then ending when no more tools need to be called:

![Core agent loop diagram](https://mintcdn.com/xpertai/KjFE_c3zPYs4Z9GJ/public/img/ai/xpert/core_agent_loop.avif?fit=max\&auto=format\&n=KjFE_c3zPYs4Z9GJ\&q=85\&s=28f76e9cf0f026757990c1f70915c1ba)

Middleware exposes hooks before and after each step:

![Middleware flow diagram](https://mintcdn.com/xpertai/KjFE_c3zPYs4Z9GJ/public/img/ai/xpert/middleware_final.avif?fit=max\&auto=format\&n=KjFE_c3zPYs4Z9GJ\&q=85\&s=aac1bfb54ecd02ff52e293e6e60814c7)

## Middleware Nodes

In XpertAI, agent nodes support attaching multiple middleware to an Agent in a visual way. Users can manage the order and configuration of middleware in a unified manner in the right panel, achieving the same pluggable Middleware architecture as LangChain.

![Middleware node config](https://mintcdn.com/xpertai/KjFE_c3zPYs4Z9GJ/public/img/ai/xpert/agent-middleware-nodes.png?fit=max\&auto=format\&n=KjFE_c3zPYs4Z9GJ\&q=85\&s=8383939aabe351cd01ada3468b3676bb)

Each middleware has its own independent configuration panel, where you can adjust its execution parameters and behavior, forming a visual, configurable, and extensible agent execution pipeline.

![Middleware node config](https://mintcdn.com/xpertai/KjFE_c3zPYs4Z9GJ/public/img/ai/xpert/middleware-node-config.png?fit=max\&auto=format\&n=KjFE_c3zPYs4Z9GJ\&q=85\&s=dbcd182086fd29603746e650376d7fd8)

Click the graph button to open the digital expert agent, and you can see the execution order and relationship between middleware and agents:

![Middleware graph](https://mintcdn.com/xpertai/KjFE_c3zPYs4Z9GJ/public/img/ai/xpert/agent-middleware-graph.png?fit=max\&auto=format\&n=KjFE_c3zPYs4Z9GJ\&q=85\&s=80e6a0932c29823ac941029e31a1d5a5)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.