Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

LangChain 5 min read Deep dive mediumAdept

Context Management for Deep Agents

The Deep Agents SDK manages long-running agent context with three compression techniques triggered at fractions of the model's context window. Tool results over 20,000 tokens are offloaded to a filesystem and replaced with a path plus a 10-line preview. Old write/edit tool inputs are replaced with file pointers once context passes 85%, and summarization runs as a last resort, producing a structured summary while saving the full history to disk for later retrieval. To evaluate these mechanisms, the team forces compression early (10-25% of the window) to generate more events and uses targeted evals such as needle-in-the-haystack recovery to catch goal drift and lost details.

Related posts

  • Context Engineering with Deep Agents: How To Remember Without Keeping Everything In Context

    LangChain mediumAdept

    Long-running agents suffer "context rot" as tool output, history and instructions accumulate, so Deep Agents moves context out of the active window via middleware hooks in the agent loop rather than deleting it. FilesystemMiddleware offloads tool results over ~20,000 tokens (and user messages over 50,000) to files and shows only a preview. SummarizationMiddleware condenses older messages at 85% of the context window while keeping recent ones verbatim and writing the originals to recoverable files. SkillsMiddleware uses progressive disclosure to load task instructions only when relevant, subagents isolate noisy work in separate contexts, and MemoryMiddleware persists learned preferences in a plain markdown file that survives switching model providers.

  • Building Long-Running Agents with ScaleDown and Deep Agents

    LangChain mediumAdept

    The langchain-scaledown integration offloads context-management decisions in long-running agents (summarizing history, compressing tool results, extracting fields, classifying state) from frontier LLMs to ScaleDown's small, task-specific constrained decoding models, which ScaleDown reports are far faster and cheaper on these narrow tasks. It ships middleware for LangChain and Deep Agents. Summarization replaces history past a trigger fraction. Tool compression adds a compression_query argument that cut a docs page by about 90%. Extraction copies exact values from user messages into agent state. A custom Classify-based circuit breaker can stop looping or drifting runs. ScaleDown calls can also route through the LangSmith LLM Gateway for tracing and spend limits, while open-ended generation stays on a general-purpose model.

  • DeepSeek-V4: a million-token context that agents can actually use

    Hugging Face hardMaster

    DeepSeek-V4 (Pro: 1.6T total/49B active; Flash: 284B/13B active) is a MoE model family whose 1M-token context is made practical by a hybrid attention design that interleaves Compressed Sparse Attention (4x KV compression plus an FP4 lightning indexer selecting top-k blocks) with Heavily Compressed Attention (128x compression with dense attention), plus FP8 KV storage. At 1M tokens V4-Pro needs 27% of V3.2's per-token FLOPs and 10% of its KV cache, about 2% of a bf16 GQA-8 cache. Agent-focused post-training preserves reasoning across user turns when tools are involved, adds an XML-based |DSML| tool-call schema to reduce escaping and type errors, and relies on DSec, a Rust sandbox platform for RL rollouts. Agent benchmarks land near frontier closed models (SWE Verified 80.6, MCPAtlas 73.6), and MRCR 8-needle retrieval holds 0.59 at 1M tokens.

  • Building Deep Research: How we Achieved State of the Art

    Hugging Face mediumAdept

    Tavily's deep research agent reached state of the art on DeepResearch Bench while using 66% fewer tokens than LangChain's Open Deep Research, mainly through context engineering. Each tool output is distilled into a short reflection, and only past reflections are passed to the tool-calling model. Raw sources come back only for the final write-up. This turns token growth from quadratic (n·m(m+1)/2) into linear (n·m) across m iterations. Other lessons: keep orchestration simple so future model gains carry through, deduplicate sources through global state, expose a small toolset, design for failure modes, and treat LLM-as-judge evals as directional feedback rather than the optimization target.

  • Context Engineering (Agent Memory) for AI Agents Starts with Retrieval

    LangChain mediumAdept

    Agent retrieval fails in three ways: inefficient searching, too much context at once, and irrelevant or stale context. The fix is to keep searches small and keep raw results out of the main agent's context window. Deep Agents does this with glob/grep before reads, paged read_file (100 lines by default with offset/limit), and automatic offloading of tool results over ~20k tokens to files with a preview. For custom RAG tools, the recommended pattern is "retrieve, offload, delegate": write results to files, return only paths, and have parallel subagents read each file and return key facts with sources and dates. Other advice covers using grep with synonyms before semantic search, agentic rather than single-shot retrieval, skills for progressive disclosure, and treating retrieved text as untrusted.

The quest for software engineering knowledge.

Look:
Layout: