QuestionQ59

Context Management & Reliability

You are building developer productivity tools with the Claude Agent SDK. The agent helps engineers explore unfamiliar codebases, understand legacy systems, generate boilerplate code, and automate repetitive tasks. It relies on the built-in tools (Read, Write, Bash, Grep, Glob) and also integrates with Model Context Protocol (MCP) servers.

During testing, you notice that in extended exploration sessions lasting 30 or more minutes, the agent begins giving inconsistent answers about code structure it already discussed earlier. Engineers report having to re-explain context about modules they have already had the agent explore.

What is the most effective approach to address this issue?

Explanation

The recommended pattern for long-running coding agents is to externalize key findings to a persistent file (a scratchpad, notes file, or memory-tool-backed store) rather than relying solely on the growing in-context conversation history. As the agent explores modules, it records what it has learned—file locations, module relationships, architectural decisions—into this file, and it reads that file back before answering follow-up questions. This keeps answers consistent across a long session and lets the agent recover accurate state even if the raw context is truncated or compacted, without needing to re-explore the codebase.

Aggressively clearing context on a fixed schedule (every 15 minutes) discards accumulated understanding and directly reproduces the reported symptom of engineers having to re-explain already-explored modules. Simply upgrading to a larger-context model delays but does not solve the underlying problem of information not being durably tracked, and it doesn't guarantee consistency. Pre-summarizing every source file before exploration begins is impractical for exploring unfamiliar or legacy codebases where relevance isn't known in advance, and it discards detail the agent would otherwise gather dynamically during exploration.

This scratchpad/notes pattern is documented by Anthropic as 'structured note-taking' and formalized via the memory tool, which explicitly supports building up project knowledge over time and maintaining state across a session without keeping everything in the active context window.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!