chapter ten

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.

10.1 Applying Agent Middleware to Agents

10.1.1 Revisiting the Two-Layer Middleware Architecture

10.2 Walking Through Agent Middleware Layer

10.2.1 Choosing the Right Agent Middleware Type

10.2.2 Preparing Requests with SharedFunction Middleware

10.2.3 Handling Requests and Responses with Response Middleware

10.2.4 Supervising Tools Calls with FunctionCalling Middleware

10.3 Composing the Full Agent Middleware Pipeline

10.3.1 Stacking SharedFunction, Response, and FunctionCalling

10.3.2 Applying the Prepare–Handle–Invoke Middleware Pattern

10.3.3 ChatClient vs Agent: Side-by-Side Comparison

10.3.4 Exercises

10.4 Summary