Optimizing Foundation Models for AI Agent Execution: Practices and Evaluation

    This article explores how foundation models must be optimized for agentic execution scenarios, where success depends on more than single-turn accuracy. It covers the core capabilities required for long-horizon tasks, including planning and task decomposition, reliable tool calling, layered context and memory management, and structured output. It then outlines key training and alignment designs such as execution trajectory data, layered rewards, and negative or boundary samples, followed by a four-dimension evaluation framework and practical engineering guidance on isolation, logging, and rollback. Readers will learn a pragmatic path to building stable and reliable agent automation.

    1. Background: From Answering to Executing

    Over the past two years, foundation model evaluation has largely focused on question answering, summarization, translation, and code completion, with metrics centered on the accuracy and fluency of single-turn outputs. But once a model is placed inside an agent framework that can call tools, read and write files, operate a terminal, and execute multiple steps in sequence, the evaluation criteria change fundamentally. The model must not only produce the correct answer, but also keep the goal from drifting across dozens of interactions, self-correct when tool calls return errors, and still grasp the critical constraints as the context keeps growing.

    These execution-oriented scenarios typically have four characteristics. First, they are long-horizon: a single task may span dozens of tool calls. Second, they are stateful: the output of each step changes the environment state, and errors accumulate. Third, they are heterogeneous: the input mixes natural language instructions, code, logs, and structured data. Fourth, they are verifiable: the final result can often be judged objectively through compilation, testing, or data comparison. Together, these four points determine the requirements for foundation models, which differ significantly from those for conventional conversational models.

    2. Core Model Capabilities Required by Execution Scenarios

    2.1 Long-Horizon Planning and Task Decomposition

    The model needs to break a vague goal into executable subtasks and re-plan along the way based on feedback. Two common engineering approaches exist. One is explicit planning, which produces a step list first and then executes it step by step; its advantages are auditability and easy rollback. The other is implicit progression, which decides the next step while executing; its advantage is flexibility. In practice, a hybrid strategy is more robust: keep a plan draft that the model itself can read and write, and allow revisions at key checkpoints.

    2.2 Reliability of Tool Calling

    Tool call failures are often not because the model “does not know how to use” a tool, but because of parameter hallucination, format deviation, or misunderstanding of return values. Common ways to reduce these problems include: providing clear schema descriptions and error-code semantics for tools; structuring failure returns to clearly distinguish “parameter error,” “insufficient permissions,” “resource not found,” and “timeout”; and instructing the model in the prompt to fix parameters first rather than repeat the same call when a failure occurs.

    1. {
    2. "tool": "file_write",
    3. "arguments": { "path": "src/app.py", "mode": "overwrite" },
    4. "on_error": "inspect_message_then_retry_once"
    5. }

    2.3 Context Management and Layered Memory

    Long tasks inevitably hit the context window limit. A workable approach is layered memory: keep recent interactions verbatim, compress mid-term steps into structured summaries, and consolidate long-term constraints into immutable project rule files. When compressing, three types of information must not be dropped: hard constraints, confirmed facts, and the list of unresolved issues.

    2.4 Structured Output and Parseability

    The execution pipeline relies on machine parsing, so model output should follow an agreed format by default, with a fallback retry when parsing fails. Rather than simply asking the model to “output JSON,” a more robust approach is to provide a three-part package: a schema definition, examples, and a failure-handling strategy.

    3. Key Design Choices in Training and Alignment

    Optimizing models for execution scenarios usually adds two types of work on top of pre-training. The first is data construction: collect real execution trajectories, including both successful paths and repair paths after failures, so the model learns the complete loop of “error—diagnose—fix” rather than only imitating a perfect flow. The second is reward design: using only final success or failure as the signal leads to sparse samples, so a more practical approach is layered rewards that score progressively for valid format, effective tool calls, achieved subgoals, and task completion.

    In addition, negative samples and boundary samples need to be deliberately constructed, such as tool timeouts, restricted permissions, dependency conflicts, and self-contradictory requirements. Only after seeing enough forms of exceptions can the model reduce “stuck and repeating” behavior in real environments.

    4. Evaluation: How to Tell Whether Optimization Works

    Execution capability cannot be measured by a single question-answering accuracy metric. A four-dimension evaluation is recommended:

    • Task success rate: whether the end-to-end goal is achieved;
    • Step efficiency: the number of interaction rounds required and the proportion of redundant calls;
    • Recovery capability: whether the model can self-correct after a fault is injected;
    • Safety and compliance: whether it performs unauthorized operations or leaks sensitive information.

    The evaluation set should come from desensitized real business logs and retain verification scripts that can be judged automatically. Only when the verification signal is objective can model iteration have a reliable direction.

    5. Engineering Implementation Recommendations

    On the application side, it is recommended to make the model’s capability boundaries explicit: high-risk operations require human confirmation, write operations are executed in an isolated environment first, and all tool calls are logged for traceability. At the infrastructure level, you can rely on container platforms for sandbox isolation, use object storage to save execution trajectories, use message queues to smooth out traffic peaks, and use logging services with monitoring and alerting to build an observability loop. If you use the relevant general capabilities of Baidu AI Cloud, the same three principles of “isolation, traceability, and rollback” apply, without relying on any proprietary interfaces.

    6. Common Pitfalls

    The first is over-pursuing model autonomy while ignoring constraints and guardrails, leading to uncontrollable operations. The second is over-compressing the context and losing hard constraints. The third is a disconnect between the evaluation set and the real distribution, so metrics look good but the system is unstable in production. The fourth is treating prompt engineering as a long-term solution while ignoring the compounding effect of the training and data loop.

    7. Conclusion

    An execution-oriented agent is the product of three factors: model capability, engineering constraints, and evaluation methods. A weakness in any one of them limits overall performance. For developers, the pragmatic path is to first make tool semantics, error handling, and observability solid, then continuously iterate through trajectory data and layered rewards, and ultimately achieve stable and reliable automation gains in real business.