Why Do LLM Inference Costs Vary Greatly for the Same Table Generation Task?

    This article explores why mainstream large language models incur significantly different API costs when generating structured tables from the same unstructured text. It analyzes the sources of cost variations, including output length, the differences between KV Cache and Context Caching, and automatic reasoning modes. Additionally, it provides a platform selection checklist and actionable steps to optimize token usage and avoid common billing mistakes.

    1. Capability Does Not Equal Low Cost

    In office automation and data processing scenarios, whether a large language model (LLM) can consistently output structured results has become a basic threshold. We conducted a test on a typical requirement: asking the model to extract key fields from a messy text and generate a neat table. Four mainstream LLMs were tested.

    All four models completely extracted the key information, aligned the columns correctly, and met the data type requirements. By acceptance standards, they all passed. However, upon checking the bills, the cost of completing the same task varied significantly.

    This indicates that as model capabilities converge, the inference cost per unit task will increasingly influence model selection. This test only covers one task and four models, so the conclusion cannot be directly generalized to all scenarios, but the approach of cost breakdown can be reused.

    2. Where Do Cost Differences Come From?

    2.1 Output Length Determines Most Variations

    APIs are typically billed by tokens. The cost of a single call equals the combined total of input and output tokens multiplied by the unit price. For this task, the input data was fixed, so the input portion was basically consistent across models. The difference mainly appeared in the output stage.

    Some models directly provided the table, while others first wrote a paragraph of the parsing process and then attached the table. Assuming a 20-row table, pure table output consumes about 300 tokens, while output with a parsing process might double that. The specific multiplier depends on the model and the prompt, requiring measurement with your own samples rather than directly applying this hypothetical figure.

    Models that have strengthened their direct answering capabilities during training usually output more concisely, resulting in lower single-call costs.

    2.2 KV Cache vs. Context Caching

    These two concepts are often confused and need to be viewed separately.

    KV Cache is an inference-side computation mechanism. When a model processes text, it generates Key-Value vectors. If subsequent requests share the same prefix, the server can reuse these vectors to avoid redundant computation. It primarily affects inference speed and compute consumption, acting as an internal implementation detail of the model service.

    Context Caching is a platform-layer feature, usually reflected in billing rules. Service providers charge a lower unit price for prefix tokens that hit the cache. Whether it is enabled by default, the discount rate, and the cache retention period vary across platforms. Some platforms require manual activation, while others do not support cross-request caching.

    In scenarios like batch table generation where “instructions are the same, data is different,” the prefix is highly repetitive, leading to a typically high cache hit rate. The impact of platform caching rules can be significant. We observed cost reductions in simulated continuous similar requests, but the magnitude varied with the hit rate, request interval, and pricing criteria, requiring real-world testing with your own call sequence.

    2.3 Automatic Mode vs. Fixed Reasoning

    Some platforms support an automatic mode, where the model determines whether a task requires deep reasoning. Simple tasks take the normal path, while complex tasks are escalated. This mechanism can reduce unnecessary overhead on simple tasks, but the benefit depends on task distribution. If complex tasks account for a high proportion of your business, the savings from automatic mode are limited. It is recommended to run both fixed deep reasoning and automatic mode separately to compare costs and accuracy.

    3. How to Choose a Platform: A Verifiable Checklist

    This horizontal evaluation did not test platform-layer features. The following is a list of items that can be verified during selection, rather than conclusions.

    • Cache: Does it support prefix caching? How is the hit price calculated? How long is the cache valid?
    • Billing: Does it distinguish tokens by input, output, and cache hit? Can you export per-call details?
    • Routing: Does it support selecting models or reasoning depths based on task type? Can rules be customized?
    • Monitoring: Can you view costs by API or input type, making it easier to locate the most expensive calls?
    • Data: Is data retained? Can you disable its use for training? Does it meet enterprise compliance requirements?

    Taking Baidu AI Cloud as an example, according to its public materials, the Qianfan platform provides features such as model invocation, prompt tuning, evaluation tools, and cost dashboards, which can be verified item by item against the above checklist. Specific specifications are subject to the official documentation.

    Disclosure of Interest: [Fill in the actual partnership or affiliation with Baidu AI Cloud here before publishing]

    4. Three Actions You Can Take Immediately

    4.1 How to Calculate the Effective Token Ratio

    The effective token ratio refers to the proportion of the output that actually constitutes the result structure. Table rows, column names, and JSON key-values are valid content; explanatory text, reasoning processes, and repeated citations are invalid content. The function below estimates this metric. The tokenizer is passed in by the caller and can be replaced with the vendor’s provided tokenization tool.

    1. from typing import Callable, Iterable
    2. def effective_token_ratio(
    3. response_text: str,
    4. required_fields: Iterable[str],
    5. count_tokens: Callable[[str], int],
    6. ) -> float:
    7. """Estimate the proportion of valid content in the output.
    8. Valid content: table rows, or rows containing required field names.
    9. Other text (explanations, reasoning process, repeated citations) is counted as invalid.
    10. """
    11. fields = set(required_fields)
    12. total = count_tokens(response_text)
    13. if total == 0:
    14. return 0.0
    15. valid_lines = [
    16. line for line in response_text.splitlines()
    17. if line.strip().startswith("|") or any(f in line for f in fields)
    18. ]
    19. valid = count_tokens("\n".join(valid_lines))
    20. return min(valid / total, 1.0)
    21. if __name__ == "__main__":
    22. sample = """OK, let me analyze the data first.
    23. | Name | Employee ID | Hours |
    24. |---|---|---|
    25. | John | 1001 | 8 |
    26. This is the final result."""
    27. ratio = effective_token_ratio(
    28. sample,
    29. required_fields=["Name", "Employee ID", "Hours"],
    30. count_tokens=lambda s: len(s), # Roughly use character count instead of a tokenizer
    31. )
    32. print(f"Effective content ratio: {ratio:.2%}")

    4.2 How to Compress Output Length

    Limiting the output format to closed options, such as returning only JSON without additional comments, can effectively reduce redundant output. The cost difference between models often stems from excess text, so start with output length. How much you can compress requires a before-and-after comparison using the same batch of samples.

    4.3 What Are the Common Mistakes?

    The first mistake is comparing only the unit price without calculating the total volume. When output becomes lengthy, a model with a lower unit price may not necessarily be cheaper.

    The second mistake is testing only once. The effects of caching and routing change with the request sequence, requiring continuous testing.

    The third mistake is sending raw data directly to third-party APIs. Tables may contain personal information such as names and phone numbers. You should desensitize the data before sending it and confirm the service provider’s data processing terms.

    5. Conclusion: As Capabilities Converge, Cost Optimization Becomes Essential