Choosing a Primary LLM for AI Agents: Cost Control and Orchestration
Learn how to choose a primary LLM for everyday agent workloads, evaluate it on real tasks, track token costs, and define multi-agent workflows. The article also covers Kubernetes autoscaling, data storage, access controls, and monitoring, with a deployment reference for Baidu AI Cloud.
1. Why Separate Primary and Flagship Models?
Every model call in an AI agent workflow costs money and time. Flagship models offer strong reasoning capabilities, but their higher costs can quickly exhaust a budget if they handle every step. A primary model balances capability, latency, and price, making it suitable for most routine calls. Complex planning and difficult problems can then be routed to a flagship model.
Start model selection with three questions:
- What is the cost of an incorrect result?
- What is the maximum acceptable latency per call?
- How many calls will the system make each day?
Use a more capable model for low-volume steps where errors are costly. High-volume, highly structured steps are better candidates for a primary model.
2. Evaluate Coding and Complex Tasks Beyond Vendor Claims
Vendors often publish coding benchmark results, including SWE-bench-style evaluations based on issues in real code repositories. Before relying on these results, check four details: the test set version, metric definitions, sampling parameters, and the version of the previous-generation model used for comparison. Without this information, benchmark scores provide only a rough indication of performance.
A more reliable approach is to evaluate models on your team’s own work. Select 30–50 real tickets or code-fix tasks, and record the pass rate, time spent on manual corrections, and failure types. These results can directly answer whether a model can replace your current approach.
The same principle applies to automated machine learning experiments. Models can design experiments, tune hyperparameters, and summarize results, but the reliability of their conclusions depends on data quality and evaluation criteria. Automatically generated conclusions need human review before they inform decisions.
3. Track and Reduce Token Costs
Agent costs mainly come from three sources: input tokens, output tokens, and tool calls. Input tokens include system prompts, conversation history, and tool results. Output tokens include generated responses and tool-call arguments. If a task goes through ten rounds of tool use, repeatedly sending the accumulated history back to the model can quickly increase input token usage.
For cost accounting:
- Record input tokens, output tokens, model name, and task ID for every call.
- Aggregate costs by task rather than looking only at the daily bill.
- Compare the same set of tasks before and after an optimization, keeping task inputs unchanged.
Common cost-reduction techniques include shortening system prompts, removing duplicate tool descriptions, summarizing conversation history while retaining only information relevant to the current step, limiting tool-result length, and caching fixed subtasks.
Measure each change separately. Some optimizations reduce output quality and should be rolled back.
Vendors may claim substantial reductions in output tokens. Such figures are useful only when the task set, model versions, and measurement methodology are disclosed. Validate them in your own system before incorporating the savings into a budget.
4. Multi-Agent Orchestration: Roles, Protocols, and Failure Handling
Multi-agent systems commonly divide work among several roles: a planning agent decomposes the goal, an execution agent calls tools, and a review agent checks results. Clear role boundaries make problems easier to diagnose.
Define three aspects of the workflow explicitly:
- Message format: Use a fixed structure, such as JSON, for communication between agents. Include fields for task ID, step, input, output, and status. Free-form text is difficult to parse and validate automatically.
- Context boundaries: Give execution agents only the information needed for the current step, rather than the entire history.
- Failure handling: Set a maximum retry count for each step. Once that limit is reached, escalate to a human or a higher-level agent. Any repeated-call loop must have an explicit termination condition.
Create a checklist for every multi-agent workflow covering maximum rounds, the per-task token budget, timeouts, retry policies, and circuit-breaker conditions. Verify each item before deployment.
5. Infrastructure: A Baidu AI Cloud Deployment Reference
Inference workloads often fluctuate over time. In a Baidu AI Cloud deployment, container services can host inference services, while the Kubernetes Horizontal Pod Autoscaler (HPA) adjusts replica counts based on metrics.
The following example illustrates the configuration structure only. Its values must be adjusted using actual load-test results and are not production recommendations.
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata:name: agent-model-hpaspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: model-inferenceminReplicas: 2maxReplicas: 8metrics:- type: Podspods:metric:name: requests_per_secondtarget:type: AverageValueaverageValue: "20"
Your monitoring system must supply the metric used in this example, for instance by exposing it through Prometheus Adapter. Select GPU node types and minimum and maximum replica counts based on measured per-replica throughput and latency. Test autoscaling response times as well. Models with slow cold starts need a sufficient minimum replica count.
Data Storage and Access Controls
Store agent conversation histories, intermediate state, and operational logs separately. Object storage suits raw log archives, relational databases suit task state, and message queues carry events between agents.
Apply the principle of least privilege: grant each service only the read and write access it needs. Keep secrets in a secrets management service rather than in source code or configuration files.
Logging and Monitoring
For every model call, record at least:
- Task ID
- Agent role
- Model name
- Input and output token counts
- Call duration
- Tool name and return status
Configure alerts for tasks that exceed their token budget, repeated calls to the same tool across consecutive rounds, and individual calls that exceed a latency threshold. These alerts help teams intervene before loops or abnormal outputs generate further costs.
6. Putting the Approach into Practice
Start with one high-volume workflow whose results are easy to verify, such as code review or ticket classification. Run it with a primary model, measure cost and quality, and then decide whether to extend the approach to other steps.
Model capabilities and prices change, so revisit selection decisions regularly. Keep your evaluation set and rerun it whenever you change models. That provides a more reliable basis for decisions than figures from a single model release.
