Skip to main content
Agents are the primary abstraction in Microsoft Agent Framework for building AI-powered conversational experiences. An agent combines a language model, optional tools (functions), instructions, and configuration into a reusable component that can process user messages and generate responses.

What is an Agent?

An agent encapsulates:
  • A language model client (e.g., Azure OpenAI, Anthropic, Ollama)
  • Instructions that define the agent’s behavior and personality
  • Tools (functions) the agent can call to access external data or perform actions
  • Middleware for intercepting and modifying requests/responses
  • Session management for multi-turn conversations
In Python, agents implement the SupportsAgentRun protocol and the framework provides:
  • Agent - Main agent class with middleware and telemetry
  • RawAgent - Core agent without built-in layers
  • BaseAgent - Minimal base class for custom implementations
The Agent class is the recommended starting point for most use cases.

Creating Your First Agent

Agent Lifecycle

Non-Streaming Mode

The run() method returns an AgentResponse containing:
  • messages - List of response messages
  • text - Concatenated text from all messages
  • response_id - Unique identifier for this response
  • usage_details - Token usage and cost information
  • value - Structured output (if using response format)

Streaming Mode

Streaming returns a ResponseStream that yields AgentResponseUpdate objects and provides get_final_response() to retrieve the complete response.

Agent Configuration

Instructions

Instructions define the agent’s behavior, personality, and capabilities:

Model Parameters

Available options:
  • temperature (0.0-2.0) - Controls randomness
  • max_tokens - Maximum response length
  • top_p - Nucleus sampling parameter
  • frequency_penalty - Reduces repetition
  • presence_penalty - Encourages topic diversity
  • seed - Reproducible outputs
  • stop - Stop sequences

Agent Protocol

The SupportsAgentRun protocol defines the agent interface:
This protocol uses structural subtyping (duck typing), so classes don’t need to explicitly inherit from it.

Agent Properties

Converting Agents to Tools

Agents can be converted to tools for use by other agents:

Best Practices

Agent Design Tips
  1. Clear Instructions: Be specific about the agent’s role, tone, and constraints
  2. Tool Selection: Only provide tools the agent actually needs
  3. Session Management: Reuse sessions for multi-turn conversations
  4. Error Handling: Always handle potential exceptions in production
  5. Observability: Enable telemetry to monitor agent behavior
  6. Cost Control: Set max_tokens to prevent runaway costs
Production Considerations
  • Always use approval_mode="always_require" for tools in production
  • Implement proper authentication and authorization
  • Monitor token usage and costs
  • Add rate limiting and timeout handling
  • Test agent behavior with edge cases
  • Use middleware for security checks

Next Steps

Tools

Learn how to give agents function-calling capabilities

Sessions

Manage multi-turn conversations with sessions

Middleware

Intercept and modify agent behavior with middleware

Observability

Monitor agent performance with OpenTelemetry