| name | large-file-reader |
| description | Comprehensive toolkit for AI agents to read, analyze, and extract information from large files without overflowing context windows. Use when working with files too large to read entirely, analyzing codebases, extracting specific sections from logs, processing large datasets, or when semantic search is needed. Covers chunked reading, semantic grep, structured extraction, and intelligent file navigation strategies. |
Large File Reader
A comprehensive guide for efficiently reading and analyzing large files without exhausting context windows.
Core Strategy
- Never read entire large files - Always use targeted extraction
- Index before reading - Understand file structure first
- Semantic over literal - Use meaning-based search when possible
- Progressive disclosure - Start broad, narrow down
Quick Decision Tree
Is the file > 500 lines?
├── YES → Use chunked reading or semantic search
│ ├── Need specific pattern? → grep/rg with context
│ ├── Need meaning-based search? → mgrep (semantic)
│ ├── Need file structure? → Index first (grep -n)
│ └── Need specific lines? → sed -n 'START,ENDp'
└── NO → Safe to read directly
Tool Reference
1. Semantic Search (mgrep) - RECOMMENDED FOR AGENTS
Best for: Natural language queries, finding code by intent, multimodal files
npm install -g @mixedbread/mgrep
mgrep login
mgrep watch
mgrep "where is authentication configured?"
mgrep "database connection handling" src/
mgrep -m 20 "error handling patterns"
mgrep -a "how does caching work?"
mgrep --agentic "yearly metrics 2020-2024"
mgrep --web --answer "how to use Redis streams"
Why mgrep is best for agents:
- 2x fewer tokens than grep-based workflows
- Understands intent, not just patterns
- Works on code, PDFs, images
- Returns only semantically relevant chunks
2. Fast Pattern Search (ripgrep/rg)
Best for: Exact patterns, regex, speed on large codebases
rg "function_name" --context 5
rg "TODO" --type py --type js
rg -in "error" src/
rg -l "deprecated"
rg -c "import" --type ts
rg "def \w+\(.*\):" --type py
rg "pattern" --glob '!node_modules' --glob '!.git'
rg "pattern" --json
3. Line-Based Extraction (sed/awk)
Best for: Extracting specific line ranges, structured data
sed -n '100,200p' file.txt
sed -n '1,50p' file.txt
tail -n 50 file.txt
grep -n "pattern" file.txt | head -5
sed -n '95,105p' file.txt
awk 'NR % 10 == 0' file.txt
awk '/pattern/{for(i=1;i<=5;i++){getline; print}}' file.txt
4. File Structure Analysis
Best for: Understanding file organization before reading
wc -l file.txt
grep -n "^class \|^def \|^function " file.py
grep -n "^## \|^### " README.md
rg "^(class|def|function|const|let|var)\s+\w+" --line-number
find . -type f | sed 's/.*\.//' | sort | uniq -c | sort -rn
tree -L 2 --dirsfirst
find . -type f -exec ls -lh {} \; | awk '{print $5, $9}' | sort -hr
5. Log File Analysis
Best for: Extracting relevant entries from large logs
awk '$0 >= "2024-01-15" && $0 <= "2024-01-16"' app.log
grep -E "ERROR|WARN" app.log
grep -B2 -A5 "ERROR" app.log | tail -50
grep "ERROR" app.log | sort -u
grep -oE "(INFO|WARN|ERROR|DEBUG)" app.log | sort | uniq -c
awk '/Exception|Error/{p=1} p; /^$/{p=0}' app.log
6. JSON/Structured Data
Best for: Extracting from large JSON files
cat large.json | jq '.' | head -100
jq '.data[0:10]' large.json
jq '.items[] | select(.status == "active") | .name' data.json
jq '.items | length' data.json
jq --stream 'select(.[0][0] == "users")' huge.json
7. CSV/TSV Analysis
Best for: Large tabular data
head -5 data.csv
wc -l data.csv
csvcut -c 1,3,5 data.csv | head -20
csvgrep -c status -m "active" data.csv
head -1 data.csv | tr ',' '\n' | nl
awk -F'\t' '{print $1, $3}' data.tsv | head -20
cut -d',' -f3 data.csv | sort -u | head -20
8. Binary/PDF Files
Best for: Extracting text from non-text formats
pdftotext document.pdf - | head -200
pdftotext -layout document.pdf -
pdftk input.pdf cat 1-10 output first10.pdf
pandoc document.docx -t plain | head -200
mgrep "contract terms" documents/
Workflow Patterns
Pattern 1: Codebase Exploration
tree -L 2 --dirsfirst
wc -l src/**/*.py
rg "if __name__" --type py
rg "main\(\)" --type py
mgrep "user authentication flow"
mgrep "database connection setup"
sed -n '50,150p' src/auth.py
Pattern 2: Log Investigation
wc -l app.log
grep -c "ERROR" app.log
grep "ERROR" app.log | head -20
grep "ERROR" app.log | tail -20
grep -n "OutOfMemory" app.log
sed -n '1000,1050p' app.log
grep "ERROR" app.log | cut -d']' -f2- | sort -u
Pattern 3: Large JSON Analysis
jq 'keys' data.json
jq '.[0]' data.json
jq 'length' data.json
jq '.[0:5]' data.json
jq '.[] | select(.type == "error")' data.json
jq '.[] | {id, name, status}' data.json
Pattern 4: Multi-File Search
rg -l "authentication" src/
for f in $(rg -l "authentication" src/); do
echo "=== $f ==="
rg -C3 "authentication" "$f"
done
mgrep "authentication implementation" src/
Best Practices
DO:
- Always check file size first:
wc -l file.txt
- Use line numbers to navigate:
grep -n
- Prefer semantic search (mgrep) for understanding intent
- Extract only what's needed
- Index before diving deep
DON'T:
- Never
cat or read entire large files
- Avoid piping through
head or tail (causes buffering issues)
- Don't guess patterns - search first
- Don't load entire files into context
Environment Setup
Required Tools
brew install ripgrep
npm i -g @mixedbread/mgrep
brew install jq
pip install csvkit
brew install poppler
brew install pandoc
mgrep Setup for Agents
mgrep login
export MXBAI_API_KEY=your_api_key_here
mgrep watch
export MGREP_MAX_FILE_SIZE=1048576
export MGREP_MAX_FILE_COUNT=1000
Troubleshooting
Context window still filling up?
- Reduce
-m (max results) in mgrep
- Use narrower line ranges with sed
- Add more specific patterns to grep
mgrep not finding results?
- Ensure
mgrep watch is running
- Check
.mgrepignore for excluded files
- Try rephrasing query semantically
Slow searches?
- Use ripgrep instead of grep
- Exclude node_modules, .git:
--glob '!node_modules'
- Index with mgrep for repeated searches
Additional References
For advanced patterns, see:
references/advanced-patterns.md - Complex extraction patterns
references/mgrep-guide.md - Full mgrep documentation