$npx -y skills add sickn33/agentic-awesome-skills --skill agent-framework-azure-ai-pyBuild persistent agents on Azure AI Foundry using the Microsoft Agent Framework Python SDK.
| 1 | # Agent Framework Azure Hosted Agents |
| 2 | |
| 3 | Build persistent agents on Azure AI Foundry using the Microsoft Agent Framework Python SDK. |
| 4 | |
| 5 | ## Architecture |
| 6 | |
| 7 | ``` |
| 8 | User Query → AzureAIAgentsProvider → Azure AI Agent Service (Persistent) |
| 9 | ↓ |
| 10 | Agent.run() / Agent.run_stream() |
| 11 | ↓ |
| 12 | Tools: Functions | Hosted (Code/Search/Web) | MCP |
| 13 | ↓ |
| 14 | AgentThread (conversation persistence) |
| 15 | ``` |
| 16 | |
| 17 | ## Installation |
| 18 | |
| 19 | ```bash |
| 20 | # Full framework (recommended) |
| 21 | pip install agent-framework --pre |
| 22 | |
| 23 | # Or Azure-specific package only |
| 24 | pip install agent-framework-azure-ai --pre |
| 25 | ``` |
| 26 | |
| 27 | ## Environment Variables |
| 28 | |
| 29 | ```bash |
| 30 | export AZURE_AI_PROJECT_ENDPOINT="https://<project>.services.ai.azure.com/api/projects/<project-id>" |
| 31 | export AZURE_AI_MODEL_DEPLOYMENT_NAME="gpt-4o-mini" |
| 32 | export BING_CONNECTION_ID="your-bing-connection-id" # For web search |
| 33 | ``` |
| 34 | |
| 35 | ## Authentication |
| 36 | |
| 37 | ```python |
| 38 | from azure.identity.aio import AzureCliCredential, DefaultAzureCredential |
| 39 | |
| 40 | # Development |
| 41 | credential = AzureCliCredential() |
| 42 | |
| 43 | # Production |
| 44 | credential = DefaultAzureCredential() |
| 45 | ``` |
| 46 | |
| 47 | ## Core Workflow |
| 48 | |
| 49 | ### Basic Agent |
| 50 | |
| 51 | ```python |
| 52 | import asyncio |
| 53 | from agent_framework.azure import AzureAIAgentsProvider |
| 54 | from azure.identity.aio import AzureCliCredential |
| 55 | |
| 56 | async def main(): |
| 57 | async with ( |
| 58 | AzureCliCredential() as credential, |
| 59 | AzureAIAgentsProvider(credential=credential) as provider, |
| 60 | ): |
| 61 | agent = await provider.create_agent( |
| 62 | name="MyAgent", |
| 63 | instructions="You are a helpful assistant.", |
| 64 | ) |
| 65 | |
| 66 | result = await agent.run("Hello!") |
| 67 | print(result.text) |
| 68 | |
| 69 | asyncio.run(main()) |
| 70 | ``` |
| 71 | |
| 72 | ### Agent with Function Tools |
| 73 | |
| 74 | ```python |
| 75 | from typing import Annotated |
| 76 | from pydantic import Field |
| 77 | from agent_framework.azure import AzureAIAgentsProvider |
| 78 | from azure.identity.aio import AzureCliCredential |
| 79 | |
| 80 | def get_weather( |
| 81 | location: Annotated[str, Field(description="City name to get weather for")], |
| 82 | ) -> str: |
| 83 | """Get the current weather for a location.""" |
| 84 | return f"Weather in {location}: 72°F, sunny" |
| 85 | |
| 86 | def get_current_time() -> str: |
| 87 | """Get the current UTC time.""" |
| 88 | from datetime import datetime, timezone |
| 89 | return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC") |
| 90 | |
| 91 | async def main(): |
| 92 | async with ( |
| 93 | AzureCliCredential() as credential, |
| 94 | AzureAIAgentsProvider(credential=credential) as provider, |
| 95 | ): |
| 96 | agent = await provider.create_agent( |
| 97 | name="WeatherAgent", |
| 98 | instructions="You help with weather and time queries.", |
| 99 | tools=[get_weather, get_current_time], # Pass functions directly |
| 100 | ) |
| 101 | |
| 102 | result = await agent.run("What's the weather in Seattle?") |
| 103 | print(result.text) |
| 104 | ``` |
| 105 | |
| 106 | ### Agent with Hosted Tools |
| 107 | |
| 108 | ```python |
| 109 | from agent_framework import ( |
| 110 | HostedCodeInterpreterTool, |
| 111 | HostedFileSearchTool, |
| 112 | HostedWebSearchTool, |
| 113 | ) |
| 114 | from agent_framework.azure import AzureAIAgentsProvider |
| 115 | from azure.identity.aio import AzureCliCredential |
| 116 | |
| 117 | async def main(): |
| 118 | async with ( |
| 119 | AzureCliCredential() as credential, |
| 120 | AzureAIAgentsProvider(credential=credential) as provider, |
| 121 | ): |
| 122 | agent = await provider.create_agent( |
| 123 | name="MultiToolAgent", |
| 124 | instructions="You can execute code, search files, and search the web.", |
| 125 | tools=[ |
| 126 | HostedCodeInterpreterTool(), |
| 127 | HostedWebSearchTool(name="Bing"), |
| 128 | ], |
| 129 | ) |
| 130 | |
| 131 | result = await agent.run("Calculate the factorial of 20 in Python") |
| 132 | print(result.text) |
| 133 | ``` |
| 134 | |
| 135 | ### Streaming Responses |
| 136 | |
| 137 | ```python |
| 138 | async def main(): |
| 139 | async with ( |
| 140 | AzureCliCredential() as credential, |
| 141 | AzureAIAgentsProvider(credential=credential) as provider, |
| 142 | ): |
| 143 | agent = await provider.create_agent( |
| 144 | name="StreamingAgent", |
| 145 | instructions="You are a helpful assistant.", |
| 146 | ) |
| 147 | |
| 148 | print("Agent: ", end="", flush=True) |
| 149 | async for chunk in agent.run_stream("Tell me a short story"): |
| 150 | if chunk.text: |
| 151 | print(chunk.text, end="", flush=True) |
| 152 | print() |
| 153 | ``` |
| 154 | |
| 155 | ### Conversation Threads |
| 156 | |
| 157 | ```python |
| 158 | from agent_framework.azure import AzureAIAgentsProvider |
| 159 | from azure.identity.aio import AzureCliCredential |
| 160 | |
| 161 | async def main(): |
| 162 | async with ( |
| 163 | AzureCliCredential() as credential, |
| 164 | AzureAIAgentsProvider(credential=credential) as provider, |
| 165 | ): |
| 166 | agent = await provider.create_agent( |
| 167 | name="ChatAgent", |
| 168 | instructions="You are a helpful assistant.", |
| 169 | tools=[get_weather], |
| 170 | ) |
| 171 | |
| 172 | # Create thread for conversation persistence |
| 173 | thread = agent.get_new_thread() |
| 174 | |
| 175 | # First turn |
| 176 | result1 = await agent.run("What' |