We build Argentic, so weigh what we say about it. LangChain's documentation is quoted as we read it on 26 November 2026.
Context rot, which we call context degradation, is an agent getting less accurate as its run grows. LangChain's own docs say the cause plainly: "most LLMs still perform poorly over long contexts," and they get "distracted" by stale or off-topic content. So the fix is deciding what the model reads on each call. If what you see is a forgotten goal, a loop or a contradicted decision and you are not sure it is context rot, start with the symptom guide. This guide takes the steps in the order that costs least, and the last ones change who builds the prompt.
How do I confirm it is the prompt?
Log the prompt size on every model call. LangChain puts it on the response message as usage_metadata, and this callback writes one line per call to a file. A steady climb means the prompt grows with the run. A sharp drop means something in your setup is trimming or summarizing it. If the size is flat and the agent still drifts, the problem is somewhere else, and the next step tells you where.
How do I find out what it forgot?
Run the five-fact test. Give the agent a rule, an ID, a number, a name and a decision at the start. Change one of them around the middle. Then ask for all five at the first hour, the middle and the end, and run it more than once. The test is described here. What it loses tells you the fix: an early rule that vanished points at trimming, a number that comes back wrong points at a summary, and an old value quoted after you changed it points at a missing way to retire a fact.
What can I fix without leaving LangChain?
Three changes, smallest first.
Trim or summarize the messages before each call. LangChain's docs offer trimming, deleting and a built-in SummarizationMiddleware, and they warn that culling the message queue can lose information. It suits runs of tens of turns, and the first thing to go is usually a rule from early in the run.
Move what must not be lost out of the messages. Keep totals, open tasks and standing rules in fields your code writes, and put them at the top of every prompt. The model reads them fresh on each call and cannot rewrite them. Checkpointers save a thread's graph state, and a Store keeps long-term data across threads. Your graph code writes both. The next section writes this one out.
Add a memory layer your code calls: Mem0, Zep with Graphiti, Cognee, Supermemory or Hindsight. Each stores facts outside the prompt and returns the relevant ones. If the test shows a changed fact being quoted stale, pick one that retires old facts. The comparison is here. Your loop still builds the prompt, so it can still grow.
What does a facts file look like?
It is the second change above, written out, and it works without Argentic. The agent saves a fact with a tool. Your code prints the whole file into the system prompt on every model call. A summary can then drop a rule from the messages and the rule is still in front of the model, because it never lived in the messages. This one is for LangChain 1.x and keeps your own summarizer.
Python, LangChain 1.x. Replace model and your_tools with yours.
import json
from pathlib import Path
from langchain.agents import create_agent
from langchain.agents.middleware import SummarizationMiddleware, dynamic_prompt
from langchain_core.tools import tool
FACTS = Path("facts.json") # one file per run: put the run id in the name
SYSTEM = "You are a support agent." # your own prompt
def load() -> dict:
return json.loads(FACTS.read_text()) if FACTS.exists() else {}
@tool
def remember(key: str, value: str) -> str:
"""Save a fact the run must not lose: a rule, an ID, a running total, a decision.
Reuse a key to replace its old value."""
FACTS.write_text(json.dumps({**load(), key: value}, indent=2))
return f"saved {key}"
@dynamic_prompt
def prompt_with_facts(request) -> str:
facts = "\n".join(f"- {k}: {v}" for k, v in load().items()) or "- none yet"
return f"{SYSTEM}\n\nFacts saved so far:\n{facts}"
agent = create_agent(
model, # your chat model
tools=[remember, *your_tools],
middleware=[SummarizationMiddleware(model=model, trigger=("tokens", 4000)), prompt_with_facts],
)
We ran it against a scripted model. A rule and an ID saved in the first run were in the prompt of a second run that had none of the earlier messages, and a fact saved again under the same key replaced the old value. We did not run it against a live model, so we cannot say how reliably yours will call remember. Put it through the five-fact test before you trust it.
Where does the facts file stop working?
Four places.
- The model chooses what to save. A fact it never saved is gone once the messages are trimmed, and the five-fact test is how you find those.
- Every saved fact rides in every prompt. A file that holds a whole shift gets big, so you trim it, and trimming a facts file is deciding what to forget.
- A changed fact replaces the old one only when the model reuses the key. A new spelling leaves both in the prompt.
- Your messages still grow beside it, so the bill still climbs with the run.
Past that point you are writing a prompt builder: a record of the run, a size cap, search over the record and a way to retire a fact.
What if the prompt itself is the problem?
Then change who builds it. There are two ways.
You can keep your own loop and write the rest: a record of the run the model never rewrites, a prompt builder under a fixed size cap, search over the record, and a way to mark a fact as replaced. That build is described step by step. Pydantic AI's Harness ships compaction and a memory notebook if you would rather start from a framework. We have not run it.
Or hand the loop to an engine. Argentic is an engine that keeps AI agents right for hours while prompt size is bounded. You bring your LangChain tools, your prompt and your model key, and it builds each prompt from a record of the run under a size cap. The next section is the swap.
How do I swap the loop for Argentic?
Keep the tools and replace the loop. Install the TypeScript client with npm i @argentic-ai/client (Node 22 or newer). The client takes any tool shaped as a name, a description, a JSON schema and an execute function, so LangChain.js tools need four lines of mapping.
TypeScript, LangChain.js tools
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { Argentic, modelFromEnv, run, type Tool } from "@argentic-ai/client";
// Your LangChain tools, unchanged.
const refundOrder = tool(async ({ order_id, amount_usd }) => billing.refund(order_id, amount_usd), { // your code
name: "refund_order",
description: "Refund an order.",
schema: z.object({ order_id: z.string(), amount_usd: z.number() }),
});
const langchainTools = [refundOrder]; // and the rest
// The only new code: tell Argentic what each tool is, and run it when Argentic asks.
const tools: Tool[] = langchainTools.map((t) => ({
name: t.name,
description: t.description,
parameters: z.toJSONSchema(t.schema as z.ZodType) as Record<string, unknown>,
execute: (args) => t.invoke(args),
}));
const out = await run({
model: modelFromEnv(), // ARGENTIC_MODEL_ID, plus ARGENTIC_MODEL_PROVIDER or ARGENTIC_MODEL_BASE_URL, and the prices
client: new Argentic(), // reads ARGENTIC_API_KEY and MODEL_KEY
agent: "support-bot",
runId: event.id, // keep it the same when a run is retried
task: event.text, // only what is new, never the earlier messages
system: "You are a support agent. Call one tool per turn.",
tools,
});
console.log(out.reply);
run sends only the new event as task, never the earlier messages, and builds each prompt from the record of the run. Each tool call comes back to your code to carry out. We ran this mapping against a scripted server: the tool ran and its result went back on the next call. We did not run it against a live model in this guide.
The swap replaces the graph. A LangGraph agent's nodes, edges and checkpointer do not run, so an agent that is a model plus tools moves over more easily than a graph with hand-written nodes. Ready-made readers ship for these frameworks' tools, and a LangChain.js tool uses the mapping above instead:
- Vercel AI SDK,
@argentic-ai/client/ai-sdk - Mastra,
@argentic-ai/client/mastra
A run of tens of turns usually does not need any of this, because trimming or summarizing is enough there. Prototype is free and needs no card. It includes 5,000 agent steps a month.
What happens when the loop compacts?
Compaction is the usual fix for a prompt that grows, and it is where an agent starts to forget. To see how a stack does when it has to cut its history, we ran shifts with the prompt window held below the model's own. Against Letta, across the two conditions both ran (Claude Haiku 5.5, seed 20261217, one run of each stack in each row, November 27, 2026 to November 28, 2026; Argentic on engine 0.116.3), Argentic was level over 12 hours at a 200K window (engine 0.116.3 0.83 against Letta 0.82, first of two; within 0.03, which one run cannot separate); scored higher over 12 hours at a 100K window (engine 0.116.3 0.80 against Claude Agent SDK with memory 0.62 and Letta 0.59, first of three). With its step fee counted it cost less over 12 hours at a 200K window and cost more over 12 hours at a 100K window. None of these rows has been rerun on the newest engine (0.116.4), so they describe an older build. Each row is its own shift (the events and the question set differ between rows), so compare the stacks within a row and not a stack with itself down the table. Every row is one run of each stack on Claude Haiku 5.5; a gap of a few hundredths in one run is not a result.
What did that look like when we measured it?
Argentic engine 0.116.4: 0.88 on day 1, then 0.83 to 0.88 on each later day. One run on Claude Haiku 5.5 at a 200,000-token window. On the questions about something the agent was told 72 to 107 hours (3 to 4.5 days) earlier (112 questions), Argentic scored 0.83. Argentic engine 0.116.3 (November 28, 2026) scored higher than Argentic engine 0.116.4 on running counts (0.65 against 0.56 on 71 questions). Argentic engine 0.116.4 stayed within 0.05 below its day 1 score (0.88) on every later day. The window was set to 200,000 tokens, smaller than the model's own; that is a test condition, not stock settings.
We have not run a LangChain agent tuned with the trim, state and memory steps above, so this run says nothing about one, and we make no claim that Argentic is more accurate than one. Earlier versions of this guide compared Argentic with two other stacks. Those comparisons are withdrawn, because neither stack ran with its recommended memory setup. A rerun with each is pending, and until it is published nothing here compares Argentic with them. The benchmark guide names both and says what each run lacked.
Short answers
- How do I fix context rot in a LangChain agent?
- Log the prompt size on every call, run a five-fact test at the first hour, the middle and the end, then fix what it shows: trim or summarize, move standing rules and totals into state your code writes, add a memory layer, or change who builds the prompt.
- Why does my LangChain agent get worse as the run gets longer?
- LangChain's own docs say most LLMs perform poorly over long contexts and get distracted by stale or off-topic content. As a run grows, so does the prompt, and the details that matter get harder for the model to use.
- How did Argentic do on facts the agent was told days earlier?
- Argentic engine 0.116.4: 0.88 on day 1, then 0.83 to 0.88 on each later day. One run on Claude Haiku 5.5 at a 200,000-token window. On the questions about something the agent was told 72 to 107 hours (3 to 4.5 days) earlier (112 questions), Argentic scored 0.83. The window was set to 200,000 tokens, smaller than the model's own; that is a test condition, not stock settings.
- Can I fix context rot in a LangChain agent without adding a vendor?
- For runs of tens of turns, yes: keep a running facts file outside the chat history and have your code print it into the system prompt on every model call, so a summary of old messages cannot drop a rule. It stops working where the file stops being small. The model chooses what to save, every saved fact rides in every prompt, a changed fact replaces the old one only when the model reuses its key, and the messages still grow beside it.
- How do I move a LangChain agent's tools to Argentic?
- Keep the tools and replace the loop. Map each LangChain.js tool to a name, a description, a JSON schema and an execute function, and hand them to the TypeScript client's run, which sends only the new event and builds each prompt from the record of the run. It replaces the graph, so a LangGraph agent's nodes, edges and checkpointer do not run.
- Does LangChain's summarization middleware fix it?
- It helps for runs of tens of turns. LangChain's docs warn that culling the message queue can lose information, so for hours-long runs you usually also keep standing facts in state your code writes.
Sources and method: Head-to-head on a 12-hour shift.