惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 【当耐特】
A
About on SuperTechFans
Last Week in AI
Last Week in AI
雷峰网
雷峰网
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements

Stories by HARSHA J S on Medium

Mastering LangChain 1.2: Part 13 — Agentic RAG: Why Your AI Needs to Decide When to Google Mastering LangChain 1.2: Part 12 — The Elephant’s Memory: Persistent AI Agents that Never Forget Mastering LangChain 1.2: Part 11 — Custom Middleware Hooks: Orchestrating the AI Lifecycle Mastering LangChain 1.2: Part 10 — Human-in-the-Loop: Adding an “Approval” Button to Your AI Agents Mastering LangChain 1.2: Part 9 — Structured Outputs: Turning Messy Chat into Clean Data Mastering LangChain 1.2: Mastering LangChain 1.2: Mastering LangChain 1.2: Part 6 — Dynamic Tool Security: The “Triple-Lock” Guardrail Mastering LangChain 1.2: Part 5 — Dynamic Model Routing: Escalating to Senior AI in Emergencies
Mastering LangChain 1.2: Part 4 — Scaling with Middleware...
HARSHA J S · 2026-03-21 · via Stories by HARSHA J S on Medium

HARSHA J S

In the previous parts of this series, we built a Unified Model Interface, added Tools, and created Autonomous Agents. But as your AI applications move from “cool demo” to “production tool,” you’ll hit a wall: The Context Window.

LLMs have limits. If you feed an agent thousands of lines of logs, it becomes slow, expensive, and eventually, it crashes because it can’t “remember” any more. This is where Middleware saves the day.

What is Middleware in LangChain?

Think of Middleware as a protective layer that sits between your User and your LLM “Brain.” It intercepts messages before the model sees them and after the model responds.

Middleware allows you to:

  1. Summarize: Compress long histories so you never hit token limits.
  2. Protect: Detect and redact PII (Personally Identifiable Information).
  3. Optimize: Add retries for failed tool calls or fall back to cheaper models.
  4. Guardrail: Prevent infinite loops or excessive costs.

1. Solving the “Memory Problem” with Summarization

The most common use case for middleware is preventing “Context Overflow.” If a user has a long debugging session with your agent, the history grows exponentially.

Instead of just deleting old messages (losing context), we use SummarizationMiddleware. This tool watches the conversation and, when a threshold is met, uses a second "Summarizer LLM" to compress the old history into a concise paragraph while keeping the most recent messages untouched.

from langchain.agents.middleware import SummarizationMiddleware

# Configure the compression logic
log_compressor = SummarizationMiddleware(
model="ollama:phi4-mini", # A fast, cheap model for summarizing
trigger=("messages", 10), # Kick in after 10 messages
keep=("messages", 3), # Always keep the last 3 messages raw
)

2. Implementing the “Log Compressor”

Let’s see how this works in a real SRE (Site Reliability Engineering) scenario. Imagine an agent analyzing a massive stack trace.

from langchain.agents import create_agent

# Pass the middleware during agent creation
agent = create_agent(
model=main_model,
tools=[analyze_stack_trace],
middleware=[log_compressor] # The secret sauce
)

By adding this one line, your agent becomes infinitely more durable. Even if the conversation goes on for 100 turns, the middleware will continuously “crunch” the oldest parts of the chat, providing the model with a “The story so far…” summary instead of 100 individual messages.

3. Why This Is Better Than “Windowing”

Many developers use a simple “Window” approach, where they only send the last 5 messages to the LLM. The problem? The model “forgets” why the conversation started.

Summarization Middleware is smarter:

  • Context Preservation: The model still knows the intent of the first message because it’s in the summary.
  • Token Efficiency: You only pay for a few hundred tokens of summary instead of thousands of tokens of raw chat history.
  • Developer Experience: It happens automatically “under the hood.” You just invoke the agent as usual.

Conclusion: Engineering for Longevity

Building an AI agent that works once is easy. Building an agent that can handle a 2-hour debugging session without getting confused or crashing is hard.

Middleware transforms your agents from fragile scripts into production-ready software. By delegating tasks like summarization, PII detection, and error handling to the middleware layer, you allow your “Main Model” to focus on what it does best: solving the user’s problem.

💬 What do you think?
Drop your thoughts, questions, or suggestions in the comments below!

Check out my YouTube channel for more exciting content! [YouTube Channel Link — Harsha Selvi]

Disclaimer: This text has been rephrased using AI tools, and some parts are derived from various sources to provide a comprehensive overview.

#AI #LangChain #SoftwareEngineering #Python #LLM #AIEngineering #MachineLearning