A Practical Guide to LLM API Cost Optimization

    Why a lower per-token price does not guarantee a lower bill, and how to evaluate model costs by breaking down billing components, measuring task-level token consumption, and comparing subscription plans with pay-as-you-go pricing on the same basis. The guide includes a practical optimization checklist and a sample gateway wrapper for observability and model switching.

    1. The Problem: Why a Low Unit Price Does Not Mean a Low Bill

    Over the past year or two, LLM service pricing has changed frequently. Within the same vendor, different model generations, and pay-as-you-go versus plan-based billing, can differ by several multiples. The most common mistake developers make when choosing a model is to look only at the price per million tokens and infer that the total cost is low. In reality, the final bill is determined by several billing components, and comparing any single one can lead to the opposite conclusion.

    The situation gets more complicated once a model’s capabilities improve, because the number of tokens needed to complete the same business task changes as well. A “smarter” model may finish the same work in fewer conversation turns, with fewer file reads and writes, and fewer retries. Its unit price may be higher, yet total spending may be lower. Conversely, a model that needs repeated probing and frequent error correction can push costs to an unacceptable level. Cost evaluation therefore has to consider two dimensions at the same time: the price structure and token efficiency.

    This article does not benchmark any specific product. Instead, it abstracts a general method: how to break down billing components, how to measure task-level token consumption, and how to compare plans with pay-as-you-go billing on the same basis.

    2. Breaking Down the Billing Structure: How Many Token Types Are in a Single Request

    The billing components of mainstream LLM APIs usually fall into four categories, and understanding the differences among them is the prerequisite for cost optimization.

    • Uncached input tokens: the part of the prompt in this request that is not reused from a cache. This usually has the highest unit price.
    • Cached input tokens: the part that exactly matches the prefix of previous requests and is reused by the server. The unit price is generally significantly lower than regular input.
    • Output tokens: the content generated by the model. The unit price is usually higher than input.
    • Add-ons: some services bill long context, specific capabilities, or storage-type resources separately. Check the official documentation to confirm.

    There is one easily overlooked lever here: cached input often accounts for the overwhelming majority in long-running tasks. Take an agent-style workflow as an example. The system prompt, tool definitions, conversation history, and files already read form a very long, stable prefix that is carried in every request round. If the cached input unit price is several times that of regular input…