middleware

Middleware guidance for connecting files, reusable skills, sub-agents, memory, context shortening, approvals, and usage tracking to a LangGraph agent. LangGraph is a framework for building agents as connected steps or states.

In plain words
What is it for?
Configuring agent middleware, exposing approved tools and skills, managing sub-agents and memory, shortening oversized context, retrying model failures, and tracking tasks.
Why use it?
It explains which capabilities are prepared before the agent starts and which checks still belong at tool-execution time, helping prevent incorrect access decisions.

Agent

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/xerrors/yuxi/middleware
Clone the repo
git clone --depth 1 https://github.com/xerrors/Yuxi
Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,459 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.01459
Opus 5 $0.00000 $0.00730
Sonnet 5 $0.00000 $0.00292
Haiku 4.5 $0.00000 $0.00146

Measured 2d ago against content hash 477950f7bb9c, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

middleware scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

docs/agents/middleware.md · 77 lines

How it starts

The opening of the file, as written. The whole thing — 77 lines — stays where its author put it; the contents beside it link to each section on GitHub.

中间件

中间件把文件、Skills、子智能体、上下文压缩、审批和用量统计接到 LangGraph Agent。它们在模型调用、工具调用或 state 更新的边界运行,让不同 Agent 复用同一套能力。

内置 ChatbotAgentSubAgentBackend 都在 get_graph() 中组装中间件。Graph 创建前,系统先完成用户资源和权限的归一化;中间件不应绕过这一步重新决定授权。

Graph 创建前的准备

prepare_agent_runtime_context 会根据当前用户和 Agent 配置:

  • 过滤内置工具、知识库、MCP、Skills 和子智能体;
  • 生成 _visible_knowledge_bases
  • 展开 Skill 依赖,生成 _effective_skill_slugs_runtime_skills
  • 使用系统默认模型补齐空的模型配置。

随后,工具解析器准备可执行工具,build_prompt_with_context 生成系统提示词,Agent 再创建 Graph。工具执行时仍需检查具体目标,准备阶段的资源快照不是授权替代品。

内置中间件顺序

ChatbotAgent 的常见顺序如下;可选项只在对应能力启用时加入:

顺序 中间件 作用
1 SteerMiddleware 在安全边界发现待接替请求
2 create_agent_filesystem_middleware 提供 Workdir、User Data、Skills 文件后端,并卸载过大的工具结果
3 SkillsMiddleware 注入 Skill 说明,按激活状态开放依赖
4 YuxiMemoryMiddleware Memory 开关开启且 MEMORY.md 有内容时,注入用户记忆并提供受限工具
5 YuxiSubAgentMiddleware 主智能体有可见子智能体时提供 task 和生命周期工具
6 YuxiSummarizationMiddleware 在上下文过大时执行 L1/L2 压缩
7 TodoListMiddleware 保存待办,供状态面板展示
8 PatchToolCallsMiddleware 修正部分工具调用消息形态
9 ModelRetryMiddleware 按配置重试模型调用失败
10 ImageInputCompatibilityMiddleware 桥接工具读取图片与模型输入格式;必要时回退 OCR
11 TokenUsageMiddleware 记录近似上下文和主模型实际用量
12 工具审批 middleware 默认模式下拦截写文件、编辑文件和执行命令

SubAgentBackend 复用文件、Skills、Summary、待办、重试和用量等能力,但不挂载子智能体 middleware,并过滤不适合子智能体的敏感或交互工具。

Skills 和知识库

Skills middleware 将 Skill 说明按模型请求注入:预加载 Skill 从首轮开放依赖,普通 Skill 在模型读取对应 SKILL.md 后激活,再开放声明的工具和 MCP。

知识库能力由内置 knowledge-base Skill 提供。它的工具是否注册、模型是否可见、参数是否能访问目标知识库分别由工具组装、Skill 激活和知识库权限检查负责。完整链路见工具系统知识库机制详解

文件和附件

附件确认后写入当前 Project Workdir。每次 Run 会把线程历史附件的文件名和实时路径加入本轮用户消息,让模型按需调用 read_file;持久化 Message 仍保存原始文本,不会把这段模型专用路径混进用户可见消息。

普通 Agent 和子 Agent 使用根 Conversation 的同一个 runtime_scope_id 和 Workdir。子 Agent 的 child thread 只隔离 LangGraph checkpoint,不隔离共享文件。Viewer、附件和 artifact API 直接访问 UserWorkspace 的持久文件,不需要创建 file-bridge Sandbox。

Read the full file on GitHub · 77 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 77 lines · 0 tokens per session scan A 477950f7bb9c

Subscribe to this mod's changes

middleware is an agent published in the GitHub repository xerrors/Yuxi (6,591 stars, last pushed 2d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,459 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.