Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build and deploy AI applications on Azure AI Foundry using Microsoft's model catalog and AI services
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
foundry-agent/faos-optimize/faos-optimize.md
1# FAOS (Foundry Agent Optimization Service) Optimize Python Agent23Convert existing Python agent code into a FAOS optimization-ready version by wiring runtime configuration knobs to the FAOS config contract. This workflow prepares source code for optimization, asks the user to review the changes, and then routes to Foundry deployment only after explicit user approval.45## When to Use This Skill67USE FOR: make my Python agent FAOS optimizable, add FAOS_Config, add `load_config`, enable optimization config, make this agent optimization-ready, convert Python agent for FAOS optimization, wire evaluator-driven optimization knobs, expose prompt/model/temperature for FAOS.89DO NOT USE FOR: non-Python agents, deploying an agent directly, running batch evaluations, prompt optimization of an already deployed agent without source-code changes, or general Foundry deployment. For deployment, use [deploy](../deploy/deploy.md). For evaluator runs and prompt optimization loops, use [observe](../observe/observe.md).1011## Scope1213- Python only for now.14- Works across Python frameworks and runtimes when there are identifiable instructions/model/options surfaces.15- The FAOS config contract is framework-neutral. Framework-specific work is limited to finding the correct insertion points and preserving the existing runtime.16- Do not switch frameworks, hosting adapters, protocols, or entrypoints unless the user explicitly asks.17- Do not deploy automatically. Always stop for review first, then suggest Foundry deployment.1819## Quick Reference2021| Property | Value |22|----------|-------|23| Supported language | Python |24| Required pattern | `from agent_optimization import load_config` |25| Required knobs | instructions, model |26| Optional knobs | temperature, skills directory, learned skills, max tokens, tool/retrieval options when safe |27| Review gate | Mandatory before deploy |28| Next workflow | [deploy](../deploy/deploy.md) after user approval |2930## Workflow3132### Step 1: Resolve Target Agent Root3334Use the parent Microsoft Foundry project context resolution rules. If the user provides a path, use that path directly. Otherwise discover `.foundry/agent-metadata*.yaml` or agent source indicators in the workspace.3536After selecting an agent root, stay inside that root. Do not scan sibling agent folders unless the user explicitly switches target roots.3738### Step 2: Confirm Python Eligibility3940Detect Python using one or more of:4142- `requirements.txt`43- `pyproject.toml`44- `setup.py`45- `*.py` entrypoints4647If the target is not Python, stop and explain that FAOS source-code conversion is Python-only for now. If the target contains multiple languages, modify only the Python agent entrypoint unless the user approves a broader change.4849### Step 3: Resolve Evaluator Objective5051FAOS optimizes behavior against evaluator signals, so first identify what the code should become optimizable for.5253Inspect these sources, in order, when available:54551. User-stated evaluator objective, for example `tool_call_accuracy`, `intent_resolution`, or `relevance`562. Selected `.foundry/agent-metadata*.yaml` `evaluationSuites[]`, legacy `testSuites[]`, or legacy `testCases[]`573. `.foundry/evaluators/*.yaml`584. `.foundry/results/**` summaries or recent failure analysis files595. Existing code comments, README guidance, or test names describing target behavior6061If evaluator context is unknown, continue with a conservative base conversion and tell the user that evaluator-specific targeting may produce better FAOS results.6263### Step 4: Build Python Knob Inventory6465Scan the selected agent root for configurable behavior surfaces. Prefer semantic reads of source files over broad string replacement.6667Look for:6869- Instructions: `instructions=`, `system_prompt`, `SYSTEM_PROMPT`, `prompt=`, `system_message`, `developer_message`70- Model selection: `model=`, `deployment=`, `MODEL_DEPLOYMENT_NAME`, `AZURE_OPENAI_DEPLOYMENT`, framework-specific model fields71- Generation options: `temperature`, `top_p`, `max_tokens`, `response_format`, `tool_choice`, `parallel_tool_calls`72- Agent topology: `Agent(`, `agents=[...]`, `handoffs`, `supervisor`, `router`, `planner`, `executor`, `critic`, `synthesizer`, `WorkflowBuilder`, `StateGraph`73- Tool/retrieval surfaces: tool decorators, tool descriptions, argument schemas, retriever settings, index names, search limits74- Hosting entrypoint: FastAPI/Flask apps, `ResponsesHostServer`, uvicorn, custom response loops, LangGraph servers7576Create an internal inventory with file path, symbol/name, role, current default, and whether it is safe to expose through FAOS config.7778### Step 5: Classify Agent Topology7980Classify the architecture before editing:8182| Topology | Default FAOS targeting |83|----------|------------------------|84| Single agent | Wire config directly to the agent's instructions/model/options |85| Multi-agent with obvious orchestrator/supervisor | Target the orchestrator by default, unless evaluator context points elsewhere |86| Multi-agent with specialist tool agent | Target the specialist/tool path when evaluators focus on tool or task behavior |87| Multi-agent peer architecture with no orchestrator | Present a plan and ask before editing |88| Unknown Python runtime | Add only the minimal config loader and propose exact manual wiring points |8990Do not collapse multiple role-specific prompts into a single global `SYSTEM_PROMPT`. Preserve specialist prompts as defaults unless the user asks to optimize them together.9192### Step 6: Map Evaluators to Candidate Knobs9394Use evaluator context to select the smallest meaningful optimization scope.9596| Evaluator signal | Prefer these knobs first |97|------------------|--------------------------|98| `relevance` | final response instructions, answer synthesis prompt, model choice |99| `task_adherence` | primary task instructions, specialist instructions, response constraints |100| `intent_resolution` | router/orchestrator prompt, classifier prompt, planner prompt, handoff descriptions |101| `builtin.tool_call_accuracy` | tool-calling agent instructions, tool descriptions, argument schema descriptions, tool-choice/planner settings, low-temperature planning behavior |102| `indirect_attack` | safety instructions, instruction hierarchy, tool input handling, retrieved/tool-content treatment rules |103| groundedness/citation quality | retrieval instructions, answer synthesis prompt, citation formatting, retrieval parameters when exposed safely |104| latency/cost | model selection, max tokens, number of agent hops, tool/retrieval limits |105106If evaluators point to different subsystems, prefer a targeted set of named config hooks over one global config. Flag any knob whose change would affect all agents, such as a shared model client.107108### Step 7: Present Proposed FAOS Targets109110Before editing, summarize:111112- Selected agent root113- Python entrypoint(s)114- Detected topology115- Known evaluator objectives116- Proposed FAOS targets and why117- Knobs that will remain unchanged118- Files that will be modified or added119120If there is exactly one safe target, proceed unless the user asked for an approval checkpoint. If there are multiple plausible targets, ask the user which scope to optimize before editing.121122Example review summary:123124```text125Detected evaluator targets:126- builtin.tool_call_accuracy127- intent_resolution128129Detected topology:130- router_agent routes user requests131- weather_agent owns get_weather tool132- final_answer_agent synthesizes output133134Proposed FAOS targets:135- router_agent instructions: improves intent resolution136- weather_agent instructions/tool schema: improves tool-call accuracy137- preserve final_answer_agent for now138```139140### Step 8: Apply the Python FAOS Config Contract141142Use the generic Python contract from [Python Patterns](references/python-patterns.md). At minimum, add or reuse:143144```python145import os146147from agent_optimization import load_config148149SYSTEM_PROMPT = """...existing default instructions..."""150EXISTING_MODEL_FALLBACK = os.getenv("<existing-model-env-var>", "gpt-4.1")151152config = load_config(153default_instructions=SYSTEM_PROMPT,154default_model=EXISTING_MODEL_FALLBACK,155default_skills_dir="skills",156)157```158159Then map the selected target knobs:160161- Existing default instructions -> `config.compose_instructions()`162- Existing model default -> `config.model or <existing fallback>`. Reuse the app's current model-selection environment variable(s) and fallback chain instead of hard-coding `MODEL_DEPLOYMENT_NAME` unless that is already what the app uses.163- Existing temperature/default options -> `config.temperature` only when the runtime supports it164- Skills directory -> `config.skills_dir` only when the runtime has a skill/tool loading mechanism or one is explicitly added165166For multi-agent code, prefer named config variables such as `orchestrator_config`, `tool_agent_config`, or `synthesizer_config` over a misleading global `config` when more than one agent can be optimized.167168### Step 9: Add or Reuse `agent_optimization`169170If the agent already has an `agent_optimization` package, reuse it and avoid overwriting user changes.171172If missing, add a minimal local package with:173174- `load_config(...)`175- `OptimizationConfig`176- `Skill`177- environment-variable fallback support for `AGENT_OPTIMIZATION_CONFIG` and `OPTIMIZATION_CONFIG`178- optional candidate resolver support for `AGENT_OPTIMIZATION_CANDIDATE_ID` and `AGENT_OPTIMIZATION_RESOLVE_ENDPOINT`179180Do not assume a public PyPI package exists. Keep the local package self-contained unless the repository already uses a shared internal package.181182### Step 10: Update Dependencies and Runtime Config183184Update Python dependency files only as needed:185186- Add `python-dotenv` if the code imports it or already uses `.env` files187- Add `azure-identity` only if resolver token support is included or already imported188189Use `load_dotenv(override=False)` so Foundry runtime environment variables win over local `.env` values.190191Do not automatically add optimization env vars to `agent.yaml`. If the user wants env var placeholders, add only the required ones for their workflow and keep optional optimization vars documented rather than injected by default.192193### Step 11: Verify194195Run these checks where possible:1961971. Python syntax check for changed files1982. Import smoke test for `agent_optimization.load_config`1993. Default config smoke test with no optimization env vars2004. Pylance or workspace diagnostics for changed files2015. Existing project tests if they are cheap and relevant202203If Azure credentials or model endpoints are missing, do not treat live invocation failures as conversion failures. The required proof is that defaults load and the original runtime can still start or import as far as local configuration allows.204205### Step 12: Stop for Review, Then Suggest Deploy206207End the workflow with a review checkpoint. Summarize:208209- Changed files210- FAOS knobs exposed211- Evaluator objectives considered212- Any global side effects, such as shared model clients213- Verification results214215Ask the user to review the diff. Do not deploy automatically.216217When the user approves deployment, route to [deploy](../deploy/deploy.md), then [invoke](../invoke/invoke.md). If the user wants to evaluate the deployed version, route to [observe](../observe/observe.md).218219## Guardrails220221- Python only for now.222- The config contract is framework-neutral; insertion points are runtime-specific.223- Preserve existing frameworks, tools, hosting adapters, protocols, and entrypoints.224- Do not use one global config across all agents in a multi-agent system unless the existing architecture already uses one global prompt/model and the user approves.225- Do not wire temperature where unsupported or semantically risky.226- Prefer low-temperature planning/tool-calling defaults unless an evaluator objective suggests otherwise.227- Treat evaluator context as a targeting signal, not proof that every related knob should be changed.228- Keep all edits scoped to the selected agent root.229- Stop for review before deployment.230