翻篇收录说明
翻篇网页快照

Patrick John Kelly - Your AI Agent Doesn't Need to Keep Every File It Reads

https://www.patrickjohnkelly.com/posts/your-ai-agent-doesnt-need-to-keep-every-file-it-reads

最近抓取:2026-12-03 18:36:07(北京时间)

最近内容变化:2026-12-03 18:36:07(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Every file your AI agent reads during exploration stays in the conversation history until compaction runs. Here's a simpler way to keep context clean in a single thread without losing details.

正文摘录

Just because your AI agent reads a file doesn’t mean it should stay in the conversation history permanently. In most agent harnesses, every single message, tool call, reasoning chain, and file read stays in the context permanently until compaction runs. Since context windows aren’t infinite, eventually the system will run a ‘compaction’ process, which summarizes the conversation, making judgements about what to preserve and what to drop. Compaction helps keep your context small, keeping costs low and responses fast. Without it, you’ll hit your context window limit and the cost of each message will increase as your conversation grows in length. Your first message might cost a fraction of a cent, while your 30th message might cost several cents, or more. However, compaction isn’t perfect: you’ll lose details from your conversation, and sometimes you’ll lose important details that end up mattering later. Even if you’re within your context window, eventually you’ll experience ‘context rot’, where irrelevant details start to degrade the quality of your results. One of the biggest culprits is file exploration. You ask your AI agent to answer a question or make a change. It decides to go do some web searches, read files, and explore the various tools it has access to, and then it comes back with a response. Imagine you ask your agent to find your finance meeting notes from last month. It runs a search including terms like: "finance", "meeting", "september 2026", "notes". Maybe your files aren’t clearly named, and so it finds and reads 10 potentially relevant files, then determines that only one is what it was looking for. In most systems, all 10 files stay in the conversation history until the next compaction event. Across thousands of real coding sessions I analyzed recently, between 40% and 89% of the files an agent reads during a task are never actually used or edited. Subagents vs. staying in a single thread One modern pattern to address this is to use subagents, where your main orchestrator agent spins up a dedicated subagent specifically to go find your finance meeting notes and return JUST the relevant information. Subagents are great for parallelism, context isolation, and routing tasks to a faster or cheaper model (I’m writing a separate post on the pros, cons, and nuances of subagents - coming soon). But in applied AI, things are getting more and more complicated, and sometimes the best solutions are simple. With subagents, your agent now has to decide intelligently when a task is big enough to use a subagent and when not to. Even in systems that have subagents, the main agent still does most of its day-to-day file exploration directly in the main thread. And keeping work in a single thread has real benefits for simplicity, telemetry, and avoiding the ‘telephone game’ of a subagent summarizing away details you actually needed. So assuming you’re in a single thread, there’s a simpler way to think about this: let your agent read as much as it wants to, but don’t keep all of that in the conversation history permanently. Post-turn context pruning The ideal state would be predicting what context the agent is going to need in the future. Because we’ll never know that for sure, the best heuristic we can use is much simpler: keep whatever was legitimately used to produce the response, and prune what wasn’t. If the agent needs more context later, fetching a file again isn’t that big of a deal. You can think of this in three levels: 1. The default state: Your agent reads 10 files (or 5 websites and 5 files) to answer a question, and all 10 stay in the conversation history forever. 2. File-level pruning: Your agent reads 10 files and responds. Immediately after that step finishes, a lightweight pass looks at the turn and scrubs the 8 or 9 files that weren’t used from the history, leaving behind a one-line receipt so the agent remembers it already checked them. 3. Verbatim line-segment pruning: Even when a file was relevant, the agent often only needed 30 lines out of a 500-line file. Instead of keeping the entire file just because one section was used, we can keep the verbatim line segments that mattered, with ... [lines trimmed] ... before and after. (The trade-off I’m currently testing: if you trim a relevant file too aggressively, does that cause the agent to re-read the whole file later when it wants to make another edit?) The prefix caching catch There is one big nuance here: prefix caching. When you make an API request to an AI provider, if the start of your context (the ‘prefix’) is identical to a recent request, the provider can skip re-computing the math on that part of the prompt and give you roughly a 90% discount on those input tokens (I’ll do a full post on how prefix caching works and the math behind it soon). Because of prefix caching, you can’t just reach back into the middle of a conversation 10 turns later and delete old file reads. Changing the middle of the history changes the prefix, which invalidates the cache and can actually cost you more than leaving the dead files in place. That’s why pruning has to happen right at the tail, immediately after the agent finishes an exploratory turn or subtask, before new turns are built on top of it. You keep the 90% cache discount on everything that came before, you keep your context window clean without lossy compaction, and you prolong the useful life of your conversation. ← Back