general•v1.0.0•2 min read
Compile Context
Compiles a list of file paths into a single Markdown file to build a context document without blowing up the context window.
When the user provides a list of file paths and asks you to compile them into a context file (e.g., context.md), do NOT use the read_file tool to read their contents into your context window. This will cause massive token truncation and background delegation failures.
Instead, use a Python script executed via the write_file + terminal combination to read the files and write them directly to disk into a single Markdown file.
Trigger
- The user provides multiple file paths.
- The user asks to "compile context", "create a markdown file with these files", or "aggregate these files".
Steps
- Identify the target output file path (e.g.,
vfs_context.mdin the current directory or a specified path). - Identify the list of input file paths.
- Write a Python script (e.g.,
generate_context.py) usingwrite_filethat reads each file and writes its content to the output Markdown file in a structured format. - Execute the Python script using the
terminaltool:python3 generate_context.py. - Verify the file was created and inform the user.
Example Python Script Pattern
PYTHON
import sys
output_path = "/Users/jared/Documents/Code/monorepo/compiled_context.md"
files = [
"/path/to/file1.py",
"/path/to/file2.tsx"
]
with open(output_path, "w") as out:
out.write("# Compiled Context\n\n")
for f in files:
try:
with open(f, "r") as infile:
content = infile.read()
out.write(f"## {f}\n\n```\n{content}\n```\n\n")
except Exception as e:
out.write(f"## {f}\n\n```text\nError reading file: {e}\n```\n\n")
print(f"Context successfully aggregated into {output_path}")Pitfalls
- NEVER use
read_filefor this task. It will flood the agent's context window. - Ensure the python script handles file reading errors gracefully (e.g., file not found).
- Use absolute paths in the Python script to avoid working directory confusion.