Series: Deepagent (5/10)

Filesystem Tools: Manage Context with Files, Don't Let Tokens Explode
The context window problem is something almost everyone using AI for complex tasks has run into: partway through a task, the context is already packed with tool return data, intermediate results, and conversation history — then the model starts "forgetting" things it said earlier, or you just get a "context too long" error.
There are several solutions, but the most direct one is: move things that don't need to be in context all the time somewhere else.
Deep Agents' Virtual Filesystem is that "somewhere else."
The Agent's Virtual Filesystem
Deep Agents gives the agent four built-in filesystem tools:
ls: list the files in a directoryread_file: read the contents of a specified filewrite_file: write content to a new fileedit_file: modify part of an existing file
These tools work with the same logic as operating files in a terminal — the specific storage backend is set by you (the next article covers this; the default is in-memory).
Why Have a Filesystem
Here's a direct analogy: imagine an analyst processing a 50-page technical report.
Without a filesystem: dump the entire report into context, then do analysis inside the context. The problem is, once the report content + analysis process + follow-up conversation are all piled together, context fills up fast — and you can't "just look at chapter three while setting the rest aside."
With a filesystem: write the report to report.md, read_file only the section you need each time, write analysis results to analysis.md, and read analysis.md at the end to produce the final report. Context only holds "what this specific step actually needs."
This is the core value of filesystem tools — they let the agent do selective reading, without needing to keep everything in context all the time.
A Practical Demonstration
Here's a research task example:
from deepagents import create_deep_agent
def fetch_article(url: str) -> str:
"""Fetch the content of a web article."""
return f"Article content: detailed technical content from {url}..."
agent = create_deep_agent(
tools=[fetch_article],
system_prompt="""You are a research assistant.
When handling large amounts of information, use the filesystem tools wisely:
- Use write_file to save raw data you've fetched
- Use read_file to load content on demand, don't load everything into context at once
- Save analysis results as files too, then integrate them at the end
""",
)
result = agent.invoke({
"messages": [
{"role": "user", "content": "Research the common themes in these three articles: url1, url2, url3. Organize into a summary report."}
]
})The agent's likely execution flow would look like this:
fetch_article("url1")→write_file("article1.md", content)fetch_article("url2")→write_file("article2.md", content)fetch_article("url3")→write_file("article3.md", content)read_file("article1.md")→ analyze themes →write_file("analysis1.md", results)- Same for article2 and article3
read_file("analysis1.md"),read_file("analysis2.md"),read_file("analysis3.md")→ integrate → final report
At each step, context only holds what the current step needs — not all three articles at once.
edit_file: Modify Without Overwriting
One tool worth calling out specifically: edit_file.
Unlike write_file (which overwrites entirely), edit_file can modify only a specific part of a file — similar to a diff operation in code. This is useful when the agent needs to incrementally build a document:
- Write the document's skeleton first
- After each step completes, use
edit_fileto update the corresponding section - No need to rewrite the entire document each time
This lets the agent maintain a "continuously updated working document" across a long task, rather than relying on memory in context to maintain state.
Division of Responsibility Between Filesystem Tools and External Tools
One thing worth clarifying: filesystem tools are the agent's own workspace, not where your application stores data.
| Filesystem tools | Your custom tools | |
|---|---|---|
| Purpose | Agent's intermediate working state | Connecting to external services, databases |
| Who uses it | Agent manages it itself | Agent calls it, external system processes it |
| Persistence | Depends on backend configuration | Determined by external system |
| Typical contents | Drafts, analysis results, plans | Database records, API responses |
Simply put: your tools handle "communicating with the outside world," filesystem tools handle "the agent organizing its own intermediate process."
Which Tasks Benefit Most from the Filesystem
Particularly valuable scenarios:
- Processing multiple long documents (reports, code, papers)
- Tasks with multiple phases where each phase produces output for the next
- Agent needs to iteratively revise a document until conditions are met
- Simultaneously tracking multiple information sources
Scenarios where you can skip it:
- Simple Q&A or single-step tool calls
- Input data is small enough to fit in context without problems
- Short tasks with no need for intermediate storage
One Sentence Summary
Filesystem tools transform the agent from "just a brain with no desk" to "an agent with a desk where it can spread things out, categorize them, and process them step by step."
But the "underlying implementation" of this virtual desk is swappable — use memory in testing, use disk or cloud storage in production. The next article covers that choice.
Previous: Task Planning and write_todos: Make the Agent Think Before It Acts Next: Pluggable Backends: In-Memory, Disk, LangGraph Store, or Sandbox — How to Choose