# How to Train an AI Agent Using the Microsoft Agent Framework

> Learn to train an AI agent with Microsoft Agent Framework. Configure system prompts, tools, and memory to guide a pre-deployed Azure LLM. Start building intelligent agents today.

- Repository: [Microsoft/ai-agents-for-beginners](https://github.com/microsoft/ai-agents-for-beginners)
- Tags: getting-started
- Published: 2026-04-22

---

**You don't train a model from scratch—you configure system prompts, tools, and memory to shape a pre-deployed Azure LLM's behavior.**

To train an AI agent using the Microsoft Agent Framework, you construct a specialized configuration layer on top of existing Azure AI models like **gpt-4o**. The framework provides `AzureAIProjectAgentProvider` for model connectivity, `AgentBuilder` for assembly, and iterative prompt refinement to achieve domain-specific behavior without modifying underlying weights.

## What "Training" Means in This Framework

Unlike traditional machine learning, the Microsoft Agent Framework treats **agent training as configuration engineering**. The LLM remains frozen; you train the agent by:

1. **Crafting system prompts** that encode role, personality, and constraints
2. **Registering tools** the agent can invoke for external capabilities
3. **Implementing memory** for state persistence across turns
4. **Iterating on examples** until outputs match specifications

This approach is implemented in `microsoft/ai-agents-for-beginners` across 14 progressive lessons, with runnable notebooks demonstrating each configuration layer.

## Step 1: Set Up the Azure AI Provider

The `AzureAIProjectAgentProvider` class in [`agent_framework/providers.py`](https://github.com/microsoft/ai-agents-for-beginners/blob/main/agent_framework/providers.py) handles authentication and model communication. It wraps Azure AI Foundry deployments, eliminating infrastructure management.

```python
from azure.identity import AzureCliCredential
from agent_framework.providers import AzureAIProjectAgentProvider
import os

# Load credentials from .env (see .env.example in repo root)

from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())

provider = AzureAIProjectAgentProvider(
    endpoint=os.getenv("AZURE_AI_PROJECT_ENDPOINT"),
    deployment_name=os.getenv("AZURE_AI_MODEL_DEPLOYMENT_NAME"),
    credential=AzureCliCredential()
)

```

**Key files for this step:**
- [`00-course-setup/README.md`](https://github.com/microsoft/ai-agents-for-beginners/blob/main/00-course-setup/README.md) — environment setup instructions
- `.env.example` — required variables template

## Step 2: Define the System Prompt

The system prompt is your primary training instrument. In `01-intro-to-ai-agents/code_samples/01-python-agent-framework.ipynb`, you define behavioral constraints that the LLM follows for every interaction.

```python
system_prompt = """
You are a travel-assistant whose job is to help users plan trips.
- Speak in a friendly, concise tone.
- Always cite the source of any factual information.
- When you are unsure, ask a clarifying question instead of guessing.
- Prioritize budget-friendly options when the user mentions cost concerns.
"""

```

This prompt is passed to `AgentBuilder.with_system_prompt()` and becomes the immutable context for all agent responses.

## Step 3: Register Tools for Extended Capabilities

Tools allow the agent to perform actions beyond text generation. The `04-tool-use/code_samples/04-python-agent-framework.ipynb` demonstrates how to declare tools with JSON schemas that the LLM uses for decision-making.

```python
from agent_framework.tools import Tool
import requests

def search_travel_knowledge(query: str) -> str:
    """Search internal travel knowledge base for up-to-date facts."""
    response = requests.get(
        f"https://mysearch.search.windows.net/indexes/travel/docs",
        params={"search": query, "api-version": "2021-04-30"}
    )
    return response.json()["value"][0]["content"]

search_tool = Tool(
    name="search_travel_knowledge",
    description="Search the internal travel knowledge base for up-to-date facts.",
    func=search_travel_knowledge,
    parameters={
        "type": "object",
        "properties": {
            "query": {
                "type": "string",
                "description": "The search query"
            }
        },
        "required": ["query"]
    }
)

```

The framework automatically handles tool invocation when the LLM generates a function call in its response.

## Step 4: Enable Memory for Stateful Interactions

For multi-turn conversations, `13-agent-memory/13-agent-memory.ipynb` shows how to persist context. The `SimpleMemory` class provides in-memory storage; production deployments use `AzureCosmosMemory` for durability.

```python
from agent_framework.memory import SimpleMemory

memory = SimpleMemory()  # Replace with AzureCosmosMemory for persistence

```

Memory contents are automatically injected into subsequent calls, allowing the agent to reference earlier parts of the conversation.

## Step 5: Build and Iterate on the Agent

The `05-agentic-rag/code_samples/05-python-agent-framework.ipynb` demonstrates assembling all components with `AgentBuilder` and the iterative refinement process that constitutes "training."

```python
from agent_framework.builder import AgentBuilder

# Assemble the agent

agent = (
    AgentBuilder()
    .with_provider(provider)
    .with_system_prompt(system_prompt)
    .with_tool(search_tool)      # optional

    .with_memory(memory)         # optional

    .build()
)

# Iterative training loop

def train_agent(examples: list[tuple[str, str]]):
    """
    examples: [(user_input, expected_response), ...]
    """
    for i, (user_input, expected) in enumerate(examples, 1):
        print(f"\n--- Example {i} ---")
        print(f"User: {user_input}")
        actual = agent.run(user_input)
        print(f"Agent: {actual}")
        # Compare with expected, adjust system_prompt or tool schemas

# Run training data

training_set = [
    ("Plan a 3-day itinerary for Paris in spring.", 
     "Sure, here's a concise itinerary..."),
    ("What's the current USD to EUR exchange rate?", 
     "The latest rate is... (source: ...)")
]

train_agent(training_set)

```

Refinement strategies:
- **Hallucination reduction**: Add explicit constraints to `system_prompt` ("Only answer using data from the search tool")
- **Tool invocation improvement**: Enhance `description` fields in tool schemas
- **Memory optimization**: Tune what gets stored in `SimpleMemory` vs. external databases

## Production Deployment

Once trained, deploy the agent with minimal code:

```python
while True:
    user_msg = input("\nYou: ")
    if user_msg.lower() in {"exit", "quit"}:
        break
    print("Agent:", agent.run(user_msg))

```

The underlying Azure model remains unchanged; your training investment is preserved in version-controlled prompts and tool configurations.

## Key Files in the Repository

| File | Purpose | Location |
|------|---------|----------|
| [`requirements.txt`](https://github.com/microsoft/ai-agents-for-beginners/blob/main/requirements.txt) | Dependency versions for `agent-framework` and Azure SDKs | Repository root |
| `.env.example` | Template for Azure endpoint and model deployment variables | Repository root |
| [`00-course-setup/README.md`](https://github.com/microsoft/ai-agents-for-beginners/blob/main/00-course-setup/README.md) | Environment configuration instructions | `00-course-setup/` |
| `01-python-agent-framework.ipynb` | Basic agent creation with system prompts | `01-intro-to-ai-agents/code_samples/` |
| `04-python-agent-framework.ipynb` | Tool registration and function calling | `04-tool-use/code_samples/` |
| `05-python-agent-framework.ipynb` | Full RAG agent with memory integration | `05-agentic-rag/code_samples/` |
| `13-agent-memory.ipynb` | Persistent memory implementations | `13-agent-memory/` |

## Summary

- **Training = Configuration**: The Microsoft Agent Framework trains agents through prompt engineering, tool registration, and memory setup—not model fine-tuning.
- **Core workflow**: Initialize `AzureAIProjectAgentProvider`, define `system_prompt`, optionally add `Tool` instances and `SimpleMemory`, then assemble with `AgentBuilder`.
- **Iterative refinement**: Run representative examples, compare outputs against expectations, and adjust prompts/tool schemas until behavior stabilizes.
- **Production ready**: Deploy the configured `Agent` with `agent.run()`; underlying Azure models require no modification.

## Frequently Asked Questions

### Do I need to fine-tune a language model to train an agent?

No. The Microsoft Agent Framework uses pre-deployed Azure AI models like **gpt-4o** without modification. You train the agent by refining system prompts, registering tools, and configuring memory—all of which shape behavior without touching model weights.

### How do I add external capabilities like web search or database access?

Register tools using the `Tool` class from `agent_framework.tools`. Each tool requires a `name`, `description`, JSON `parameters` schema, and Python `func` implementation. The framework automatically handles LLM function calling and execution. See `04-tool-use/code_samples/04-python-agent-framework.ipynb` for working examples.

### What is the difference between SimpleMemory and persistent storage?

`SimpleMemory` provides in-memory dictionary storage that persists only during the Python process lifetime. For production deployments, replace it with `AzureCosmosMemory` or custom implementations that write to Azure Cosmos DB, Blob Storage, or other durable stores. The `13-agent-memory/13-agent-memory.ipynb` notebook demonstrates both approaches.