Making Token Usage Observable in AI Coding Assistants

    When an AI coding assistant consumes more of the context window than expected, a total token count rarely explains why. By breaking context usage into system instructions, skills, agents, tools, plugins, and task-specific content, developers can distinguish fixed costs from variable costs, establish baselines, and optimize context usage based on measured data rather than guesswork.

    1. A Bill Without Line Items

    When using an AI coding assistant, it is common to see a small conversation consume a surprisingly large portion of the context window. Tokens are clearly being used, but users often cannot determine which components are responsible.

    This is similar to receiving an electricity bill that shows only the total amount. The total tells you that consumption is high, but not whether the air conditioner, refrigerator, or aging dryer is the main source. You have to turn devices off one by one to investigate, which is expensive and produces inconsistent conclusions.

    Token consumption in large language model applications presents the same problem. Without a breakdown, optimization depends on experience and guesswork. Splitting the total into detailed categories provides a reliable basis for improvement.

    2. The Persistent Members of a Context

    Before the user types the first character, the context may already contain several types of content. These items continue to consume part of the token budget and are not automatically released as more conversation turns are added. The main categories include the following.

    2.1 System Instructions

    System prompts define the assistant’s behavior, output format, and safety constraints. They are usually a fixed cost included with every request. Their length directly affects the baseline cost of each conversation turn.

    2.2 Skills

    Skills package reusable instructions and tools into modular units. They are designed to be loaded when needed, but once a skill is enabled, its description and instruction text remain in the context. If multiple skills are active, their usage can accumulate over time.

    2.3 Agents

    Agents are independent execution units that the primary assistant can dispatch to handle subtasks. Each agent requires its own role definition, objective, and invocation conventions. As the number of agents increases, the total length of their initial descriptions also grows. They may consume context space even before they are actually called.

    2.4 External Tool Connections

    Through a common external tool connection protocol, a model can access databases, call APIs, or read and write files. Each connected tool requires its name, parameter schema, and capability description to be added to the context so the model knows that the tool exists and how to use it. The more detailed the description, the greater the token usage for that tool.

    2.5 Plugins

    Plugins often bundle multiple capabilities and inject related descriptive text into the context. Both the number of plugins and the level of detail in their descriptions affect token consumption.

    3. Why Usage Details Matter

    The engineering community has long followed a basic principle: observe first, then optimize. Server performance tuning depends on resource monitoring, database optimization depends on execution plans, and frontend performance work depends on analyzing load times. Without metrics, the effect of any change can only be judged by intuition.

    Token usage details serve the same purpose. They map context costs to specific sources and answer three questions:

    1. What components make up the current context, and how much does each consume?
    2. Which components are fixed costs, and which vary with the task?
    3. How much context space can be recovered by removing or disabling a given component?

    With these answers, users can make informed trade-offs. For example, if a plugin consumes a significant share of the context but is irrelevant to the current task, disabling it can immediately free space.

    4. Building a Cost Model

    When official usage details are unavailable, users can create a rough cost model. The basic approach is to estimate token counts by category and then aggregate the results. The following example illustrates the calculation logic. The field names are hypothetical and do not represent the interface of any specific product.

    1. # Example: aggregate token usage by context category
    2. context_items = [
    3. {"category": "system", "name": "Base instructions", "tokens": 1800, "always_on": True},
    4. {"category": "skill", "name": "Code review skill", "tokens": 950, "always_on": False},
    5. {"category": "agent", "name": "Test agent", "tokens": 620, "always_on": False},
    6. {"category": "tool", "name": "Database connection", "tokens": 1400, "always_on": False},
    7. {"category": "plugin", "name": "Documentation plugin", "tokens": 780, "always_on": False},
    8. ]
    9. total = sum(item["tokens"] for item in context_items)
    10. summary = {}
    11. for item in context_items:
    12. summary.setdefault(item["category"], 0)
    13. summary[item["category"]] += item["tokens"]
    14. for category, tokens in sorted(summary.items(), key=lambda kv: -kv[1]):
    15. print(f"{category:<8} {tokens:>6} tokens {tokens / total:6.1%}")
    16. # Items that can be considered for disabling: non-persistent and irrelevant to the current task
    17. removable = [i for i in context_items if not i["always_on"]]
    18. print("Items to evaluate for disabling:", [i["name"] for i in removable])

    The value of this code lies in the process: categorize, count, calculate proportions, and filter. Users can replace the sample data with real measurements. The numbers should come from actual observation rather than assumptions.

    5. A Practical Process for Observation and Optimization

    An actionable workflow typically includes the following five steps.

    Step 1: Inventory. List the system instructions, skills, agents, tools, and plugins enabled in the current session. Record the name and purpose of each item.

    Step 2: Measure. Estimate or measure the token usage of every item. Fixed costs can be measured once in an empty session, while dynamic costs should be sampled multiple times across representative tasks.

    Step 3: Establish a baseline. Record the total usage of typical tasks as a reference for future comparisons. Without a baseline, it is impossible to determine whether an optimization had a meaningful effect.

    Step 4: Enable and disable as needed. Remove components that are unnecessary for the current task. For example, a task focused solely on document writing will usually not require database-related tools.

    Step 5: Measure again. Recalculate usage after making changes and compare it with the baseline. If the savings are not significant, check whether another component grew at the same time.

    6. Optimization Principles and Common Mistakes

    6.1 Do Not Optimize for the Lowest Possible Count

    Enabling more capabilities is not inherently a problem. The key issue is whether those capabilities match the task. Disabling everything may reduce token usage, but it can also make the assistant less useful and create a worse overall trade-off.

    6.2 Keep Descriptions Concise

    Instructions for the same capability can be either detailed or concise. As long as the model can call the capability correctly, removing redundant wording and repeated examples can provide consistent savings.

    6.3 Watch the Upfront Cost of Agents

    Agents are valuable when they handle independent tasks. However, every agent carries a role definition and other setup text. If an agent is enabled only for a simple step, its upfront cost may exceed its practical benefit.

    6.4 Separate Fixed Costs from Incremental Costs

    System instructions are a fixed cost that appears in every request. Their optimization space may be limited, but their impact is broad. Tool results, file contents, and similar data are incremental costs that vary by task and are often more directly controllable.

    7. Broader Implications

    Token usage observability turns a black box into a measurable system. As AI coding assistants become part of production workflows, teams need to manage not only cost, but also response speed and context quality. Breaking context usage into categories, measuring it, comparing results, and making adjustments creates a general engineering loop that can be applied across different model services and development tools.

    Teams can also incorporate usage data into routine monitoring, define thresholds for unusual consumption, and periodically review which capabilities are enabled. This shifts cost management from after-the-fact reconciliation to proactive planning.