LLM Agent Capabilities and Token Cost Optimization: A Practical Guide
As large language models shift from answering questions to executing end-to-end tasks, engineering teams face two intertwined challenges: improving model capability and controlling the cost of that capability. This article breaks down the core dimensions of capability improvement, explains why total token consumption matters more than per-token price, and offers reusable strategies for cost optimization through model tiering, prefix caching, context pruning, and business-specific evaluation.
Introduction: The Shift from Conversation to Task Execution
Over the past two years, the role of large language models has changed significantly. Early models were used mainly for single-turn or multi-turn question answering, but the industry’s current focus has moved toward whether a model can reliably complete end-to-end, multi-step tasks—for example, generating presentations from templates, locating and fixing bugs in a code base, or operating graphical interfaces to complete cross-application workflows. This shift from “answering questions” to “executing tasks” raises the bar for reasoning coherence, tool-calling ability, and output stability. It also makes “how many tokens does it actually take to complete a task, and what does it cost” an unavoidable part of engineering decisions.
Setting aside product marketing, this article focuses on the technical theme itself: it systematically reviews the key dimensions of capability improvement and lays out reusable approaches to cost optimization.
Part 1: Three Core Dimensions of Capability Improvement
1. Professional Work and Structured Output
In enterprise scenarios, a model’s value often comes from its ability to produce “ready-to-use” structured results rather than generic text. This includes strictly following the templates and format constraints a user provides, keeping rows and columns aligned in tables and documents, and maintaining consistent terminology and style across long documents. In engineering terms, these capabilities are usually achieved through stricter instruction following and longer context retention.
2. Programming and Front-End Development
Programming ability should not be judged only by “can it write correct code,” but by overall performance in a real engineering environment: understanding cross-file dependencies, following existing code conventions, generating runnable front-end pages, and handling complex logic in graphical scenarios such as games and 3D rendering. These tasks are often multi-step, requiring the model to repeatedly read code, modify code, and run tests.
3. Computer Operation and Multi-Step Agent Tasks
The core of an agent task is the loop of “perceive–decide–act.” The model needs to observe the current environment state (such as the screen, terminal output, or file system), plan the next action, and be able to self-correct when something goes wrong. This capability directly determines whether a model can handle long-chain scenarios such as automated operations and batch data processing.
Part 2: Token Efficiency—The Overlooked Second Cost Curve
Many teams focus only on a low per-token price while ignoring a more critical cost curve: the total number of tokens consumed to complete the same task. If a new model achieves the same results with one-third of the output tokens, then even at an unchanged per-token price, actual spending drops significantly.
To understand the cost structure, it helps to distinguish three types of tokens:
- Input tokens (prompt): the entire context fed to the model;
- Output tokens (completion): the answers and code the model generates;
- Cached input tokens: a lower rate available when repeating a shared prefix (such as a system prompt or a long document).
In agent scenarios, conversation history and tool-returned results keep accumulating, so input tokens are often the largest cost driver. As a result, “completing a task with fewer tokens” usually means fewer wasteful round trips, more refined context pruning, and a higher cache hit rate.
Part 3: Model Tiering Strategy—Flagship and Lightweight Working Together
A mature approach is not “one model for everything,” but tiered routing based on task value:
- Flagship models: aimed at high-value, low-frequency tasks such as complex professional projects, scientific research, and sophisticated front-end and 3D design;
- Lightweight models: aimed at high-frequency daily tasks, routine code completion, and simple workflows, prioritizing efficiency and cost.
A practical rule of thumb: in many everyday evaluation scenarios, the gap between a lightweight model and a flagship model may be quite small, yet the cost can differ by several times. Dynamic routing based on this insight can substantially cut overall spending while preserving quality.
def route_task(task):"""Select the model tier based on task complexity to balance quality and cost"""if task.complexity in ("high", "multi_step"):return call_model("flagship", task) # Flagship: complex multi-step tasksreturn call_model("lightweight", task) # Lightweight: high-frequency daily tasks
Part 4: Inference Cost Composition and Optimization Levers
Beyond per-token price, cost optimization has several actionable levers:
- Prefix cache reuse: place stable system prompts, tool definitions, and long documents at the very front of the context to improve the cache hit rate;
- Context pruning: summarize agent history or apply a sliding window to avoid the linear growth of wasted tokens;
- Prompt refinement: remove redundant instructions and replace lengthy natural-language descriptions with structured templates;
- Task merging and batching: aggregate parallelizable small tasks to reduce the fixed overhead from repeated calls;
- Output constraints: for scenarios that do not need long text, explicitly limit the length to control output tokens.
A simplified estimation model can help quantify these decisions:
def estimate_cost(input_tokens, output_tokens,in_price, cache_price, out_price,cached_ratio=0.0):"""Roughly estimate the cost magnitude of a single call (prices are per unit)"""cached = input_tokens * cached_ratiouncached = input_tokens - cachedreturn (uncached * in_price+ cached * cache_price+ output_tokens * out_price) / 1_000_000
Part 5: Evaluation and Selection—How to Assess Models Scientifically
Judging by a single leaderboard ranking can easily lead to a wrong conclusion. It is better to build a multi-dimensional internal evaluation set covering the following categories:
- Agent tasks: completion rate and per-task cost for long-chain, multi-step tasks;
- Programming ability: bug fixing and feature implementation in real code bases;
- Scientific/reasoning ability: closed-domain problems that require deep reasoning;
- Everyday tasks: response quality and latency in high-frequency general scenarios.
The core principle is to “use your own business data as the ground truth.” Leaderboards can serve as a reference, but the final decision should be based on task samples that closely match the real distribution, while recording the token consumption and end-to-end cost of each task category to form a baseline for continuous comparison.
Part 6: Implementation Recommendations
Overall, application development for the agent era can follow this path:
- Start with task tiering: assign high-value, complex tasks to flagship models and high-frequency, simple tasks to lightweight models;
- Build cost observability: instrument each call to separately measure its input, output, and cached tokens;
- Optimize context structure rather than simply cutting per-token price—the gains from caching and pruning tend to be more durable;
- Use a business-specific evaluation set for selection to avoid being misled by a single leaderboard;
- Pay attention to stability in instruction following and structured output, since this determines whether results are truly “ready to use.”
Capability improvement and cost control are not in opposition. When a team treats “token efficiency” as a first principle of design and systematically optimizes through tiered routing, cache reuse, and context governance, it can strike a sustainable balance between complex agent tasks and everyday applications—turning large-model capability into affordable, scalable productivity.
