| name | context-aware-ops |
| description | Intelligent resource management with size checking and filtering to preserve context window. You MUST load this skill when managing large resources or context window limits. |
| license | MIT |
Context-Aware Operations Skill
This skill provides patterns and techniques for managing large files and command outputs
efficiently, preventing context window exhaustion while maintaining effective problem-solving
capabilities.
WHEN TO USE
- Before executing commands that might produce large output
- Before reading any file in the codebase
- When debugging issues that might involve large resources
- When searching through codebases
- When working with logs, build outputs, or data files
WHEN NOT TO USE
- For trivially small, single-file edits where the overhead of checking file size slows down the workflow unnecessarily.
- When the user explicitly demands the full output of a specific file and context limits are known to be sufficient.
- For interactive terminal sessions where pagination tools (
less, more) are natively handled by the user.
Common Pitfalls
- Blind Dumping: Running
cat on a build log without checking its size, instantly blowing out the LLM's context window.
- Truncating Crucial Errors: Using
head to sample a file when the actual error message resides at the very end of the stack trace (where tail was needed).
- Ignoring Binary Files: Attempting to read binary artifacts or minified JS without filtering, resulting in unreadable token noise.
Core Principle
Always check before you dump!
Never blindly dump large resources into your context. Always:
- Check the size first
- Use filtering if needed
- Focus on relevant portions only
File Size Checking
Check Line Count
wc -l filename.txt
wc -l < filename.txt
wc -l filename.txt && ls -lh filename.txt
Check File Size
ls -lh filename.txt
stat -c%s filename.txt
stat -f%z filename.txt
[ $(wc -l < filename.txt) -gt 500 ] && echo "Large file" || echo "Small file"
Filtered File Reading
Read Beginning and End
head -n 50 filename.txt
tail -n 50 filename.txt
head -n 30 filename.txt && echo "..." && tail -n 30 filename.txt
Read Specific Ranges
sed -n '100,200p' filename.txt
sed -n '125,175p' filename.txt
tail -n +100 filename.txt | head -n 50
Search-Based Reading
grep -n "pattern" filename.txt
grep -C 5 "pattern" filename.txt
grep -n "pattern" filename.txt | head -20
grep -c "pattern" filename.txt
Command Output Filtering
Check Output Size First
command | tee >(wc -l >&2) | head -20
output_lines=$(command | wc -l)
echo "Command produced $output_lines lines"
if [ $output_lines -gt 100 ]; then
command | head -50
else
command
fi
Filter Common Patterns
command 2>&1 | grep -E "error|warn|fail" -i
command | grep -E "ERROR|WARN|FATAL"
command | grep -v -E "DEBUG|TRACE|INFO"
command 2>&1 | grep -i error | sort -u
Paginate Large Output
command | head -n 50
command | wc -l
command | head -20 && echo "... (showing first 20 lines)"
command | tail -20 && echo "... (showing last 20 lines)"
Smart File Viewing Strategy
Decision Tree
#!/bin/bash
FILE=$1
if [ ! -f "$FILE" ]; then
echo "File not found: $FILE"
exit 1
fi
LINES=$(wc -l < "$FILE")
if [ $LINES -le 100 ]; then
cat "$FILE"
elif [ $LINES -le 500 ]; then
cat "$FILE"
echo "--- End of file ($LINES lines) ---"
elif [ $LINES -le 2000 ]; then
echo "--- First 50 lines of $LINES ---"
head -n 50 "$FILE"
echo "--- ... ---"
echo "--- Last 50 lines ---"
tail -n 50 "$FILE"
else
-n 30
-n 30
Working with Logs
Efficient Log Analysis
grep -i error logfile.log | head -20
tail -1000 logfile.log | grep -i error
grep -i error logfile.log | awk '{print $NF}' | sort | uniq -c | sort -rn
grep -i error logfile.log | awk '{print $1, $2, $NF}' | head -20
grep -B 3 -A 10 "Exception" logfile.log | head -50
Log Sampling
awk 'NR % 10 == 0' large.log | head -100
shuf -n 50 large.log
grep -i error large.log | awk '!seen[$0]++' | head -20
Working with Code
Search Code Efficiently
grep -n "^def " *.py
grep -A 5 "^class " *.py | head -50
grep -rn "TODO\|FIXME" --include="*.py" | head -20
grep -rc "pattern" . | grep -v ":0$" | sort -t: -k2 -rn
Browse Large Codebases
find . -name "*.py" -exec wc -l {} + | sort -rn | head -20
for f in $(grep -l "pattern" *.py); do
lines=$(wc -l < "$f")
echo "$f: $lines lines"
done
find . -type f -name "*.py" | head -30
tree -L 3 --filesfirst 2>/dev/null || find . -type d | head -20
Context Window Budget Management
Track Usage Mentally
- Small files (<100 lines): ~100 tokens per file
- Medium files (100-500 lines): ~500 tokens per file
- Large files (500-2000 lines): Consider partial reading
- Very large files (>2000 lines): Never dump completely
Prioritization Strategy
- Critical: Files directly related to the bug/feature
- Important: Dependencies and related modules
- Nice-to-have: Context and documentation
- Skip: Tangentially related or very large files
When Context is Running Low
wc -l filename && head -20 filename
grep -E "^(def|class|function|export)" filename
sed -n '/start_marker/,/end_marker/p' filename
awk '/^def target_function/,/^def [^t]/' filename.py
Advanced Techniques
Pipe Chains for Efficiency
find . -name "*.log" -exec grep -l "ERROR" {} \; | head -10
grep -rh "pattern" . | sort -u | head -20
cat large.txt | \
grep -i "keyword" | \
grep -v "noise" | \
sort -u | \
head -30
Binary Search for Large Files
total_lines=$(wc -l < large.txt)
middle=$((total_lines / 2))
head -n $middle large.txt | grep -q "pattern" && echo "In first half" || echo "In second half"
Incremental Reading
chunk_size=100
current=0
total=$(wc -l < file.txt)
sed -n "1,${chunk_size}p" file.txt
current=$((current + chunk_size))
sed -n "${current},$((current + chunk_size))p" file.txt
What to Avoid
-
Don't: cat huge_file.log
Do: head -100 huge_file.log && echo "... (showing first 100 of $(wc -l < huge_file.log) lines)"
-
Don't: npm install --verbose
Do: npm install 2>&1 | grep -E "error|warn" -i || echo "Install successful"
-
Don't: git log
Do: git log --oneline -20 or git log --oneline | head -20
-
Don't: docker logs container
Do: docker logs --tail 100 container or docker logs container 2>&1 | grep -i error
-
Don't: ./run_tests.sh
Do: ./run_tests.sh 2>&1 | tee >(wc -l >&2) | grep -E "fail|error|pass" -i | head -50
Remember
- Size matters: Always check before you dump
- Filter first: Use grep, head, tail, sed, awk
- Focus: Only show what's relevant to the current task
- Summarize: When in doubt, show a summary rather than everything
- Iterate: You can always come back for more details if needed
Related Skills
- shell:
You MUST load this skill when handling shell commands with performance monitoring or timeouts.
Your context window is precious - use it wisely!