Use this file to discover all available pages before exploring further.
Agents are the core abstraction in the Microsoft Agent Framework. An agent wraps a chat client with instructions, tools, and middleware to create an autonomous assistant that can interact with users and perform tasks.
print("Agent: ", end="", flush=True)async for chunk in agent.run("Tell me a story", stream=True): if chunk.text: print(chunk.text, end="", flush=True)print()
Instructions define the agent’s behavior and personality:
agent = client.as_agent( name="WeatherAgent", instructions=""" You are a helpful weather assistant. - Always provide temperature in both Celsius and Fahrenheit - Include humidity and wind speed when available - Be concise and friendly """)
from agent_framework import toolfrom typing import Annotatedfrom pydantic import Field@tool(approval_mode="always_require")def get_weather( location: Annotated[str, Field(description="City name")]) -> str: """Get the weather for a location.""" return f"The weather in {location} is sunny."# Agent-level tools (available for all runs)agent = client.as_agent( name="Assistant", instructions="You are helpful.", tools=[get_weather, get_time] # Available for all queries)# Run-level tools (available only for this specific run)response = await agent.run( "What's the weather?", tools=[additional_tool] # Merged with agent-level tools)
from agent_framework import AgentSessionsession = AgentSession()# First interactionresponse1 = await agent.run( "My name is Alice", session=session)# Session remembers previous contextresponse2 = await agent.run( "What's my name?", session=session)print(response2.text) # "Your name is Alice."
import asyncio# Start the agent run in the backgroundtask = asyncio.create_task(agent.run("Complex task"))# Do other workawait do_other_work()# Wait for the agent to finishresponse = await task
writer = client.as_agent( name="Writer", instructions="You are a content writer.")reviewer = client.as_agent( name="Reviewer", instructions="You are a content reviewer. Provide feedback.")# Writer creates contentcontent = await writer.run("Write a tagline for an electric SUV")# Reviewer provides feedbackfeedback = await reviewer.run( f"Review this tagline: {content.text}")print(f"Content: {content.text}")print(f"Feedback: {feedback.text}")
from datetime import datetimefrom random import randint@tool(approval_mode="always_require")def get_weather(location: str) -> str: """Get the weather for a location.""" conditions = ["sunny", "cloudy", "rainy"] return f"The weather in {location} is {conditions[randint(0, 2)]}."@tool(approval_mode="always_require")def get_time() -> str: """Get the current time.""" return f"The current time is {datetime.now().strftime('%H:%M')}."agent = client.as_agent( name="Assistant", instructions="You are a helpful assistant.", tools=[get_weather, get_time])response = await agent.run( "What's the weather in Seattle and what time is it?")