Agent Optimizer is currently in preview. This preview is provided without a service-level agreement, and we don’t recommend it for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.
Prerequisites
- A Foundry project with a deployed hosted agent
- The
azure.ai.agentsCLI extension installed (see Quickstart: Optimize a hosted agent) - A model deployed for evaluation (for example,
gpt-4.1-mini) and an optimization model from the supported list (for example,gpt-5.1) - Your agent is optimizer-ready (calls
load_config())
Run an optimization
Start an optimization run with a single command:eval.yaml that references your dataset, evaluators, and options:
eval.yaml schema, see Configure the optimization run.
Target a specific agent
How the CLI resolves the agent depends on whether you run the command from anazd project:
The deployed agent name must match a hosted agent in the target Foundry project.
Run
azd ai agent invoke "test" to verify your agent responds before starting optimization.Optimize an existing agent without AZD project files
You can optimize an existing hosted agent without runningazd ai agent init and without creating azure.yaml or a .azure environment directory. In this standalone flow, provide the Foundry project endpoint and deployed agent name explicitly.
-
Ensure the deployed agent is optimizer-ready. In a local working directory, create the instruction file, dataset, evaluators, and
eval.yamldescribed in Configure the optimization run. Run the command from this working directory. Without anazdproject, relative paths ineval.yamlresolve from the current working directory. For this standalone flow, omitagent.config. The CLI asks for the baseline instruction when you run the command: -
Authenticate:
- Copy the project endpoint from the Foundry project’s Overview page. Use the project endpoint URL, not the Azure resource ID.
-
Save the endpoint in your user-level
azdconfig so subsequent commands can resolve the same project from any directory:This step writes the default endpoint to~/.azd/config.json. For the full resolution order and commands to inspect or clear the saved context, see Set the Foundry project context for azd commands. -
Run the optimization with the deployed agent name:
When prompted for the agent instruction, provide it inline or select a file such as
.agent_configs/baseline/instructions.md.
In the current preview, a standalone run doesn’t expand
agent.config from eval.yaml. Run the command interactively so you can provide the baseline instruction. Don’t use --no-prompt for this flow. Loading file-based skill and tool baselines also requires an azd project.--project-endpoint:
-
Save the operation ID from the command output. Because this flow has no
azdenvironment, the CLI doesn’t persist the last operation ID locally. Pass the operation ID to follow-up commands:These commands use the endpoint saved byazd ai project set. If you used the one-off--project-endpointform instead, pass the flag again to each follow-up command.
azd ai agent optimize apply requires an azd project because it writes candidate files under .agent_configs/ and updates the agent service in azure.yaml. If you don’t want to create AZD project files, review and deploy the winning candidate from the Foundry portal.Configure the optimization run
Configure optimization runs through aneval.yaml file that ties together your dataset, evaluators, and run options. The command azd ai agent eval generate writes this file for you, or you can create it by hand. The optimizer auto-detects eval.yaml in your project root, or you can pass it explicitly with --config eval.yaml.
Author the dataset and evaluators separately; see Create an evaluation dataset and evaluators. The following sections describe the run options.
Choose the eval and optimization models
The optimizer uses two models: an eval model that scores agent responses against criteria, and an optimization model that generates candidate configurations. Set them ineval.yaml or use CLI flags.
The
optimization_model field is required. If you don’t specify it and don’t pass --optimize-model, the optimization API returns an error. Always verify that both models are deployed in your project before you run optimization.Set the number of candidates
Themax_candidates option sets the expected number of candidate configurations for the run. The optimizer typically returns after it reaches that count, unless the run stops early because of an error or another stopping condition.
Higher values explore more variations but take longer. The optimizer learns from earlier candidates, so later candidates tend to score higher.
Times are approximate for a dataset of 3 to 10 tasks. Larger datasets or slower eval models increase run duration.
Evaluate multiple models
To compare model deployments in a single run, list them underoptimization_config.model_search_space. The optimizer evaluates your agent with each model against the same dataset and ranks the results by score and token cost.
model_search_space must be deployed in your Foundry project.
If the list includes your agent’s current model deployment, the optimizer automatically removes it from the candidates because the baseline already represents that model. If no models remain after this removal, you receive a validation error.
Monitor a running job
An optimization run is asynchronous. Use these commands when a job is long-running or you want to check its progress:status and cancel. The commands use the user-level endpoint saved by azd ai project set; otherwise, include --project-endpoint.
Interpret results
After optimization completes, review the results table. An asterisk (*) marks the best candidate. For the results table columns, scoring details, score-improvement thresholds, and the portal view, see Understand optimization results.
Deploy the winner
The recommended workflow is to apply the optimized config locally, then deploy:.agent_configs/<candidate_id>/ in your project. On next deploy, your agent uses the improved instructions and tool descriptions.
Alternatively, you can deploy directly via the API (useful for quick A/B testing):
What each target changes
The optimizer automatically activates the targets that apply to your baseline. This section is a reference for what a run changes. Use the following table to anticipate what optimization does for your agent:
Your code stays the same across all targets because
load_config() returns the optimized values automatically. Only the configuration the model sees changes.
Instructions
The optimizer rewrites the system prompt. Common improvements include:- Adding explicit constraints that the original prompt implied but didn’t state
- Restructuring instructions for clarity
- Adding output format specifications
- Strengthening safety and scope boundaries
You are a helpful assistant. might become:
Skills
The optimizer refines each skill’s description, body, and activation criteria while keeping the skill’s purpose intact. The agent loads improved skills throughload_config(), which appends them to the instruction set. Skills use the open Agent Skills format. For how your agent loads skills, see Make your agent optimizer-ready.
Tools
The optimizer refines yourtools.json definitions. Common improvements include:
- Clearer function descriptions that help the model know when to call a tool
- More specific parameter descriptions that reduce inaccurate arguments
- Added constraints (enums, required fields) that prevent invalid inputs