SitePoint16 min readtutorialintermediate
Build a Typed Context Compaction Gate for AI Agents
Summary
A step‑by‑step tutorial showing how to build a typed context‑compaction gate for AI agents in TypeScript using LangChain and Zod. It defines a discriminated‑union schema for keep/summarize/discard actions, implements a fast gpt‑4o‑mini classifier, validates decisions at runtime, and wires the gate as middleware in an agent loop, with testing and production‑grade tuning advice.
- Use a Zod discriminated‑union to enforce a strict decision schema (keep, summarize, discard) for each tool output.
- Run a cheap gpt‑4o‑mini classifier with structured output to decide the compaction action before the main LLM sees the data.
- Validate classifier responses with `safeParse` and fall back to “keep” on malformed output to avoid accidental data loss.
- Sample large tool outputs (head/tail) to keep classifier cost low while preserving critical information.
Long‑running AI agents that call many external tools quickly blow past token limits (800‑2,000 tokens per call). Naïve truncation discards crucial tail data; blanket summarization adds extra LLM calls. A typed compaction gate lets a cheap classifier prune or compress tool outputs before they hit th…
6/10





