Lesson 3: Context Budgeting & Token Management

Understanding the "Context Tax." How multi-file scanning multiplies token costs and how modular sub-rules protect your wallet. Breaking a giant rules file into modular domain rules (ui-style.md, database.md).

By • Last updated • 65 min

Every codebase search and multi-file read contributes to the "Context Tax." In this lesson, we will learn how to budget tokens, estimate costs, and split giant system rules into lean, modular domain files.

Theory: The Context Tax

When an AI agent scans a repository, it feeds the content of relevant files into the prompt context window. Because modern models charge per input token, reading 10 files recursively can easily consume 20,000 to 50,000 tokens per action.

Calculating Context Costs

Context tax is cumulative. As the chat grows longer, the agent re-sends the entire conversation history along with all read files:

1 Agent Iteration
~5,000 tokens
$0.015
10 Iterations (Standard)
~75,000 tokens
$0.225
50 Iterations (Complex)
~750,000 tokens
$2.25

Cost estimates are based on an average LLM developer pricing of $3.00 per million input tokens.

Hands-on: Modularizing Domain Rules

Instead of stuffing all style requirements, component guidelines, database schemas, and testing instructions into a single, massive AGENT.md file, adopt a **modular domain structure**:

  • Root Constitution (AGENT.md): Keep this brief. Include general directives, build scripts, and pointers to domain rules.
  • UI Guidelines (.rules/ui-style.md): Detail Tailwind styles, CSS conventions, and React layout parameters. The agent only reads this when changing front-end assets.
  • Database Constraints (.rules/database.md): Outline ORM setups, column schemas, and migration steps. The agent loads this only when executing database changes.

Context Budget Check

How can a developer reduce the context tax and token cost when working with multi-file repository-level agents?

Coding Challenge: Context Token Cost Estimator

Let's code a workspace context estimator in Python! Write a helper function that reads a list of files with their sizes in characters, estimates the total token count (assuming 4 characters equal 1 token), and calculates the financial cost at a rate of $3.00 per 1 million tokens.

  1. Create a dictionary mapping file names to character lengths (e.g. { 'main.py': 800, 'style.css': 3400, 'schema.db': 12000 }).
  2. Calculate the total character count and divide by 4 to get estimated tokens.
  3. Multiply the total tokens by 0.000003 ($3.00 / 1,000,000) to find the cost.
  4. Print out the estimated tokens, formatting the cost as a dollar value (e.g., $0.0123).

Write this token budget calculator script in the workspace editor on the right!

main.py
Run your code to see output here.