← Back to Blog

How to Use .aiignore and Context Exclusion to Cut Token Costs

By Eric Bush · July 15, 2026 · 6 min read

Round eyeglasses resting on a sheet of handwritten notes on a desk

The Files You Pay For but Never Need

When an AI coding agent works on your repository, it pulls files into its context to understand the code. The problem is that a lot of what lives in a repo is noise the model does not need: build artifacts, dependency folders, lock files, minified bundles, generated code, logs, and test fixtures. Every one of those tokens, once loaded into context, is an input token you pay for — often on every single turn.

A single package-lock.json or a vendored dependency directory can be tens of thousands of tokens of pure overhead. If your agent scans it repeatedly across a long session, you are paying for that dead weight over and over. Context exclusion is the fix, and it is one of the highest-leverage, lowest-effort cost optimizations available.

What Is an .aiignore File?

Many AI coding tools now support an ignore file — commonly named .aiignore, .cursorignore, or similar — that works just like .gitignore. You list glob patterns for files and directories the agent should never read, and the tool skips them when building context. Some tools also respect your existing .gitignore by default, but relying on that alone is not enough: plenty of files you commit to git (lock files, generated docs, large data fixtures) are still useless to the model.

Check your specific tool's documentation for the exact filename and precedence rules, since they vary. The concept is universal even when the syntax differs.

What to Exclude

A good starting exclusion list for most projects:

  • Dependencies: node_modules/, vendor/, .venv/, target/ — the model should reason about your code, not your libraries' source.
  • Lock files: package-lock.json, yarn.lock, poetry.lock — huge, and almost never relevant to a coding task.
  • Build output: dist/, build/, .next/, minified and compiled files.
  • Generated code: protobuf output, ORM schemas, API client stubs — regenerable and verbose.
  • Data and logs: large JSON/CSV fixtures, .log files, snapshots, media assets.

A word of caution: exclude aggressively but not blindly. If the agent genuinely needs to understand a generated schema to write correct code, excluding it will cause errors that cost more than the tokens you saved. Exclude what is truly noise, keep what carries signal.

The Savings, Quantified

Suppose your agent would otherwise load 60K tokens of context per turn, and 25K of that is node_modules snippets, a lock file, and build output. Excluding them drops you to 35K tokens per turn — a 42% cut in input tokens. Over a 50-turn session at $3 per million input tokens, that is the difference between $9 and roughly $5.25 in input cost for the same work. Scale that across a team and a month, and context exclusion pays for the five minutes it took to set up thousands of times over.

There is a quality bonus too: a leaner context helps the model focus on the code that matters, which often improves the answers. You pay less and get better output.

Setting It Up

Getting started takes minutes:

  • Create the ignore file your tool supports at the repo root.
  • Start from your .gitignore, then add the extra items git tracks but the model doesn't need (lock files, generated code, big fixtures).
  • Commit it so the whole team benefits from the same exclusions.
  • Watch your token usage before and after to confirm the drop — and adjust if the agent starts missing context it actually needs.

Context exclusion is the rare optimization with almost no downside: less cost, faster responses, and often better answers. To see how much a leaner context saves at your scale, model your per-turn token count in the AI Cost Estimator both before and after trimming.

Want to calculate exact costs for your project?

Frequently Asked Questions

What is an .aiignore file?

It's a file — named .aiignore, .cursorignore, or similar depending on your tool — that works like .gitignore. You list glob patterns for files and directories the AI agent should never read, and the tool skips them when building context, so you stop paying input tokens for irrelevant files.

What files should I exclude from AI context?

Dependencies (node_modules, vendor, .venv), lock files (package-lock.json, yarn.lock), build output (dist, build, minified files), generated code (protobuf, ORM schemas, API stubs), and large data/log files. Exclude true noise, but keep files that carry signal the model needs to write correct code.

How much can context exclusion save on token costs?

Substantial amounts. If excluding node_modules, lock files, and build output cuts per-turn context from 60K to 35K tokens, that's a 42% input-token reduction. Over a 50-turn session at $3/M input, cost drops from about $9 to $5.25 for the same work — and it compounds across a team and a month.

Does excluding files from AI context hurt quality?

Usually the opposite — a leaner context helps the model focus on relevant code and often improves answers. The one risk is excluding something the agent genuinely needs (like a generated schema it must reference), which causes errors. Exclude noise, keep signal, and monitor for missing-context issues.