ag2-middleware
Skill12 starsChanged 9 days agoApache-2.0View on GitHub
Intercept the AG2 agent loop with `BaseMiddleware` hooks — `on_turn`, `on_llm_call`, `on_tool_execution`, `on_human_input` — registered via `Agent(middleware=[...])` or `ask(middleware=[...])`. Custom subclasses are passed as the CLASS or `Middleware(Cls, **kwargs)`, never an instance. Covers getting per-turn state out (shared object via kwargs, `reply.context.variables`), ordering, `ConditionalMiddleware`, `statusbar_middleware` (fresh status each model call), `describe()` / `DescribableMiddleware`, `ScopedMiddlewareFactory`, and assembly policies via `Middleware(AssemblerMiddleware, policies=[...])` + `HaltCheckMiddleware`. Built-ins: `LoggingMiddleware`, `RetryMiddleware`, `HistoryLimiter`, `TokenLimiter`, `MetricsMiddleware`, `TelemetryMiddleware` (see `ag2-telemetry`), `approval_required` (see `ag2-hitl`). Use for retry, logging, timing, history trimming, request mutation, tool auditing, guardrails, rate limiting.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- ag2ai/build-with-ag2their own version, first seen Oct 9, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- ag2ai/build-with-ag2★ 252same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ag2-a2aSkill
- ag2-acpSkill
- ag2-add-custom-toolSkill
- ag2-ag-uiSkill
- ag2-eval-comparisonSkill
- ag2-evaluationSkill
- ag2-hitlSkill
- ag2-knowledge-and-memorySkill
- ag2-liveSkill
- ag2-mcpSkill
- ag2-multimodal-inputSkill
- ag2-network-discussionSkill
- ag2-network-governanceSkill
- ag2-network-quickstartSkill
- ag2-network-tools-and-viewsSkill
- ag2-network-workflowSkill
- ag2-observers-and-alertsSkill
- ag2-overviewSkill
- ag2-quickstartSkill
- ag2-shell-toolSkill
- ag2-structured-outputSkill
- ag2-subagent-delegationSkill
- ag2-telemetrySkill
- ag2-testingSkill
- ag2-use-builtin-toolsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 37
Call-level middleware (agent.ask(..., middleware=[...]), reply.ask(...)) is appended after the agent's list.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Middleware
When to use
Cross-cutting behaviour that should apply to every turn without changing the agent, model client, or tools: logging/timing, retries, history trimming, request mutation, tool auditing/guardrails, rate limiting, HITL auditing.
Four hooks
| Hook | Wraps | Returns |
on_turn(call_next, event, context) | the whole ask() turn | ModelResponse |
on_llm_call(call_next, events, context) | each model call (history in events) | ModelResponse |
on_tool_execution(call_next, event, context) | each tool call (ToolCallEvent) | ToolResultType |
on_human_input(call_next, event, context) | each context.input() / approval | HumanMessage |
A new instance is created per turn (__init__(self, event, context, **your_kwargs)), so self state dies with the turn.
Registration — the rule that trips people up
middleware=[...] takes factories: callables (event, context) -> BaseMiddleware.
| What you have | Pass it as |
Built-in (LoggingMiddleware, RetryMiddleware, TokenLimiter, ...) | an instance: RetryMiddleware(max_retries=2) (built-ins are factories) |
Your BaseMiddleware subclass, no extra args | the class: middleware=[MyMiddleware] |
Your subclass with extra __init__ args | Middleware(MyMiddleware, key=value) |
MyMiddleware() / MyMiddleware(event, ctx) | wrong → TypeError: 'MyMiddleware' object is not callable |
Call-level middleware (agent.ask(..., middleware=[...]), reply.ask(...)) is appended after the agent's list.
Recipe — custom middleware + getting state out
import time
from collections.abc import Sequence
from ag2 import Agent, Context, tool
from ag2.config import OpenAIResponsesConfig
from ag2.events import BaseEvent, ModelResponse
from ag2.middleware import BaseMiddleware, LLMCall, Middleware, RetryMiddleware
class CallTimer(BaseMiddleware):
def __init__(self, event: BaseEvent, context: Context, stats: list[float]) -> None:
super().__init__(event, context)
self.stats = stats # shared object outlives the per-turn instance
async def on_llm_call(self, call_next: LLMCall, events: Sequence[BaseEvent], context: Context) -> ModelResponse:
start = time.perf_counter()
try:
return await call_next(events, context)
finally:
self.stats.append(time.perf_counter() - start)
class CountCalls(BaseMiddleware): # no extra args -> pass the class
async def on_llm_call(self, call_next, events, context):
context.variables["llm_calls"] = context.variables.get("llm_calls", 0) + 1
return await call_next(events, context)
@tool
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
durations: list[float] = []
agent = Agent(
"calc",
config=OpenAIResponsesConfig(model="gpt-4.1-mini"),
tools=[add],
middleware=[Middleware(CallTimer, stats=durations), CountCalls, RetryMiddleware(max_retries=2)],
)
reply = await agent.ask("What is 17+25? Use the tool.")
print(len(durations), reply.context.variables["llm_calls"]) # 2 2 (tool round-trip)Two ways out of a per-turn instance: pass a mutable object via Middleware(Cls, obj=...), or write to context.variables and read reply.context.variables (the dict you passed to ask(variables=...) is copied, not mutated). For a reusable component with its own state, write a factory class (__call__(self, event, context) -> BaseMiddleware) like the built-ins do.
Ordering
[A, B, C] enters A → B → C and unwinds C → B → A (A is outermost). Put retry outside anything whose work should be retried; put history trimming closest to the model; put statusbar_middleware before RetryMiddleware to reuse one snapshot across attempts.
Built-ins and helpers (all from ag2.middleware)
| Name | Use |
LoggingMiddleware(logger=None) | log turns, LLM calls, tools |
RetryMiddleware(max_retries=3, retry_on=(Exception,)) | retry LLM calls (not after a chunk/tool call was already published) |
HistoryLimiter(max_events=N) / TokenLimiter(max_tokens=N, chars_per_token=4) | trim history before each call (targets, not hard caps) |
MetricsMiddleware(registry=CollectorRegistry()) | Prometheus metrics, ag2[metrics]; one per registry |
TelemetryMiddleware(...) | OpenTelemetry spans — ag2-telemetry |
ConditionalMiddleware(factory, condition=ToolCallEvent.name == "x") | gate any middleware by event condition |
statusbar_middleware | append fresh Input parts to every model request |
HaltCheckMiddleware | stop model calls after a HaltEvent (with AlertPolicy) |
approval_required() | tool middleware for HITL — ag2-hitl |
Status bar (1.3.0)
from typing import Annotated
from ag2 import DataInput, Variable
from ag2.middleware import statusbar_middleware
@statusbar_middleware
def budget(calls: Annotated[int, Variable("llm_calls", default=0)]) -> DataInput | None:
return DataInput({"llm_calls_so_far": calls, "limit": 5})
agent = Agent(..., middleware=[budget]) # the decorated function is already a factoryCallback (sync/async, DI-enabled) returns Input, a list of Input, or None; parts are not stored in history unless persist=True.
Assembly policies (replaces deprecated Agent(assembly=[...]))
from ag2.assembly import AssemblerMiddleware
from ag2.middleware import HaltCheckMiddleware, Middleware
from ag2.policies import AlertPolicy, SlidingWindowPolicy
middleware = [
Middleware(AssemblerMiddleware, policies=[AlertPolicy(), SlidingWindowPolicy(max_events=50)]),
Middleware(HaltCheckMiddleware), # right after, so FATAL alerts stop the call
]Going deeper
references/builtin_middleware.md— built-in parameters,describe()/DescribableMiddleware,ScopedMiddlewareFactory, stacking order.- Upstream:
website/docs/user-guide/middleware.mdx,status_bars.mdx,advanced/assembly.mdx,tools/tool_middleware.mdx,metrics.mdx. - One-tool behaviour → tool middleware (
@tool(middleware=[fn]), plainasync def fn(call_next, event, context)) —ag2-add-custom-tool. Agent middleware runs outside tool middleware.
Common pitfalls
- Passing an instance of your subclass —
TypeError: '...' object is not callable. Pass the class orMiddleware(Cls, **kw). - Passing the class when
__init__needs kwargs —TypeError: ... missing 1 required positional argument. Wrap withMiddleware(...). - Storing results on
self— gone after the turn; use a shared object orcontext.variables. - Per-call middleware replacing agent middleware — it is appended; both run.
- Using
assembly=— deprecated (removed 1.4.0); useAssemblerMiddlewareas above. - Branching on
event.nameinon_tool_executionfor one tool — use tool middleware orConditionalMiddleware.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

