Microsoft Agent Framework โ Best Practices for Hosted Agents
Best practices when building hosted agents with Microsoft Agent Framework for deployment to Foundry Agent Service.
Official Resources
| Resource | URL |
|---|---|
| GitHub Repo | https://github.com/microsoft/agent-framework |
| MS Learn Overview | https://learn.microsoft.com/agent-framework/overview/agent-framework-overview |
| Quick Start | https://learn.microsoft.com/agent-framework/tutorials/quick-start |
| User Guide | https://learn.microsoft.com/agent-framework/user-guide/overview |
| Hosted Agents Concepts | https://learn.microsoft.com/azure/ai-foundry/agents/concepts/hosted-agents |
| Python Samples (MAF repo) | https://github.com/microsoft/agent-framework/tree/main/python/samples |
| .NET Samples (MAF repo) | https://github.com/microsoft/agent-framework/tree/main/dotnet/samples |
| PyPI | https://pypi.org/project/agent-framework/ |
| NuGet | https://www.nuget.org/profiles/MicrosoftAgentFramework/ |
Installation
Python: pip install agent-framework agent-framework-foundry-hosting (installs all sub-packages)
.NET: dotnet add package Microsoft.Agents.AI
Hosting Adapter
Hosted agents must expose an HTTP server using the hosting adapter. This enables local testing and Foundry deployment with the same code.
Python adapter packages: agent_framework_foundry_hosting
.NET adapter packages: Azure.AI.AgentServer.Core, Microsoft.Agents.AI.Foundry.Hosting
The adapter handles protocol translation between Foundry request/response formats and your framework's native data structures, including conversation management, message serialization, and streaming.
๐ก Tip: Make HTTP server mode the default entrypoint (no flags needed). This simplifies both local debugging and containerized deployment.
Key Patterns
Python: Credentials
For local development, use DefaultAzureCredential from azure.identity. In production, use ManagedIdentityCredential. See auth-best-practices.md.
Python: Environment Variables
Always use load_dotenv(override=False) so environment variables set by Foundry at runtime take precedence over local .env values.
Required .env variables:
FOUNDRY_PROJECT_ENDPOINTโ project endpoint URLFOUNDRY_MODEL_DEPLOYMENT_NAMEโ model deployment name
Authentication
If explicitly asked to use API key instead of managed identity, then use AzureOpenAIResponsesClient and pass in api_key parameter to it.
Agent Naming Rules
Agent 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.
Python: Virtual Environment
Always use a virtual environment. Never use bare python or pip โ use venv-activated versions or full paths (e.g., .venv/bin/pip).
Workflow Patterns
Agent Framework supports single-agent and multi-agent workflow patterns using graph-based orchestration:
- Single Agent โ Basic agent with tools, RAG, or MCP integration
- Multi-Agent Workflow โ Graph-based orchestration connecting multiple agents and deterministic functions
- Advanced Patterns โ Reflection, switch-case, fan-out/fan-in, loop, human-in-the-loop
For workflow samples and advanced patterns, search the Agent Framework GitHub repo.
Debugging
Use AI Toolkit for VS Code with the agentdev CLI tool for interactive debugging:
- Install
debugpyfor VS Code Python Debugger support - Install
agent-dev-cli(pre-release) for theagentdevcommand - Key debug tasks:
agentdev run <entrypoint>.py --port 8087starts the agent HTTP server,debugpy --listen 127.0.0.1:5679attaches the debugger, and theai-mlstudio.openTestToolVS Code command opens the Agent Inspector UI
For VS Code launch.json and tasks.json configuration templates, see AI Toolkit Agent Inspector โ Configure debugging manually.
Common Errors
| Error | Cause | Fix |
|---|---|---|
ModuleNotFoundError | Missing SDK | pip install agent-framework agent-framework-foundry-hosting in venv |
| Credential error | Wrong import | Use azure.identity.DefaultAzureCredential (local dev) or ManagedIdentityCredential (production) |
| Agent name validation error | Invalid characters | Use alphanumeric + hyphens, start/end alphanumeric, max 63 chars |
| Hosting adapter not found | Missing package | Install agent-framework-foundry-hosting |