LLM API Cost Optimization and Practical Applications of an AI Code Assistant

    This article explains how to balance capability and cost when choosing an LLM API for developer workflows. It covers how per-token pricing, cache reads, and limited-time promotions affect a realistic workload, then evaluates a multimodal model on five criteria. It also outlines the architecture of a low-cost code review agent and includes a minimal Python example.

    1. Background: Why Developers Are Reassessing LLM API Costs

    Recently, while refactoring our team’s internal automation scripts, I again researched the call costs and capabilities of mainstream LLM APIs. Over the past year, pricing competition among cloud providers in the LLM space has become very intense. For developers, this competition has steadily pushed call prices down. It has also raised a new question: with so many model options, how do you find the right balance between capability and cost?

    In practice, we worry most about two things. First, model quality fluctuates, which leads to unstable output and frequent rework in downstream tasks. Second, API pricing is complex and changes often, so a workflow that works today can push the bill far over budget tomorrow. This is especially true for high-frequency calls that process long contexts, such as code review, document analysis, and automated test generation. These tasks consume a large number of tokens. Even a difference of a few cents per call adds up to a significant expense across thousands of calls.

    Therefore, building a code assistant agent that is capable enough and cost-controllable has become an important project for our team.

    2. Cost Accounting: Breaking Down Model Call Fees

    Before selecting a model, we need to understand how LLM API billing works. Typically, cloud providers charge separately for input tokens and output tokens. Many also offer a lower rate for cache reads.

    Take a multimodal model recently launched by a cloud provider as an example. Its pricing structure is roughly as follows:

    Billing item Standard price (CNY per million tokens) Promotional price (CNY per million tokens)
    Input (including context) 0.8 0.4
    Output (generated results) 2.8 1.4
    Cache read 0.23 0.115

    During the limited-time promotion, input and output costs both drop to 50% of the standard price. For high-frequency users, this halves the overall cost. Cache reads also deserve attention. When an application repeatedly sends similar context within a short time, cache hits can significantly reduce input costs.

    Cost Estimate for a Realistic Scenario

    Suppose we write a script that analyzes the code repository of a mid-sized project and generates optimization suggestions and fixed code. The input and output requirements are:

    • Input: about 500,000 tokens of project context, including code files, dependency configurations, and comments
    • Output: about 20,000 tokens of optimization suggestions and code snippets

    At standard prices:

    (0.5 × 0.8) + (0.02 × 2.8) = 0.456 CNY per call

    At promotional prices:

    (0.5 × 0.4) + (0.02 × 1.4) = 0.228 CNY per call

    A single call costs about 50% less. If the team makes several hundred to a thousand calls a month, the savings add up to a substantial amount.

    Cost Optimization Goes Beyond Unit Price

    Unit price is only part of the picture. Engineers should also think about the total cost of the full pipeline. Pay particular attention to these three areas:

    1. Context compression: Not every code file needs to be sent in full. Extracting key classes, function signatures, and dependency graphs in a structured way can reduce input tokens to about one third of the original while preserving the essential information.
    2. Caching strategy: For identical or similar project structures, reuse cache reads where possible to reduce charges for repeated input.
    3. Output length control: Use precise prompt instructions to ask the model for only the necessary code and brief explanations. This avoids redundant content and keeps output tokens in check.

    3. Model Selection