10 Building enterprise-ready agents with Agent middleware
This chapter covers
- Agent middleware as the per-agent companion to ChatClient middleware
- Agent middleware architecture: session context and agent identity
- Three Agent middleware types: SharedFunction, Response, and FunctionCalling
- Composing a complete Agent middleware pipeline
Robby’s problems at the ChatClient layer were only half the story. In production we also discovered that different agents, users, and missions needed different rules: who is allowed to move hardware, which missions require approval, how to tag responses with session and mission metadata, and when to persist sanitization instead of re‑applying it on every call. Agent middleware is the higher‑level pipeline that wraps an agent run, with access to agent identity and session state, so we can enforce these per‑agent and per‑session guardrails without cluttering the agent code. That extra layer of policy (e.g., per agent, per user, per mission, but centrally enforced) turns Robby from a clever prototype into an enterprise‑ready fleet that still relies on the shared, organization‑wide safeguards from the ChatClient layer.