← Terse Blog
Agent Sessions

Git Diff Compression

By ·Founder, Terse·Updated

When an AI coding agent runs git diff, the full output lands in your context window — routinely 30,000 to 60,000 tokens on a busy branch. Most of it is lockfile churn, whitespace, and unchanged context lines the model never needs. Filtering the diff before it reaches the model cuts that cost by roughly 90% with zero information loss.

Where the Tokens Go

A 200-line code change can weigh 40,000 tokens as a raw diff. That sounds impossible until you look at what a diff actually contains. Only a fraction of it is your change. The rest is unchanged context lines around each hunk, whitespace-only churn from reformatting, vendored code and build output that happened to move, and — the silent killer — lockfiles. A single package-lock.json diff can be 20,000 tokens on its own, and it carries essentially zero information the model can act on.

The cost is worse than it first appears, because tokens in an agent session are not billed once. Everything in the context window is re-sent — and re-billed — on every subsequent turn until it falls out of context. A 40K-token diff that stays in context for ten turns costs you ten times. At typical frontier-model rates, a large unfiltered diff can add around $0.45 per turn; the same diff filtered down to the meaningful hunks costs closer to $0.06. Over a heavy week of Claude Code sessions, that difference is on the order of $300 per year — from one habit change.

The Fix: Show the Model the Change, Not the Whole Repo

The principle is simple: scope the diff to what actually moved, and exclude everything generated. Three concrete techniques cover almost all of the waste.

1. Exclude lockfiles and generated output with a pathspec

Git pathspecs can carve files out of a diff without touching your .gitignore:

git diff -- . ':(exclude)*-lock.json' ':(exclude)*.lock' \
         ':(exclude)dist/' ':(exclude)vendor/'

Lockfiles are machine-generated and machine-consumed. The model gains nothing from seeing 800 lines of resolved dependency hashes — if it needs to know a dependency changed, the one-line change in package.json already says so.

2. Ignore whitespace churn

git diff --ignore-all-space

Reformatting commits — a prettier run, a tab/space conversion, a trailing-newline sweep — produce diffs where thousands of lines "changed" but nothing meaningful did. --ignore-all-space makes those lines disappear from the diff entirely. For an even lighter first pass, git diff --stat gives the model an overview for a few hundred tokens, and it can request specific files afterward.

3. Reduce context lines

By default git shows three unchanged lines around every hunk. On a diff with many small hunks, those context lines can outweigh the changes themselves. git diff -U1 keeps a single line of context — usually enough for the model to orient itself, at a third of the framing cost.

Compressing Pasted Diffs

Command-line flags help when the agent runs git itself. But developers also paste diffs into chat by hand — a PR diff from GitHub, a colleague's patch, a CI failure log. That is where an optimizer layer earns its keep: Terse compresses anything you paste before it is sent, stripping the noise while preserving every changed line, and shows the token count falling in real time. The same pipeline that powers telegraph compression for prose knows to leave code hunks untouched — compression applies to the framing, never the change itself.

Why This Matters More Than Prompt Wording

Developers often start token optimization by trimming their own sentences. That helps — see the politeness tax — but tool output is usually an order of magnitude larger than anything you type. One unfiltered diff can outweigh a week of verbose prompts. The highest-leverage optimizations target the biggest payloads first: diffs, file reads (see the context window diet), and search results. Fix those and the context window stops silently draining your budget.

Stop Paying for Diff Noise

Terse compresses pasted diffs and monitors your agent sessions for oversized tool output. Runs on-device — no code leaves your machine.

Download Terse

Frequently Asked Questions

How many tokens is a typical git diff?

A small focused diff is 500-2,000 tokens, but on a busy branch with lockfile changes, git diff routinely produces 30,000-60,000 tokens. A single package-lock.json change can account for 20,000 tokens by itself.

Does filtering the diff hide information the AI needs?

No. Lockfiles, whitespace-only changes, and vendored code carry no actionable information for a coding model. The meaningful signal — which source lines changed and how — is fully preserved by pathspec excludes and --ignore-all-space.

Why does a diff cost money on every turn?

LLM APIs are stateless: the full conversation, including old tool output, is re-sent with every request. A diff that stays in context for ten turns is billed roughly ten times, which is why removing 30K tokens of noise compounds quickly.

Further Reading

Related reading

12 Token Optimization Techniques for 2026How to optimize tokens: 12 proven token optimization techniques for AI coding tools in 2026 — pr… Selective Context Pruning — How Terse Removes Redundant Context from AI Selective context pruning removes redundant information from AI conversation history. Learn how … Pattern OptimizationTerse applies 130+ phrase-shortening rules to compress verbose AI prompts automatically. Learn h… How Typos Inflate AI Agent CostsA single typo can triple a word's token count, break tool calls, and trigger costly retries. How…