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/create/references/agentframework.md
1# Microsoft Agent Framework โ Best Practices for Hosted Agents23Best practices when building hosted agents with Microsoft Agent Framework for deployment to Foundry Agent Service.45## Official Resources67| Resource | URL |8|----------|-----|9| **GitHub Repo** | https://github.com/microsoft/agent-framework |10| **MS Learn Overview** | https://learn.microsoft.com/agent-framework/overview/agent-framework-overview |11| **Quick Start** | https://learn.microsoft.com/agent-framework/tutorials/quick-start |12| **User Guide** | https://learn.microsoft.com/agent-framework/user-guide/overview |13| **Hosted Agents Concepts** | https://learn.microsoft.com/azure/ai-foundry/agents/concepts/hosted-agents |14| **Python Samples (MAF repo)** | https://github.com/microsoft/agent-framework/tree/main/python/samples |15| **.NET Samples (MAF repo)** | https://github.com/microsoft/agent-framework/tree/main/dotnet/samples |16| **PyPI** | https://pypi.org/project/agent-framework/ |17| **NuGet** | https://www.nuget.org/profiles/MicrosoftAgentFramework/ |1819## Installation2021**Python:** `pip install agent-framework agent-framework-foundry-hosting` (installs all sub-packages)2223**.NET:** `dotnet add package Microsoft.Agents.AI`2425## Hosting Adapter2627Hosted agents must expose an HTTP server using the hosting adapter. This enables local testing and Foundry deployment with the same code.2829**Python adapter packages:** `agent_framework_foundry_hosting`3031**.NET adapter packages:** `Azure.AI.AgentServer.Core`, `Microsoft.Agents.AI.Foundry.Hosting`3233The adapter handles protocol translation between Foundry request/response formats and your framework's native data structures, including conversation management, message serialization, and streaming.3435> ๐ก **Tip:** Make HTTP server mode the default entrypoint (no flags needed). This simplifies both local debugging and containerized deployment.3637## Key Patterns3839### Python: Credentials4041For **local development**, use `DefaultAzureCredential` from `azure.identity`. In production, use `ManagedIdentityCredential`. See [auth-best-practices.md](../../../references/auth-best-practices.md).4243### Python: Environment Variables4445Always use `load_dotenv(override=False)` so environment variables set by Foundry at runtime take precedence over local `.env` values.4647Required `.env` variables:48- `FOUNDRY_PROJECT_ENDPOINT` โ project endpoint URL49- `FOUNDRY_MODEL_DEPLOYMENT_NAME` โ model deployment name5051### Authentication5253If explicitly asked to use API key instead of managed identity, then use AzureOpenAIResponsesClient and pass in api_key parameter to it.5455### Agent Naming Rules5657Agent names must: start/end with alphanumeric characters, may contain hyphens in the middle, max 63 characters. Examples: `MyAgent`, `agent-1`. Invalid: `-agent`, `agent-`, `sample_agent`.5859### Python: Virtual Environment6061Always use a virtual environment. Never use bare `python` or `pip` โ use venv-activated versions or full paths (e.g., `.venv/bin/pip`).6263## Workflow Patterns6465Agent Framework supports single-agent and multi-agent workflow patterns using graph-based orchestration:6667- **Single Agent** โ Basic agent with tools, RAG, or MCP integration68- **Multi-Agent Workflow** โ Graph-based orchestration connecting multiple agents and deterministic functions69- **Advanced Patterns** โ Reflection, switch-case, fan-out/fan-in, loop, human-in-the-loop7071For workflow samples and advanced patterns, search the [Agent Framework GitHub repo](https://github.com/microsoft/agent-framework).7273## Debugging7475Use [AI Toolkit for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-windows-ai-studio.windows-ai-studio) with the `agentdev` CLI tool for interactive debugging:76771. Install `debugpy` for VS Code Python Debugger support782. Install `agent-dev-cli` (pre-release) for the `agentdev` command793. Key debug tasks: `agentdev run <entrypoint>.py --port 8087` starts the agent HTTP server, `debugpy --listen 127.0.0.1:5679` attaches the debugger, and the `ai-mlstudio.openTestTool` VS Code command opens the Agent Inspector UI8081For VS Code `launch.json` and `tasks.json` configuration templates, see [AI Toolkit Agent Inspector โ Configure debugging manually](https://github.com/microsoft/vscode-ai-toolkit/blob/main/doc/agent-test-tool.md#configure-debugging-manually).8283## Common Errors8485| Error | Cause | Fix |86|-------|-------|-----|87| `ModuleNotFoundError` | Missing SDK | `pip install agent-framework agent-framework-foundry-hosting` in venv |88| Credential error | Wrong import | Use `azure.identity.DefaultAzureCredential` (local dev) or `ManagedIdentityCredential` (production) |89| Agent name validation error | Invalid characters | Use alphanumeric + hyphens, start/end alphanumeric, max 63 chars |90| Hosting adapter not found | Missing package | Install `agent-framework-foundry-hosting` |91