Figures on this page were verified 31 August 2026 against the providers' own documentation. Pricing, context windows and rate limits change without notice, so confirm any number against the provider before you rely on it. Tell us if something here is out of date.
The reason AI tools mangle long files is almost never comprehension. It is that asking a model to reproduce a 2,000-line file guarantees a truncated or silently altered copy. The fix is to stop asking for whole files. Ask for a patch, apply it yourself, and let a compiler or test suite verify the result.
Why whole-file rewrites fail
- Output ceilings. A 2,000-line file is roughly 25,000 tokens. Ask for it back and you are near the output limit before the model writes anything new, which produces a response cut off at max_tokens that returns HTTP 200 and looks like success.
- Cost. You pay output rates, three to six times input, to receive code you already had.
- Silent drift. Regenerating 2,000 lines to change 5 gives 1,995 opportunities to quietly reformat a string, drop a comment or reorder an import.
- Review burden. A diff touching the whole file is unreviewable, so real changes hide among noise.
Ask for a patch instead
Constrain the output to the change itself, with enough surrounding context to locate it unambiguously.
Return ONLY the edits, as one or more blocks in this exact form:
<<<<<<< SEARCH
(3-5 lines of existing code, copied exactly)
=======
(the replacement)
>>>>>>> REPLACE
Rules:
- The SEARCH block must match the file byte for byte, including indentation.
- Do not reformat, reorder or reflow anything outside the change.
- If a change is not possible, say so instead of guessing.Two properties make this work. Output is proportional to the change rather than the file, so truncation stops being a factor. And the search block either matches or it does not, so a wrong edit fails loudly at apply time instead of landing quietly in your repository.
Send less of the file, not more
The instinct when a model gets confused is to paste in more context. Beyond a point that makes things worse: the relevant lines are diluted, cost rises, and you move toward context_length_exceeded.
Send the function being changed, its immediate callers, and the type or interface definitions it depends on. That is usually a few hundred lines rather than a few thousand, and it is enough for a correct edit. The context budget planner shows what fits once fixed overhead is accounted for.
Verify mechanically, every time
The point of a patch workflow is that the result is checkable. Wire the loop so nothing reaches a commit unverified.
# 1. Snapshot before any AI edit, so reverting costs nothing
git add -A && git stash push -m "pre-ai"
# 2. Apply the patch, then prove the file is still valid
python -m compileall -q path/to/file.py || exit 1
pytest tests/ -x -q || exit 1
git diff --stat # should be smallIf git diff --stat reports far more changed lines than the task implied, the model rewrote the file rather than patching it. Revert and retry with a tighter instruction rather than reviewing a thousand-line diff by eye.
When the file is genuinely too big
Sometimes the real problem is the file. A 3,000-line module that no model can reason about is usually one a new colleague cannot reason about either. Splitting it is not a workaround for the tool; it is the change the codebase needed anyway, and it makes every subsequent edit cheaper and safer.
Frequently asked questions
Why do AI tools mangle long files?
Not from misunderstanding. A 2,000-line file is roughly 25,000 tokens, so asking for it back puts you near the output ceiling before anything new is written. You get truncation that returns HTTP 200, or 1,995 chances for silent drift.
What should I ask for instead of the whole file?
A search-and-replace patch with three to five lines of exact surrounding context. Output then scales with the change rather than the file, and a wrong edit fails loudly at apply time instead of landing quietly in your repository.
Should I add more context when the model gets confused?
Usually the opposite. Beyond a point more files dilute the relevant code, raise cost and push you toward context_length_exceeded. Send the function, its callers and the types it depends on.



