05.18Context and memory
Context engineering for long-running AI agents: strategies and memory
How to keep long-running AI agents effective as context fills: write, select, compress and isolate context, plus memory, checkpointing and AI review.
On this page
How do engineers keep AI agents working well when they run for hours or days and their context windows fill with tool calls and history? AR Labs researched that question in January 2026, and the findings inform our view of context engineering: choosing what goes into a model's context window at each step. The report synthesises three video talks on context engineering with published material from Anthropic, Cognition, LangChain, Microsoft and Hugging Face into four core strategies and the supporting patterns for memory, checkpointing, observability and AI review.
Summary
Cognition calls context engineering "effectively the number one job of engineers building AI agents". As agents move from single-turn assistants to systems that run for hours, days or weeks, what goes into the context window increasingly decides whether an agent works or fails.
Key findings:
- Context window = RAM: limited capacity requires active management, not passive accumulation.
- Four core strategies: write, select, compress and isolate (all the major practitioners reviewed converge on these).
- Time horizons doubling every seven months: the sources treat long-running agents as the 2026 frontier.
- The harness matters as much as the agent logic: context pipelines, memory systems and observability infrastructure are as important as the agent's own logic. (We use harness to mean everything around the model that shapes what an agent does: the agent program, its instructions, skills, hooks, tools and checks.)
- AI review of AI work lets the loop scale: human attention becomes the bottleneck, and AI-based review and evaluation relieve it.
Core concepts
What context engineering is
"Context engineering is the delicate art and science of filling the context window with just the right information for the next step." (Andrej Karpathy, via LangChain video)
The term gained traction from Toby Lutke (Shopify) and was amplified by Karpathy's analogy:
| Component | Analogy |
|---|---|
| LLM | CPU |
| Context window | RAM / Working memory |
| Context engineering | Operating system memory management |
Just as an OS curates what fits in RAM, context engineering is the discipline of deciding what needs to fit in context at each step of an agent's trajectory.
Alternative framing (Microsoft): Think of context as a grocery basket. Everything has a token cost. Context engineering ensures you're putting in "healthy food" (relevant, accurate information) rather than filling the basket with junk.
Types of context
| Type | Description | Examples |
|---|---|---|
| Instructions | Static guidance for behaviour | System prompts, rules files, CLAUDE.md |
| Knowledge | Facts and information | RAG retrieval, documents, databases |
| Tools | Capabilities and feedback | Function calls, API responses, MCP servers |
| Conversation history | Interaction record | Chat messages, growing over time |
| User preferences | Personalisation | Likes, dislikes, learned patterns |
| Memories | Persistent learnings | Episodic, semantic, procedural memory |
Why context engineering is harder for agents
Agents have two properties that increase context pressure:
- Longer-running tasks: more turns mean more accumulated tokens
- Tool calling: each tool call adds both input and output to context
Turn 1: User query + LLM response + Tool call + Tool result
Turn 2: + LLM response + Tool call + Tool result
Turn 3: + LLM response + Tool call + Tool result
...
Turn N: Context window approaching limits
As context grows, problems emerge:
- Distraction: important information buried in noise
- Confusion: conflicting information causes errors
- Poisoning: hallucinations persist and influence future responses
- Cost explosion: token usage grows linearly (or worse)
The four strategies
All the major practitioners reviewed converge on four fundamental approaches to context engineering:
1. Write context (save outside the window)
Purpose: Persist information for later retrieval without consuming context space now.
Techniques:
| Technique | Scope | Implementation |
|---|---|---|
| Scratchpad | Single session | File (e.g., agent-scratchpad.md) or runtime state object |
| State object | Single session | LangGraph state, Pydantic model with fields |
| Long-term memory | Cross-session | Database, vector store, memory service |
Example, Anthropic's multi-agent researcher:
"The lead researcher begins by thinking through the approach and saving that plan to memory to persist it. The context window might exceed 200K tokens, but the plan can always be retrieved and retained."
Example, Semantic Kernel's scratchpad plugin:
# Creates agent_scratchpad.md file
# Agent writes: completed tasks, user preferences, key decisions
# Retrieved when needed, stays out of context otherwise2. Select context (pull in what is needed)
Purpose: Dynamically retrieve only relevant context for the current step.
Techniques:
| What to Select | How |
|---|---|
| Knowledge | RAG over documents, embedding similarity search |
| Tools | RAG over tool descriptions (solves 30+ tool degradation problem) |
| Memories | Retrieve relevant episodic/semantic memories |
| Instructions | Load relevant rules files, few-shot examples |
Tool Selection Problem:
"Agents have difficulty handling large collections of tools. This paper shows degradation after ~30 tools and complete failure at ~100 tools. Solution: RAG over tool descriptions to fetch only relevant tools for a given task." (LangChain video, citing research)
The Windsurf and Cursor approach (code agents):
- Parse code along semantically meaningful boundaries (not random chunks)
- Combine embedding search + grep + knowledge graphs
- LLM-based ranking on top of retrieval results
- "Highly non-trivial": significant engineering investment
3. Compress context (retain only essential tokens)
Purpose: Reduce context size while preserving critical information.
Techniques:
| Technique | How it Works | When to Use |
|---|---|---|
| Summarisation (full) | Summarise the entire conversation | Approaching context limits |
| Summarisation (scoped) | Summarise completed work sections only | Preserving active context |
| Trimming | Remove older messages via heuristics | Simple, predictable reduction |
| Pruning (learned) | LLM-based selective removal | Intelligent, context-aware |
Claude Code Auto-Compact:
"Claude Code calls auto-compact once the session reaches 95% of the context window (200K tokens for Claude)."
Anthropic Multi-Agent Researcher:
"Applies summarization only to completed work sections", which preserves active context while compressing finished work.
Cognition (Devin) Pattern:
"Use summarization to compress context at the interface between different agents and sub-agents", which enables information handoff.
Semantic Kernel Implementation:
chat_history_reducer = ChatHistoryReducer(
reducer_target_count=10, # Target messages after reduction
reducer_threshold=15 # Trigger reduction after 15 messages
)4. Isolate context (split across boundaries)
Purpose: Partition context so different components don't compete for space.
Techniques:
| Technique | Description |
|---|---|
| Multi-agent | Each agent has own context window |
| Sandboxing | Execute code in isolated environment, return only results |
| State partitioning | Different fields in state object for different purposes |
Anthropic Multi-Agent Researcher:
"Sub-agents operate in parallel with their own context windows, exploring different aspects of the question simultaneously. This expands the number of tokens the overall system can process."
Hugging Face Open Deep Research (Code Agent Pattern):
- LLM generates executable code containing tool calls
- Code executes in sandbox
- Sandbox persists state over multiple turns
- Only selective information (return values, stdout) passed back to LLM
- Token-heavy objects (images, audio) stay in sandbox, never enter context
State Object Isolation:
class AgentState(BaseModel):
messages: list # Always exposed to LLM
scratchpad: str # Only accessed when needed
tool_results: dict # Accessed at specific pipeline stages
final_output: str # Used at end of trajectoryPractitioner insights
Anthropic
Two-Agent Pattern:
- Initializer agent creates plan and artefacts
- Coding agent works with artefacts as external memory
- Artefacts:
claude-progress.txt, feature lists, etc. - Re-read artefacts on each turn to restore context
Key principle: Artefacts serve as "extended context" that survives window limits.
Cognition (Devin)
"Context engineering is effectively the number one job of engineers building AI agents."
Approach:
- Heavy investment in context pipeline architecture
- Summarisation at agent-to-agent interfaces
- Focus on preventing context pollution
LangChain / LangGraph
State-centric design:
- State object accessible at every node
- Checkpointing persists state across turns
- Long-term memory as first-class component
Built-in utilities:
- Message summarisation
- History trimming
- Tool selection via RAG (
langgraph-bigtool)
Recommendation:
"Before undertaking context engineering: (1) token tracking via observability, (2) evaluation to measure the effect of changes."
Microsoft / Semantic Kernel
Chat History Reducer:
- Configurable target count and threshold
- Automatic summarisation when threshold exceeded
Agent Scratchpad Plugin:
- Markdown file for agent notes
- Tracks: completed tasks, user preferences, session state
- Stays out of context until explicitly retrieved
Context issues and solutions:
- Context poisoning (false info persists) → Validate before adding to context
- Context confusion (too many tools) → Tool loadout management via vector DB
Manus and Devin-style agents
Long-running patterns:
- Sessions spanning hours to days
- Checkpointing at regular intervals
- Human intervention points for course correction
Key metric: KV-cache hit rate
"Single most important metric for production agents - affects both latency and cost."
Technical patterns
Checkpointing and persistence
LangGraph Threads:
- Every "super-step" automatically checkpointed
- Thread ID enables resume from any point
- State reconstruction without re-running
Implementation pattern:
# State persisted at each node
graph.add_node("process", process_function)
# Checkpoint includes full state
config = {"configurable": {"thread_id": "user-123"}}Memory system architecture
Three memory types (cognitive science mapping):
| Memory Type | Human Analogy | Agent Implementation |
|---|---|---|
| Semantic | Facts learned in school | Knowledge base, RAG |
| Episodic | Past experiences | Conversation logs, few-shot examples |
| Procedural | Motor skills, instincts | Instructions, rules files, prompts |
Short-term vs Long-term:
- Short-term: Session scratchpad, working memory, current state
- Long-term: Cross-session persistence, user preferences, learned patterns
Agent observability
What to track:
- Token usage per turn (input, output, cumulative)
- Context utilisation percentage
- Time-to-first-token (affected by context size)
- Tool call patterns and frequencies
- State changes and decision points
Intervention capabilities:
- Pause agent execution
- Inspect current state ("what is agent thinking")
- Redirect with updated instructions
- Resume from checkpoint
AI reviewing AI work
Why it matters for scale:
"In 2026, the big win will not be 'AI can do drafts.' It will be 'AI can audit drafts and ensure work product is complete and consistent.'" (2026 Predictions video)
Review patterns:
- Judge models for quality assessment
- Evaluation suites (multiple test passes)
- Red team / adversarial checks
- Policy and compliance validation
- Factuality verification
- Domain-specific "linting" for reasoning
Implementation:
# Eval loop pattern
while not all_evals_pass:
output = agent.generate()
results = run_eval_suite(output)
if not results.passed:
agent.refine(results.feedback)Long-running agent challenges
The state of things in 2026
- Time horizons: Doubling approximately every 7 months
- Current capability: 20 to 30 hour runs reported by leading researchers
- 2026 prediction (from the 2026 Predictions video): week-long agent runs becoming normal by Q4
Core challenges
| Challenge | Description | Mitigation |
|---|---|---|
| Context window amnesia | Agent forgets critical details as window fills | Write + Select strategies |
| State drift | Agent's understanding diverges from reality | Observability, checkpoints, human review |
| Error accumulation | Small mistakes compound over time | AI reviewing AI, eval loops |
| Cost explosion | Token usage grows with conversation length | Compress strategy, efficient caching |
| Observability gaps | Hard to know what agent is doing mid-run | State inspection, intervention points |
The human bottleneck
"In a world where agents run for a week, burning millions of tokens, we humans become the bottleneck. Our ability to review work, assign work, have good taste about what we want done." (2026 Predictions video)
Implication: scaling long-running agents depends on AI review of AI work, because human review time does not scale with them.
How the topics build on each other
Each topic in this report depends on the one before it: the four strategies rest on an understanding of context-window limits, and supervision of production agents rests on memory, observability, checkpointing and AI review.
flowchart TD
A["Context window fundamentals (token limits, KV-cache)"] --> B["Context engineering strategies"]
B --> W["Write: scratchpads, state objects, memory persistence"]
B --> S["Select: RAG for knowledge, RAG for tools, rules files"]
B --> C["Compress: summarisation, trimming, chat history reduction"]
B --> I["Isolate: multi-agent split, sandboxes, state partitioning"]
I --> M["Memory system design"]
M --> O["Agent observability and monitoring"]
O --> K["Checkpointing and persistence"]
K --> R["AI reviewing AI work"]
R --> P["Production agent supervision"]Key quotes
"Context engineering is the delicate art and science of filling the context window with just the right information for the next step." (Andrej Karpathy)
"Context engineering is effectively the number one job of engineers building AI agents." (Cognition)
"The sub-agents operate in parallel with their own context windows, exploring different aspects of the question simultaneously." (Anthropic, multi-agent researcher)
"In 2026, the big win will not be 'AI can do drafts.' It will be 'AI can audit drafts and ensure work product is complete and consistent.'" (2026 Predictions)
"In a world where agents run for a week, burning millions of tokens, we humans become the bottleneck." (2026 Predictions)
"The lead researcher begins by thinking through the approach and saving that plan to memory to persist it. The context window might exceed 200K tokens, but the plan can always be retrieved and retained." (Anthropic)
"Agents have difficulty handling large collections of tools. This paper shows degradation after ~30 tools and complete failure at ~100 tools." (LangChain, citing research)
Sources
Videos
| Video | Source | Link |
|---|---|---|
| Context Engineering for Agents | LangChain (Lance Martin) | https://www.youtube.com/watch?v=4GiqzUHD5AA |
| Context Engineering for AI Agents | Microsoft Developer | https://www.youtube.com/watch?v=F5zqRV7gEag |
| The Compounding Gap (2026 Predictions) | Not named | https://www.youtube.com/watch?v=pOb0pjXpn6Q |
Web sources
- Anthropic Multi-Agent Researcher blog post
- Cognition blog on context engineering
- LangGraph documentation (checkpointing, memory)
- Hugging Face Open Deep Research implementation
- Drew Breunig's post on context failures (poisoning, distraction, confusion, clash)
- Research paper on tool degradation at 30+ tools
Referenced by 10
- ConceptsThe Loop: how AR Labs workslinks here in its text
- ConceptsContext engineering for AI agentslists this as related
- ResearchAI agent planning and execution loop: holding a plan, choosing the next task and resuminglists this as related
- ResearchAI agent orchestration driver: reconciliation loops versus eventslists this as related
- ResearchHarness engineering for AI agents: origins and sourceslists this as related
- ResearchSelf-hosted always-on AI agents: OpenClaw, Hermes, Claude Agent SDKlists this as related
- ResearchMoving AI agent sessions between Claude Code, Codex, Pi and machineslists this as related
- ResearchAI agent memory for changing facts: append-only records versus memory systemslists this as related
- ResearchMarkdown RAG for Claude Code and LangGraph: grep versus vector searchlists this as related
- ResearchRAG frameworks for LangGraph and Claude Code in 2026lists this as related