Diff-Based Tool Output
When a tool edits a file, return only the changed lines or a unified diff instead of the full file content. The model does not need to re-read the entire file to understand the modification.
How It Works
The tool computes the diff between the original and modified file states and returns that diff in a standard format (unified diff, git diff, or structured patch). The model receives a compact representation that highlights exactly what changed.
Benefits
- drastically reduces token volume for file edit operations
- makes changes explicit and reviewable
- avoids model confusion from re-reading large unchanged sections
Example
Instead of returning 500 lines of file content after an edit:
{
"file": "file.py",
"diff": "@@ -10,3 +10,4 @@\n def foo():\n+ pass\n"
}
See Also
- Grep-Result Tool Output — another technique for returning minimal tool output.
- Structured Tool Output — diffs can be wrapped in structured schemas.
- Stateful Tracking in Agent Loops — tracking file state enables diff computation.
Tools
- Aider — coding assistant that uses diffs and concise edit formats to minimize context size.
- GitHub Copilot edits — returns edit diffs rather than full file rewrites.