Context Execution Mode Tested: Why Costs Dropped 13%, Not 77%

    This article reports a controlled experiment comparing traditional multi-turn tool calling with a code execution mode, where a model generates a script that runs in a sandbox and returns only the necessary results. Using the same model, prompt, repository state, and test command, three alternating rounds per mode show an average cost reduction of about 13% and roughly flat latency, while cache reads fall by more than 30%. The article explains why token savings do not translate directly into bill savings, how cache writes act as a floor cost, which task types benefit most, and how teams can decide whether to enable this mode.

    1. Background: Why “77% Fewer Tokens” Does Not Translate to Your Bill

    In discussions about the cost of AI coding tools, the easiest thing to misread is the equivalence between a “token reduction” and a “cost reduction.” Industry reports often cite figures such as “total tokens reduced by more than 60%, up to nearly 77%.” That number describes how the context size a model has to process during a task changes—not how the bill changes. Several variables sit in between: cache pricing, the input-to-output ratio, and the number of tool-calling rounds.

    This article answers a specific question: when the traditional interaction pattern—where the model reads context turn by turn and calls external tools step by step—is rewritten so that the model generates executable code, the code performs multiple steps inside a sandbox, and only the necessary results are returned (referred to below as “code execution mode”), how much does the real bill actually shrink, does speed suffer, and which tasks are worth enabling it for?

    2. Experiment Design: Making the Results Reproducible

    2.1 Control Setup

    The experiment uses a single-variable control: the same model, the same prompt, the same code repository state, and the same test command, with only the context organization method switched. The task itself is deliberately lightweight—modify one local file, then run the tests once.

    • Normal mode: the model reads files, locates the change point, writes the file back, and triggers the tests through multiple tool-calling rounds.
    • Code execution mode: the model generates a script in one shot, which completes the read, rewrite, and test trigger in a controlled environment and returns only a summary and failure information.

    2.2 Metrics

    Cost alone is not enough. At least four metrics must be recorded at the same time: total task cost, task duration, cache read volume, and cache write volume. The first two determine the user experience; the latter two determine the cost structure.

    2.3 Rounds

    Six rounds were run in total, three per mode, executed alternately to offset load fluctuations across time periods. Cost and duration for each round are shown below.

    Round Normal mode cost Code execution mode cost Normal mode duration Code execution mode duration
    1 0.1813 0.1480 26s 23s
    2 0.1794 0.1763 28s 32s
    3 0.1841 0.1516 28s 25s
    Average 0.1816 0.1586 27.3s 26.7s

    3. Reading the Results: Where 13% Comes From, and Why Not 77%

    3.1 Cost: A Stable but Limited Gain

    The three-round average for normal mode is about 0.1816, and about 0.1586 for code execution mode—a reduction of roughly 13%. Round 2 is worth noting: code execution mode was less than 2% cheaper, which shows the gain is not constant but fluctuates with the task and the context state.

    3.2 Cache Reads: A Drop of More Than 30%

    In the usage statistics, cache read shows the clearest gap. For normal mode, the three rounds were about 317,000, 264,000, and 381,000 tokens; for code execution mode, about 178,000, 265,000, and 178,000—an average reduction of more than 30%. This is exactly where “saving a lot of tokens” comes from: multiple tool-calling rounds repeatedly pull file contents, directory structures, and command output into the context, while code execution mode handles that shuttling locally.

    3.3 The Key Contradiction: Tokens Down 30%, Cost Down Only 10%

    The difference is eaten up by cache writes. Each round still incurs about 28,000 tokens of cache writes, and the write price per token is significantly higher than the read price. This overhead exists at roughly the same level whether or not code execution mode is enabled. That explains a common confusion: why the usage panel looks strongly optimized while the bill improves only slightly.

    A simplified relationship helps:

    1. total cost ≈ read volume × read price
    2. + write volume × write price
    3. + output volume × output price

    When an optimization only acts on “read volume,” the overall reduction is naturally diluted by the other two terms.

    3.4 Speed: No Penalty, No Bonus

    Normal mode took 26–28 seconds; code execution mode took 23–32 seconds, with the variation falling in the normal range. For lightweight tasks, code execution mode removes the multiple round trips but adds the overhead of generating and executing a script, and the two roughly cancel out.

    4. Why the Reported Range Varies So Much: Task Complexity Sets the Ceiling

    The industry figure of “more than 20% cost reduction on multi-step development tasks” does not conflict with the 13% here; the difference comes from task structure:

    • The denser the tool calls and the more frequent the external retrieval, the more context gets shuttled repeatedly, and the closer the code execution mode’s gain gets to the upper bound.
    • The simpler the task and the more concentrated the change, the less redundancy can be compressed, and the gain naturally falls back to around 10%.
    • Tasks dominated by output content (such as long document generation) are almost unaffected by this mode.

    Therefore, treating “up to 77% savings” as a budget baseline is not rigorous. A more reasonable approach is to first measure your team’s task distribution, then run a small-scale controlled experiment to determine the gain range for your own scenario.

    5. Implementation Advice: How to Decide Whether to Enable It

    1. Build a minimal controlled experiment. Fix the model and prompt, switch only the context organization method, run no fewer than three rounds, and execute alternately.
    2. Watch all four metrics. Cost, duration, cache read, and cache write are all required; avoid being misled by a single usage number.
    3. Account for failure costs. Retry rounds when script execution fails can significantly raise costs and should be tracked separately.
    4. Decide by task tier. Prioritize enabling it for tasks with high-frequency tool calls and multi-step retrieval; lightweight single-file changes can enable it as needed.
    5. Handle permissions and isolation. Code execution means running generated content in a sandbox; restrict the filesystem scope, network egress, and the allowlist of executable commands, and keep complete audit logs.
    6. Combine it with cost governance. Context trimming, summarizing retrieval results, and a systematic logging and monitoring/alerting setup can all further reduce read volume.

    6. Conclusion

    Code execution mode is not a “cost reduction switch” but an optimization of how context is organized: by cutting repeated shuttling, it pushes cache reads down by about 30%, which translates into a real cost reduction of about 13% on lightweight tasks with roughly flat duration. Only in tool-call-intensive multi-step tasks can the gain approach—or exceed—the 20%+ reported in industry coverage. Understanding cache writes as a “floor cost” is more valuable than memorizing any single percentage. For teams doing cost governance on cloud platforms such as Baidu AI Cloud, the reusable path is to quantify task structure first, run a controlled experiment next, and then enable the mode by tier.