MAF AcademyLearnCapstoneSandbox
MAF Academy · Microsoft Agent Framework hands-on workshop · labs verified on agent-framework-core 1.26
← All modules
MODULE 2 OF 8 7 minutes

Building Your First AI Agent

Client + instructions + run(). Then make it yours with personas, options and streaming.

1.Understanding agent architecture and components
2.Setting up the development environment
3.Creating an agent using Microsoft Agent Framework
4.Configuring model providers and agent instructions
5.Executing an agent and processing user requests
6.Understanding agent execution and responses

On this page

What an agent is made ofSet up in 90 secondsPick a provider — keep the agentInstructions, options & the as_agent shortcutWhat happens inside agent.run()Key takeawaysHands-on activityQuiz
1Anatomy

What an agent is made of

An Agent is a thin, composable object. Only client is required — everything else is optional and additive.

Agent

client *

Model provider connection

instructions

Role, rules, format

tools

Functions & MCP servers

session

Conversation state

context_providers

Memory & knowledge

middleware

Guardrails, logging

client

Model provider connection

instructions

Role, rules, format

tools

Functions & MCP servers

session

Conversation state

context_providers

Memory & knowledge

middleware

Guardrails, logging

Agent(client=…, instructions=…, tools=[…], context_providers=[…], middleware=[…]) — only client is required.
2Environment

Set up in 90 seconds

bash
1python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
2pip install -r requirements.txt # agent-framework-core, -openai, -orchestrations, pandas …
3cp .env.example .env # then paste your GROQ_API_KEY
4python ex1_first_agent.py

Google Colab? !pip install agent-framework-core agent-framework-openai and use await main() directly — notebooks already run an event loop.

All labs share one helper, common.make_client(), so changing provider is a one-file change.

common.py
1import os
2from agent_framework.openai import OpenAIChatCompletionClient
3
4def make_client() -> OpenAIChatCompletionClient:
5 return OpenAIChatCompletionClient(
6 model=os.getenv("GROQ_MODEL", "llama-3.3-70b-versatile"),
7 api_key=os.environ["GROQ_API_KEY"],
8 base_url="https://api.groq.com/openai/v1",
9 )
3Model providers

Pick a provider — keep the agent

Every provider package exposes a chat client with the same interface. Groq, Ollama, LM Studio and vLLM speak the OpenAI Chat Completions protocol, so OpenAIChatCompletionClient + base_url covers them.

⚠️ In v1.x, OpenAIChatClient targets OpenAI's Responses API. For Groq use OpenAIChatCompletionClient.

Groq

This workshop · BYOK

OpenAIChatCompletionClient + base_url

Azure OpenAI

Enterprise

OpenAIChatCompletionClient(azure_endpoint=…)

Microsoft Foundry

Hosted agents

agent_framework.foundry

OpenAI

OpenAIChatClient (Responses)

Anthropic

agent_framework.anthropic

Ollama / local

Offline

agent_framework.ollama

Gemini

agent_framework.gemini

Amazon Bedrock

agent_framework.amazon

4Configuration

Instructions, options & the as_agent shortcut

Good instructions = role + rules + output format + what to do when unsure. Model options such as temperature and max_tokens go in default_options (per-agent) or options= on a single run().

ex2_configured_agent.py
1agent = client.as_agent(
2 name="OpsBot",
3 instructions="""You are OpsBot, a business operations analyst.
4- Be concise and use bullet points.
5- State assumptions explicitly. If unsure, say so.""",
6 default_options={"temperature": 0.2, "max_tokens": 400},
7)
5Execution

What happens inside agent.run()

run() returns an AgentResponse (.text, .messages, usage). Pass stream=True to get an async stream of AgentResponseUpdate chunks instead — essential for responsive UIs.

1

agent.run(msg)

Your request

2

before_run

Context providers add history & knowledge

3

middleware

Agent → chat middleware

4

chat client

Send to model provider

5

tool loop

Model calls tools ↺ until done

6

after_run

Store history / memories

7

AgentResponse

.text · .messages · usage

Lifecycle of a single agent.run() call
run vs stream
1result = await agent.run("Give me 3 KPIs for a support team.")
2print(result.text)
3
4async for update in agent.run("Explain the first KPI.", stream=True):
5 print(update.text, end="", flush=True)

Key takeaways

01

Agent(client=…, instructions=…) is all you need to start.

02

client.as_agent(...) is a convenient shortcut.

03

default_options sets temperature / max_tokens for every run.

04

run() → AgentResponse; run(stream=True) → async updates.

Hands-on activity

Create and execute a basic AI agent using Python

  1. 1In the Playground, change the agent name and write a persona for ‘OpsBot’.
  2. 2Drop temperature to 0.1, ask the same question twice — compare. Then try 1.2.
  3. 3Open Code Lab → Exercise 2 and complete the TODOs (as_agent, default_options, streaming).
  4. 4Ask the AI reviewer to check your code.
Open Code Lab

Reference solution — labs/ex2_configured_agent.py (verified on agent-framework-core 1.26)

ex2_configured_agent.py
1"""Exercise 2 — Configure instructions, model options and streaming (Module 2)."""
2
3import asyncio
4
5from common import make_client
6
7INSTRUCTIONS = """
8You are "OpsBot", a business operations analyst for a mid-size SaaS company.
9- Be concise and use bullet points.
10- Always state assumptions explicitly.
11- If you are not sure, say so instead of guessing.
12"""
13
14
15async def main() -> None:
16 client = make_client()
17
18 # Shortcut: every chat client can mint an agent with .as_agent(...)
19 agent = client.as_agent(
20 name="OpsBot",
21 instructions=INSTRUCTIONS,
22 default_options={"temperature": 0.2, "max_tokens": 400},
23 )
24
25 # Non-streaming: wait for the full answer
26 result = await agent.run("Give me 3 KPIs to track for a customer-support team.")
27 print(result.text)
28 print("-" * 60)
29
30 # Streaming: print tokens as they arrive
31 async for update in agent.run("Now explain the first KPI to a new manager.", stream=True):
32 if update.text:
33 print(update.text, end="", flush=True)
34 print()
35
36
37if __name__ == "__main__":
38 asyncio.run(main())

Check your understanding

0/3

Q1.Which client class should you use with Groq's OpenAI-compatible endpoint in MAF 1.x?

Q2.How do you stream an agent's answer?

Q3.Where do you set temperature for every call an agent makes?

M1 · IntroductionM3 · Tools