Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Deploy, evaluate, and manage AI agents end-to-end on Microsoft Azure AI Foundry
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
foundry-agent/faos-optimize/references/python-patterns.md
1# Python FAOS (Foundry Agent Optimization Service) Optimization Patterns23These patterns are framework-neutral. Use them to expose Python agent behavior knobs to FAOS while preserving the app's current runtime.45## Base Contract67Use this when there is one clear instructions/model surface.89```python10import os1112from agent_optimization import load_config1314SYSTEM_PROMPT = """You are a helpful assistant."""1516config = load_config(17default_instructions=SYSTEM_PROMPT,18default_model=os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1"),19default_skills_dir="skills",20)21```2223Then map the resolved values into the existing framework:2425```python26instructions = config.compose_instructions()27model = config.model or os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1")28```2930Only apply temperature if the framework supports it:3132```python33options = {}34if config.temperature is not None:35options["temperature"] = config.temperature36```3738## Multi-Agent Named Targets3940When a Python app has multiple agents, use names that match the architecture rather than one generic `config`.4142```python43orchestrator_config = load_config(44default_instructions=ORCHESTRATOR_PROMPT,45default_model=os.getenv("ORCHESTRATOR_MODEL_DEPLOYMENT_NAME", os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1")),46default_skills_dir="skills/orchestrator",47)4849tool_agent_config = load_config(50default_instructions=TOOL_AGENT_PROMPT,51default_model=os.getenv("TOOL_AGENT_MODEL_DEPLOYMENT_NAME", os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1")),52default_skills_dir="skills/tool-agent",53)54```5556Use the evaluator objective to choose which named target to add first. For example, `intent_resolution` usually points to `orchestrator_config`, while `builtin.tool_call_accuracy` often points to `tool_agent_config`.5758## Microsoft Agent Framework5960Keep the current hosting adapter and agent construction. Replace only the selected knobs.6162```python63agent = Agent(64client=client,65instructions=config.compose_instructions(),66tools=existing_tools,67default_options=default_options,68)69```7071For model selection:7273```python74client = FoundryChatClient(75project_endpoint=project_endpoint,76model=config.model or os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1"),77credential=credential,78)79```8081If the model client is shared by multiple agents, flag this as a global side effect in the review summary.8283## FastAPI or Custom Responses Runtime8485Keep the existing HTTP contract. Use config values where the model call is created.8687```python88instructions = body.get("instructions", config.compose_instructions())89model = body.get("model", config.model or os.getenv("MODEL_DEPLOYMENT_NAME", "gpt-4.1"))90```9192When the app already supports request-level overrides, preserve them and use FAOS config as the default.9394## LangGraph or Workflow Runtimes9596Do not rewrite the graph. Identify node-level prompts and model clients.9798- Router/planner nodes are good targets for `intent_resolution`.99- Tool nodes are good targets for `builtin.tool_call_accuracy`.100- Final synthesis nodes are good targets for `relevance`, style, and task adherence.101102Prefer node-specific config names:103104```python105router_config = load_config(106default_instructions=ROUTER_PROMPT,107default_model=default_model,108)109```110111## Optional Skill Support112113`default_skills_dir="skills"` records the default skill location. It does not automatically make the runtime load files or expose skill tools.114115Add file-based skill support only when the target framework has a safe tool-calling or plugin mechanism. If adding it, use progressive disclosure:1161171. Startup prompt contains skill name and description only1182. Model calls a tool such as `load_skill` to load full skill instructions1193. Model calls a file-reading tool only for deep skill assets when needed120121Do not append every `SKILL.md` body into every agent prompt by default, especially in multi-agent architectures.122123## Dependency Guidance124125Add dependencies only when needed:126127```text128python-dotenv>=1.0.0129azure-identity>=1.19.0130```131132Use `python-dotenv` when local `.env` support exists. Use `azure-identity` when the local resolver uses Entra tokens.133134## Environment Variables135136Supported optimization variables:137138| Variable | Purpose |139|----------|---------|140| `AGENT_OPTIMIZATION_CONFIG` | Inline JSON config from the optimization service |141| `OPTIMIZATION_CONFIG` | Non-reserved inline JSON fallback |142| `AGENT_OPTIMIZATION_CANDIDATE_ID` | Candidate identifier to resolve from a service |143| `AGENT_OPTIMIZATION_RESOLVE_ENDPOINT` | Resolver API base URL |144| `AGENT_OPTIMIZATION_SKILLS_DIR` | Download location for candidate skill files |145146Do not add all of these to `agent.yaml` by default. Add only the variables required for the user's intended optimization path.147148## Verification Checklist149150- Changed Python files compile151- `from agent_optimization import load_config` succeeds from the agent root152- `load_config(default_instructions="x", default_model="m")` returns defaults when no optimization env vars are set153- Existing entrypoint, hosting adapter, and protocol remain unchanged154- Multi-agent targets are named and documented155- Evaluator objective influenced the target selection or was explicitly unavailable156- User is asked to review before deployment157