Large Language Model API Selection and Agent Engineering: A Practical Guide
This guide explains how to select large language model APIs for AI agent systems and how to engineer those agents reliably. It covers building a reproducible evaluation baseline instead of relying on public leaderboards, treating tokens as a managed budget, fixing two common agent framework failures (skill recognition and missing confirmation on high-risk actions), and using tiered model routing behind a unified gateway rather than a single strongest model. Readers will find practical evaluation criteria, metrics, code samples, and policy patterns they can apply directly.
1. Why Agent Systems Turn Model Selection into Systems Engineering
When choosing a large language model in the past, the main criterion was single-turn question-answering quality. In agent scenarios, however, a single task typically consists of multiple rounds of reasoning, tool calls, and result validation, so the model is only one component of the system. The evaluation dimensions therefore expand from “how good is the answer” to: whether instruction following is stable, whether structured output is reliable, how well information is retained across long contexts, the token cost per task, and whether the model pauses to request confirmation before high-risk actions.
Two typical patterns of feedback have recently appeared in the industry. First, some ultra-large-parameter models fail to recognize local skills under an agent framework and execute high-risk actions without asking; the root cause is a mismatch between the framework’s skill descriptions and the model’s tool-selection strategy. Second, the same task consumes significantly more tokens than with other model tiers, quickly exhausting the quota. Neither problem can be explained by “model strength” alone; both must be located within the engineering pipeline.
2. Build a Reproducible Evaluation Baseline Instead of Reading Leaderboards
Leaderboards only reflect a specific distribution of test questions. It is better to build your own small evaluation set that covers real business scenarios, usually 50–200 items, stratified by task type:
- Single-turn understanding: summarization, extraction, classification;
- Tool calling: parameter extraction accuracy, number of rounds, number of error retries;
- Long context: cross-paragraph citation, choosing between conflicting information;
- High risk: whether confirmation is triggered, whether execution exceeds authorization.
During evaluation, fix the temperature, maximum output length, system prompt, and tool descriptions, and record four metrics: task success rate, average number of rounds, input/output token distribution, and end-to-end latency. Only by breaking the token distribution down into three dimensions—input, output, and cache hits—can you explain why two models have similar success rates but costs that differ by several times; the difference usually comes from longer chain-of-thought output and repeated retries.
Example evaluation record structure:
{"task_id": "agent-017","model_tier": "large","success": false,"rounds": 6,"tokens": { "input": 18240, "output": 4310, "cached": 0 },"latency_ms": 9120,"risk_action_confirmed": false}
3. Cost Governance: Manage Tokens as a Budget
The root cause of burning tokens is often not the unit price but “wasted rounds.” Four layers of actionable practice:
- Budget layer: set a token limit and a round limit for each task type; when exceeded, abort and fall back to a smaller model tier;
- Context layer: trim and summarize tool return results, keeping only the fields needed for decisions, and avoid feeding entire logs back in;
- Cache layer: place stable system prompts and tool descriptions in the prefix to reduce repeated billing through prefix caching;
- Observability layer: aggregate consumption by session, configure quota alerts, and avoid “one long task eating half a month’s quota.”
def run_with_budget(agent, task, max_tokens=30000, max_rounds=8):used, rounds = 0, 0while rounds < max_rounds:step = agent.step(task)used += step.input_tokens + step.output_tokensif step.done:return step.result, usedif used > max_tokens:return fallback_small_model(task), usedrounds += 1return fallback_small_model(task), used
4. Two High-Frequency Agent Framework Failures and Their Fixes
Skill recognition failures are mostly caused by descriptions that do not match the model’s capability boundaries: semantically overlapping skill names, overly loose parameter schemas, and missing examples. The fix is to reduce the number of skills, state clearly for each skill “when not to use it,” and provide positive and negative examples in the prompt.
Missing confirmation for high-risk actions is a security design flaw and cannot rely on the model’s own judgment. The correct approach is enforced interception at the execution layer: all tools with write permissions, delete permissions, or financial or external-publishing semantics go through a single human confirmation queue. The model only outputs the “proposed action + parameters + impact scope,” and the policy layer decides whether to allow it.
Model output -> Action parsing -> Risk classification (high/medium/low)High: enter confirmation queue, default to reject on timeoutMedium: secondary validation against parameter allowlistLow: execute directly and keep an audit log
5. Selection Conclusion: Tiered Routing Beats a Single Strongest Model
The stable approach in practice is “tiered routing + a unified gateway”: smaller model tiers handle classification, extraction, and formatting; larger tiers handle only complex planning and difficult reasoning; the gateway uniformly handles authentication, rate limiting, logging, and cost accounting. This avoids paying high token costs for simple tasks while preserving the capability ceiling for complex ones. If you use the relevant general capabilities of Baidu AI Cloud, you can combine model invocation, logging services, monitoring alerts, and object storage into a closed loop to track cost and quality per session.
Final recommendation: first use your own evaluation set to determine the capability boundaries of each model tier, then use budget and risk policies to hard-code those boundaries. Selection is not about picking one “strongest model,” but about finding the most suitable tier for each task type and keeping the framework safe and controllable even when the model makes mistakes.
