LLM Model Selection for AI Agents: Latency, Context, and Cost
Selecting a large language model for agent workloads requires more than comparing benchmark scores. Because agents invoke models dozens of times per task with long inputs and short outputs, latency, context utilization, and per-call cost become decisive factors. This article explains three key metrics and how to measure them, examines how Mixture-of-Experts architectures affect agent inference, provides a prompt structure that maximizes prefix cache hits, presents a formula for estimating per-task cost, and outlines a five-step process for production model selection.
1. Agent Workloads Impose New Requirements on Models
Traditional Q&A products follow a single request-response pattern. An agent, by contrast, calls the model repeatedly within a single task: it reads context, decides the next action, invokes external tools, and feeds tool results back into the model. A complete task may involve dozens of model calls, most of which have long inputs and short outputs.
This workload profile creates three direct pressures:
- Response latency: Every step waits on the model. Time To First Token (TTFT) compounds across a multi-step chain and noticeably shapes the user’s experience.
- Context size: System instructions, action history, user preferences, and tool descriptions accumulate over time. Both the context window length and the model’s ability to use long context affect task success rates.
- Call cost: With many calls per task, small differences in per-call pricing amplify into significant changes in the overall budget.
Model selection therefore cannot rely on leaderboard scores alone; it requires engineering evaluation against these three pressures.
2. Three Key Metrics and How to Measure Them
2.1 Time To First Token and Throughput
TTFT is the time from sending a request to receiving the first output token; it determines whether the user perceives an “instant” response. Throughput (tokens per second) is the generation speed and determines how long long-output tasks take.
For tool-decision requests with short outputs, TTFT is the primary metric. For long-form generation, throughput matters more.
Measurements should simulate production concurrency and network conditions, and report P50 and P95 percentiles. Looking only at single-request averages tends to hide tail latency problems.
2.2 Context Window and Information Utilization
Window length only indicates how much input a model can accept; it does not guarantee the model can reliably use every part of that input. Most models show reduced recall accuracy for content located in the middle of a long context.
Build an internal evaluation set: embed key facts at different positions within long texts and measure the model’s extraction accuracy at each position. The results are more informative than the window parameter itself.
2.3 Cache Hits and Unit Cost
Prefix caching (Context Caching) is a feature offered by many inference services. When multiple requests share the same prefix, the already-computed portion can be reused and is usually billed at a lower rate than uncached input.
An agent’s system instructions and tool definitions remain essentially unchanged across calls within a task, so cache benefits are high. Whether you actually benefit depends on prompt structure and the provider’s billing rules.
3. Mixture of Experts and Agents
Mixture of Experts (MoE) models contain a large number of parameters, but only a subset of expert subnetworks is activated per token. Inference compute is determined mainly by the activated parameters rather than the total parameter count, allowing the model to maintain large capacity while controlling per-call compute cost.
Two caveats apply when using such models:
- All expert parameters must still be loaded into memory or VRAM. For self-deployment, the hardware bar is higher than the activated parameter count suggests.
- The routing mechanism can cause uneven load, so real-world latency must be measured; it cannot be inferred from parameter scale alone.
4. Prompt Structure for Higher Cache Hit Rates
Caching is based on prefix matching, so how you organize the prompt directly determines the hit rate. The principle: stable content goes first, dynamic content goes last.
prompt = [system_instructions, # fixed: role, rules, safety constraintstool_definitions, # fixed: tool descriptions in a consistent orderuser_profile_summary, # low-frequency changes: preferences and long-term memory summarytask_history[-N:], # dynamic: the last N actionscurrent_observation # dynamic: input for the current turn]
Practical considerations:
- Serialize the tool list in a fixed order. Changes in field order break the prefix and invalidate the cache.
- Keep fields that change every request—timestamps, request IDs—out of the prefix.
- Inject long-term memory as a compressed summary rather than concatenating the full history every turn.
5. A Formula for Estimating Per-Task Cost
The cost of a single task can be estimated by summing across calls:
task_cost = Σ_calls [uncached_input_tokens × input_price+ cached_input_tokens × cached_price+ output_tokens × output_price]
The number of cached tokens should come from actual request logs, not idealized assumptions. The ratio between cached_price and input_price follows the provider’s published billing rules.
For example, suppose a task averages 20 calls, each with 8,000 input tokens of which 6,000 hit the cache, and 500 output tokens. Plugging these into the formula yields the per-task cost; multiplying by daily task volume gives a monthly budget estimate. This example only illustrates the method—replace the numbers with measured data.
6. How to Treat Promotions and Free Tiers
Free tiers reduce validation costs early on, but they are short-term conditions and should not drive long-term selection decisions. Distinguish three types of information:
- Stable long-term pricing structure, including the ratios among input, output, and cached-input unit prices.
- Short-term promotions, including time-limited credits and discounts.
- Service quality, including rate-limiting policies, availability, data retention, and terms of use.
A provider’s commercial motivations are hard for a technical team to verify independently, so base decisions on measurable metrics. If your business depends on a promotional price, design a migration plan in advance so that switching models does not require rewriting core business logic.
7. Five Steps for Production Model Selection
- Define the tasks: Map out the agent’s typical execution chains, annotating input length, output length, and latency requirements for each step.
- Build an evaluation set: Collect real task samples covering both normal flows and exception branches.
- Measure the metrics: Record task accuracy, TTFT, P95 latency, throughput, and per-task cost.
- Roll out gradually: Start with a small share of traffic and watch error rates and user feedback.
- Monitor continuously: Set alerts for cost, latency, and failure rates, and re-test periodically, since model versions and prices change over time.
The basic structure of the evaluation loop:
for model in candidate_models:results = []for case in eval_set:start = now()output = call(model, case.prompt)latency = now() - startresults.append({"correct": grade(output, case.expected),"latency": latency,"cost": estimate_cost(output.usage)})report(model, aggregate(results))
Model selection is part of ongoing operations and must be re-validated as the business and supply conditions evolve.
