Microsoft Agent Framework Tutorial 2025 - Build AI Agents with Python from Scratch | Complete Course
Introduction: The New Era of Agentic AI
What is the Microsoft Agent Framework?
AutoGen was great for research and multi-agent conversations but hard to control in production.Semantic Kernel was excellent for enterprise integration and plugins but lacked native multi-agent orchestration.
Key Features 2025
Unified Python SDK: No need to juggle multiple libraries.Graph-Based Workflows: Define deterministic paths for your agents to follow.Interoperability: Native support for theModel Context Protocol (MCP) , allowing agents to connect to any tool or data source easily.Azure AI Foundry Integration: Seamlessly deploy your local agents to the cloud for scaling and observability.
Prerequisites
Python 3.10 or later : The framework relies on modern async features.Visual Studio Code : Recommended for its new "Agentic AI" extension support.API Keys : An OpenAI API key or an Azure OpenAI endpoint.Azure Account (Optional) : For deploying to the Azure AI Agent Service.
Step 1: Setting Up Your Environment
# Create a virtual environment
python -m venv agent-env
# Activate the environment
# Windows:
agent-env\Scripts\activate
# Mac/Linux:
source agent-env/bin/activate
pip install azure-ai-agents python-dotenv
Step 2: Building Your First Single Agent
import os
import asyncio
from azure.ai.agents import AgentClient, Agent
from azure.identity import DefaultAzureCredential
# Load environment variables
project_connection_string = os.getenv("AZURE_AI_PROJECT_CONNECTION_STRING")
async def main():
# Initialize the client
# For local development with OpenAI, you might use a different configuration class
# tailored to the open-source SDK, but here is the Azure-ready pattern:
async with AgentClient(project_connection_string, credential=DefaultAzureCredential()) as client:
# Create an agent
agent = await client.agents.create_agent(
model="gpt-4-turbo",
name="research-assistant",
instructions="You are a helpful AI assistant focused on summarizing technical articles."
)
print(f"Agent Created: {agent.id}")
# Create a thread for conversation
thread = await client.agents.create_thread()
# Send a user message
await client.agents.create_message(
thread_id=thread.id,
role="user",
content="Explain the difference between AutoGen and Microsoft Agent Framework."
)
# Run the agent
run = await client.agents.create_run(thread_id=thread.id, agent_id=agent.id)
# Poll for completion (simplified for brevity)
while run.status in ["queued", "in_progress"]:
await asyncio.sleep(1)
run = await client.agents.get_run(thread_id=thread.id, run_id=run.id)
# Retrieve and print messages
messages = await client.agents.list_messages(thread_id=thread.id)
for msg in messages.data:
if msg.role == "assistant":
print(f"Agent: {msg.content[0].text.value}")
if __name__ == "__main__":
asyncio.run(main())
Step 3: Orchestrating a Multi-Agent Workflow
Agent A (Planner) breaks down a task.Agent B (Coder) writes the Python script.Agent C (Reviewer) checks the code for bugs.
# Conceptual example of a Workflow definition
from microsoft_agent_framework import Workflow, AgentNode
# Define your agents
planner = AgentNode(name="Planner", instructions="Break down tasks.")
coder = AgentNode(name="Coder", instructions="Write Python code.")
reviewer = AgentNode(name="Reviewer", instructions="Review code for security.")
# Define the graph
workflow = Workflow()
workflow.add_edge(planner, coder) # Planner passes output to Coder
workflow.add_edge(coder, reviewer) # Coder passes output to Reviewer
workflow.add_edge(reviewer, end) # If approved, end. If not, loop back to Coder.
# Execute
result = await workflow.run("Build a snake game in Python.")
Deployment: Azure AI Agent Service
Security: Managed identity and data privacy.Scalability: Serverless execution of your agent threads.Observability: Built-in integration with OpenTelemetry. You can see exactly which agent took too long or spent too much money.
FAQ: AutoGen vs. Microsoft Agent Framework
Conclusion
Clone the Official Samples Repository .Sign up for a free Azure AI Foundry account. Build your first multi-agent workflow today!
0 Response to "Microsoft Agent Framework Tutorial 2025 - Build AI Agents with Python from Scratch | Complete Course"
Post a Comment