Workflow:Microsoft Agent framework Basic Agent Creation
| Knowledge Sources | |
|---|---|
| Domains | AI_Agents, LLM_Ops, Python |
| Last Updated | 2026-02-11 17:00 GMT |
Overview
End-to-end process for creating a single AI agent with an LLM chat client, attaching function tools, and running queries with both streaming and non-streaming response modes.
Description
This workflow covers the foundational pattern for building an AI agent using the Microsoft Agent Framework Python SDK. The process starts by selecting and configuring a chat client (OpenAI, Azure OpenAI, Anthropic, Ollama, or others), defining the agent's instructions and persona, optionally attaching function tools that the agent can call, and finally running the agent against user queries. The agent handles the full request lifecycle: sending messages to the LLM, processing tool calls, and returning the final response. Both non-streaming (single response) and streaming (chunked real-time) execution modes are supported.
Usage
Execute this workflow when you need to build a single conversational AI agent backed by an LLM provider. This is the starting point for any agent-based application, whether it is a simple chatbot, a tool-augmented assistant, or a building block for a larger multi-agent system. You should have access to an LLM provider (OpenAI API key, Azure OpenAI endpoint, or a locally running Ollama instance) and optionally have Python functions you want the agent to call as tools.
Execution Steps
Step 1: Install the framework and configure credentials
Install the agent-framework package and set up authentication for your chosen LLM provider. This involves installing the core package (and optional provider-specific extras), then configuring API keys or Azure credentials via environment variables or explicit constructor parameters.
Key considerations:
- Use pip install agent-framework for the full bundle, or agent-framework-core for minimal install
- Provider packages (agent-framework-azure-ai, agent-framework-ollama) are installable individually
- Credentials can be set via environment variables or passed directly to the client constructor
- Azure providers support both API key and AzureCliCredential token-based authentication
Step 2: Select and instantiate a chat client
Choose the appropriate chat client class for your LLM provider and create an instance. Each provider has its own client class (OpenAIChatClient, OpenAIResponsesClient, AzureOpenAIChatClient, AzureOpenAIResponsesClient, OllamaChatClient, AnthropicChatClient). The client abstracts the provider-specific API and presents a unified interface to the Agent.
Key considerations:
- OpenAI clients use OPENAI_API_KEY and model ID environment variables
- Azure clients accept endpoint, deployment name, API version, and credential
- The Responses API clients support newer features like web search, file search, and hosted MCP tools
- The Chat Completion clients provide broader compatibility with legacy models
Step 3: Define function tools
Create Python functions decorated with the @tool decorator that the agent can invoke during execution. Each tool function should have type annotations (using Annotated with Field for descriptions) and a docstring that helps the LLM understand when and how to use it. Tools can be configured to require user approval or to execute automatically.
Key considerations:
- Use Annotated[type, Field(description="...")] for parameter documentation
- The docstring is sent to the LLM as the tool description
- Set approval_mode to "never_require" for automatic execution or "always_require" for human approval
- Tools can be attached at agent creation time or passed per-run for flexibility
Step 4: Create the agent
Instantiate an Agent by providing the chat client, system instructions, and the list of tools. The Agent class wraps the client and manages the conversation lifecycle including the tool-call loop. Alternatively, use the client convenience method .as_agent() which creates the Agent in a single call.
Key considerations:
- The instructions parameter sets the system prompt that defines agent behavior
- The name parameter provides an identity for logging and multi-agent scenarios
- Tools can be a single function or a list of functions
- Middleware can be attached to intercept and modify requests and responses
Step 5: Run the agent and process results
Invoke the agent with a user query using agent.run(). For non-streaming mode, the method returns an AgentResponse containing the full text response and any metadata. For streaming mode, pass stream=True to receive an async iterator of AgentResponseUpdate chunks that arrive as the LLM generates tokens.
Key considerations:
- Non-streaming returns a complete AgentResponse with .text property
- Streaming yields AgentResponseUpdate objects with incremental .text chunks
- Pass a thread parameter to maintain conversation state across multiple runs
- Run-level tools can be passed to agent.run() to supplement or override agent-level tools