Choosing LLMs for AI Agents: Calculating Costs Beyond Free Tiers

    Selecting the right large language model (LLM) for AI agents requires more than just finding a provider with free API credits. This article explores key factors in agent model selection, including how multi-step task loops amplify token costs, the practical trade-offs of Mixture of Experts (MoE) architectures, and strategies for optimizing expenses with context caching. It also provides a framework for evaluating tool-calling reliability to ensure robust agent performance.

    Introduction: Recalculating Selection Criteria After Free Tiers

    Recently, a popular open-source AI agent framework announced it would offer a batch of free API credits to community users, covering general conversation and code generation. This sparked enthusiastic discussions among developers. For many, free credits mean they can try out the model without paying upfront.

    However, free credits only answer “who pays,” not “whether this model fits my agent.” This article outlines a model selection strategy from three perspectives: the specific requirements of agent scenarios, the practical trade-offs of Mixture of Experts (MoE) architectures, and context caching with cost accounting methods.

    How to Choose LLMs for AI Agents

    First, consider the invocation pattern. A standard chat usually requires a single model call, whereas an agent task (e.g., completing a code refactoring) involves a loop of “planning, executing, observing, and re-planning,” often triggering dozens or even hundreds of calls. Each call reads the context, and each call’s latency adds to the total task time.

    Therefore, selection should focus on three aspects:

    • Price and latency per single call;
    • Long context handling capabilities and caching support;
    • Stability of structured output (e.g., function calling).

    The first two determine cost and speed, while the third determines whether the task can run successfully. A model might be cheap per token, but if its tool-calling format frequently fails, the increased retry rate can make the total cost higher.

    How Invocation Frequency Amplifies Costs

    The assumption that “halving the unit price halves the total cost” only holds true if the number of calls remains constant. Agent tasks involve numerous calls, so small price differences per call accumulate, and the absolute amount scales linearly with task volume.

    Let’s use a set of hypothetical numbers to demonstrate. Assume Model A has an input price of $0.004 per 1K tokens and an output price of $0.012 per 1K tokens, while Model B’s prices are exactly half. A single task averages 20K input tokens and 1K output tokens per call, with a total of 80 calls.

    Item Original Price Model Half Price Model
    Single call input cost $0.080 $0.040
    Single call output cost $0.012 $0.006
    Total per call $0.092 $0.046
    Single task (80 calls) $7.36 $3.68
    1000 tasks per day $7360 $3680

    Saving $3.68 on a single task might not seem like much. However, at 1,000 tasks per day, the difference reaches $3,680 daily. Agent model selection should calculate costs based on task volume, not just unit price. Note: These prices are for demonstration only and do not reflect any vendor’s actual pricing.

    Practical Trade-offs of MoE Architecture

    Mixture of Experts (MoE) models consist of multiple expert subnetworks and a router. The total parameter count is large, but each token only passes through the few experts selected by the router.

    Therefore, two figures must be evaluated separately:

    • Total parameter count affects the model’s knowledge capacity and capability ceiling;
    • Activated parameter count affects the computation required per inference, which in turn impacts speed and unit cost.

    A large total parameter count does not mean high computation per call, nor does it guarantee slower performance. The actual performance of MoE models is also influenced by:

    • Routing balance: When a large number of tokens concentrate on a few experts, those experts become bottlenecks, increasing latency;
    • Batch size: The degree to which the server merges requests changes the effective latency per call. Under high concurrency, time to first token (TTFT) may differ significantly from single-machine tests;
    • Deployment method: The same model may have different throughput and pricing across different inference services. Selection should be based on measured data from the target service.

    Thus, “MoE is more suitable for agents” is only a starting point for selection. Before going live, you should measure TTFT, total time per task, and failure rate on your own tasks.

    Considerations for Context Caching

    Agents often repeatedly send system prompts, user preferences, and tool definitions, which can amount to tens of thousands of tokens. Context Caching allows the server to save these repeated prefixes; subsequent calls only bill the incremental content at the standard rate, while the cached portion is usually billed at a lower rate than the standard input price.

    The savings depend on the hit rate. The average input cost can be estimated using the following formula:

    Average Input Cost = Hit Rate × Cache Price + (1 − Hit Rate) × Standard Price + Cache Storage Fee

    Assuming the cache price is 20% of the standard price:

    • 80% hit rate: Input cost is roughly 36% of the standard, saving about 64%;
    • 50% hit rate: Input cost is roughly 60% of the standard, saving about 40%;
    • 20% hit rate: Input cost is roughly 84% of the standard, saving about 16%.

    When using caching, keep three points in mind:

    • Caching only covers input. Output token prices are usually higher, so input savings will be diluted by output costs. Overall savings must be recalculated based on the input-output ratio.
    • Caches have storage fees and TTLs. If not called for a long time, the cache expires, and the next call will be billed from scratch.
    • The prefix must be stable. If the system prompt contains current time, random IDs, or varying user data, the prefix changes every time, bringing the hit rate close to zero. Placing static content first and dynamic content last is the basic practice to improve hit rates.

    Architecture Diagram

    1. User Action
    2. │
    3. ▼
    4. Local Agent Gateway (Intercepts terminal/file operations, maintains task state)
    5. │
    6. ▼
    7. Cloud Model Service
    8. ├── Cached Prefix: System prompt + User preferences + Tool definitions (Billed at cache price)
    9. ├── Incremental Content: Current task context (Billed at standard price)
    10. └── Structured Output: Tool calling instructions (Validated via JSON Schema)

    How to Ensure Tool Calling Reliability

    Agents execute commands, modify files, and call APIs. The cost of a tool calling error is much higher than a mistake in a chat response. After selecting a model, you should implement the following engineering checks:

    1. Input and output validation: Use JSON…