A Practical Guide to Token Cost Optimization for Office AI Agents

    This article explains why the cost of office AI agents comes from the execution framework rather than individual model calls, and how to reduce it. It covers three core cost levers—prefix cache hit rate, output length and structured constraints, and tiered reasoning depth—along with task-based routing that sends most routine work to low-cost models. It also outlines product design for quotas and free usage, observability metrics for continuous tuning, and a deployment checklist. The intended takeaway is that stable, low-cost daily tasks depend on execution framework engineering, not just model selection.

    1. The Real Problem: Agent Cost Comes From Execution, Not Calls

    When teams deploy agents in office workflows, the most common question is why the same model on the same task can produce bills that differ by several times. The cost of a single task is not the price of one model call. It accumulates across several intermediate stages:

    • Repeated context transfer: system prompts, tool definitions, and message history are counted again as input across multi-turn tool calls.
    • Redundant output: models tend to produce long explanations, repeated tables, and unnecessary pleasantries.
    • Over-reasoning: simple tasks receive an excessive reasoning budget, generating a large amount of wasted thinking.
    • Failed retries: invalid tool arguments or structured output parsing failures force an entire turn to be rerun.

    The right place to start optimizing cost is therefore the execution framework (often called the harness)—the engineering layer around the model that handles context assembly, tool orchestration, retries, and truncation—rather than simply switching models.

    2. Three Core Cost Levers

    2.1 Cache Hit Rate

    Place the stable, unchanging prefix (role definition, tool schemas, specification documents, long tables) at the very front of the context and keep it byte-for-byte stable. This allows prefix caching to take effect, so that portion of the input is billed at a rate far below the standard price. A schematic context layering looks like this:

    1. [Stable prefix] Role + tool definitions + output spec + reference docs
    2. [Semi-stable zone] Conversation summary (updated only at turn boundaries)
    3. [Volatile tail] Current user instruction and tool return results

    Practical points: do not insert timestamps or random request IDs into the system prompt; tool definition changes should go through versioned releases; template rendering must keep whitespace consistent.

    2.2 Output Length and Structured Constraints

    Output tokens are usually billed at a higher price and directly determine the input size of subsequent turns. They can be constrained with a “contract first, generate later” approach:

    1. {"task":"data_summary","fields":["conclusion","key_metrics","risks"],"max_chars":600}

    Combined with JSON Schema validation and localized retries on failure (rerunning only the fields that failed to parse), this significantly reduces the waste caused by rerunning an entire turn.

    2.3 Tiered Reasoning Depth

    Label tasks with a difficulty level: turn off deep thinking and narrow the retrieval scope for simple tasks; open up the reasoning budget only for complex tasks. The grading basis can come from observable metrics such as task type, input length, and historical first-pass rate.

    3. Task-Based Routing: Put 80% of Tasks in the Low-Cost Lane

    Office requests show a clear long-tail distribution. High-frequency tasks such as format conversion, key-point extraction, email polishing, table cleaning, and weekly report generation have clear rules and strong verifiability, and can be fully handled by a lightweight model plus a deterministic tool chain. Only a few tasks—such as investment research modeling, cross-document auditing, and complex application development—require a high-capability model.

    A runnable routing skeleton:

    1. def route(task):
    2. if task.risk == "low" and task.steps <= 3:
    3. return {"model": "lite", "thinking": False, "cache": True}
    4. if task.risk == "medium":
    5. return {"model": "standard", "thinking": True, "max_output": 2000}
    6. return {"model": "pro", "thinking": True, "verification": "dual_run"}

    The value of routing is shifting the cost structure from “paying for the worst case” to “paying for actual demand.” This is the fundamental reason most teams can bring the average cost of daily tasks down to roughly one tenth of comparable approaches.

    4. Productizing Quotas and Free Usage

    Low-cost execution only feels “free” to users when paired with sensible quota operations. Common practices include: no threshold for low-consumption tasks and simple conversations; time-limited trials for new users; daily check-ins that grant quota with increasing rewards for consecutive check-ins; and clear quota conversion hints retained for high-consumption tasks. Three bottom lines should be followed in the design: rules must be queryable, consumption must be explainable, and balance must trigger advance warnings, so that opaque billing does not erode trust.

    5. Observability and Continuous Tuning

    Optimization cannot rely on intuition; it requires a closed metric loop: cache hit rate, average input/output tokens, task first-pass rate, retry rate, cost per task, and the share of free-mechanism triggers. It is recommended to build a dashboard with task type as the main dimension, and to run canary comparisons for every framework change, using the same task set for regression validation that “cost drops but quality does not.”

    6. Deployment Checklist

    • Is the stable prefix truly byte-for-byte stable?
    • Is the output constrained by a schema and a length limit?
    • Is there any unnecessary deep reasoning?
    • Is there risk-based model routing?
    • Are failed retries localized?
    • Are quota rules and consumption details visible to users?

    7. Conclusion

    The cost-performance competition among office agents is essentially a competition of execution framework engineering capability. Only by eliminating waste in caching, output, and reasoning one by one, and then delivering to users through task-based routing and a transparent quota mechanism, can high-frequency daily tasks reliably fall into the zero-cost or extremely low-cost range. To implement this on Baidu AI Cloud, you can use the general-purpose category capabilities to carry this architecture: deploy the execution framework on the container platform, manage templates and documents with object storage, decouple tool calls with a message queue, and build a cost dashboard through log services and monitoring alerts.