How to Build a Local AI Agent with Microsoft Agent Framework

The agent loop in Microsoft Agent Framework: the agent, the model, and tools pass messages until the task is done. Source: Microsoft Tech Community.

What Microsoft Agent Framework Is

Microsoft Agent Framework is the SDK Microsoft built to replace two of its earlier projects at once: AutoGen and Semantic Kernel. AutoGen's own README now points new users here. It ships for Python and .NET, and wraps any chat client (OpenAI, Anthropic, Azure, or a local Ollama server) behind one agent API, so swapping the client leaves the rest of your agent code untouched.

That swap is what makes it interesting for local setups. The framework ships a native Ollama client, so an agent that talks to a cloud model on Monday can talk to a model on your laptop on Tuesday. The Python packages reached version 1.26.0 in November 2026, and the Ollama integration is still in beta.

The shape will feel familiar: a model plus instructions plus tools, with the framework running the loop of calling the model, executing tools, and feeding results back until the task is done. If you are coming from AutoGen, our AutoGen (AG2) with Ollama tutorial covers the project it replaces.

Prerequisites

  • Python 3.11 or later
  • Ollama installed and running on your machine
  • A model that supports tool calling. Microsoft's docs suggest llama3.2 or qwen3:4b
  • About 30 minutes and roughly 3 GB of free disk space for the model

Tool calling is the requirement people trip over: plenty of small local models can chat but cannot reliably emit function calls. If your agent never touches its tools, the model is usually the problem, not your code, so start with qwen3:4b, which is small enough for a laptop and handles tools well.

ollama pull qwen3:4b
ollama list

Step 1: Install the Framework

Install the core package first, then the Ollama client. The Ollama package is still a prerelease, so it needs the --pre flag, or pip will report no matching version.

pip install agent-framework
pip install agent-framework-ollama --pre

Use a virtual environment: the meta-package pulls in around thirty dependencies.

Step 2: Point the Client at Ollama

The native client connects to http://localhost:11434 by default and reads the model name from OLLAMA_MODEL. Set it once and the model name stays out of your code. For a different host, set OLLAMA_HOST or pass host to the client constructor.

export OLLAMA_MODEL="qwen3:4b"

Step 3: Your First Local Agent

Create a file called agent.py. The pattern is three lines of setup and one call to run it:

import asyncio
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient

async def main():
    agent = Agent(
        client=OllamaChatClient(),
        name="HelpfulAssistant",
        instructions="You are a helpful assistant running locally via Ollama.",
    )

    result = await agent.run("What is the largest city in France?")
    print(result)

asyncio.run(main())

Run it with python agent.py: the client reads OLLAMA_MODEL from the environment and sends the prompt to your local server. Nothing leaves your machine: no API key, no account, no usage meter.

Step 4: Give the Agent a Tool

An agent without tools is a chatbot. Tools are plain Python functions: the framework reads the signature and docstring, describes them to the model, and calls them when the model asks. Here is one that reads files from a notes folder:

import asyncio
from pathlib import Path
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient

NOTES_DIR = Path.home() / "notes"

def read_note(filename: str) -> str:
    """Read a text file from the local notes folder."""
    path = (NOTES_DIR / filename).resolve()
    if not str(path).startswith(str(NOTES_DIR.resolve())):
        return "That file is outside the notes folder."
    if not path.exists():
        return f"No note named {filename} exists."
    return path.read_text()

async def main():
    agent = Agent(
        client=OllamaChatClient(),
        name="NotesAgent",
        instructions="You answer questions using the notes in the local folder. Use the read_note tool when you need a file.",
        tools=read_note,
    )
    result = await agent.run("What does my shopping list note say?")
    print(result)

asyncio.run(main())

The docstring matters: the model reads it to decide when to call the function, so write it as an instruction, not a label. The path check is deliberate: a tool that reads files should never follow a model-supplied path outside the folder you intended, so treat tool arguments as untrusted input.

Per Microsoft's docs, the Ollama client supports function tools and local MCP tools, but not hosted tools like code interpreters, file search, or web search. That is the trade for keeping your data local. To add memory between sessions, our Mem0 and Ollama memory guide builds on the same setup.

Step 5: Stream the Answer

A full response feels slow when a local model is thinking, so stream it: the loop is the same run call with stream=True, and the if chunk.text guard skips empty heartbeat chunks.

async def streaming_example():
    agent = Agent(
        client=OllamaChatClient(),
        instructions="You are a helpful assistant.",
    )

    print("Agent: ", end="", flush=True)
    async for chunk in agent.run("Tell me about Python.", stream=True):
        if chunk.text:
            print(chunk.text, end="", flush=True)
    print()

Step 6: Two Agents, One Pipeline

Single agents cover a lot of ground, but the framework's real strength is composition. The simplest pattern needs no extra packages: one agent researches, the next writes, and you pass the first agent's output into the second agent's prompt.

async def main():
    researcher = Agent(
        client=OllamaChatClient(),
        instructions="You collect facts. Answer with short bullet points and nothing else.",
    )
    writer = Agent(
        client=OllamaChatClient(),
        instructions="You turn bullet points into one clear paragraph.",
    )

    facts = await researcher.run("What are the three main causes of the seasons?")
    summary = await writer.run(f"Turn these facts into one paragraph:\n{facts}")
    print(summary)

asyncio.run(main())

For fancier shapes, ready-made orchestration builders (sequential, concurrent, group chat, handoff, and Magentic-style teams) ship in the separate agent-framework-orchestrations package, adding checkpointing, approval flows, and shared state. Start with the plain version and add the builders when a workflow needs them.

An Ollama-powered agent plans a task, picks a tool, runs it, and observes the result — all on local hardware. Source: sandeepmadavu.com.

Troubleshooting

The agent never calls its tools. Switch models first: try qwen3:4b or llama3.2, which Microsoft's docs name as tool-friendly. If a model cannot emit a valid function call, no framework setting will fix it.

pip cannot find agent-framework-ollama. You forgot --pre: the Ollama client is still in beta, so a plain install will not see it.

Connection refused on localhost:11434. Ollama is not running. Start it with ollama serve, then check curl http://localhost:11434/api/tags lists your pulled models.

An error about tool_choice or required mode. The native Ollama client only supports auto and none for tool_choice; required mode or a list of allowed tools raises an error, because Ollama cannot enforce it. Leave the default.

Replies are slow. A 4B model on CPU answers in seconds, not milliseconds. Use a smaller prompt, close other heavy apps, or move to a machine with a GPU. The framework adds almost no overhead; the model is the bottleneck.

When to Pick This Framework

Agent Framework is the pragmatic choice if you already live in the Microsoft ecosystem, want one codebase that switches between cloud and local models, or are migrating off AutoGen and want the officially supported path. Its agent API is small (an agent, a client, instructions, tools), and the orchestration builders cover the multi-agent shapes most projects need without forcing a graph DSL on you.

It is not the only option. LangGraph gives you finer control over state machines, CrewAI leans harder into role-based crews, and our Google ADK with Ollama guide covers Google's equivalent for the same local setup. For a local agent with a few tools, any of them will do; the model you pick matters more than the framework. What Agent Framework offers is the shortest path from zero to a working local agent Microsoft will keep maintaining, and with Ollama underneath it costs nothing to run.

Comments

Popular posts from this blog

Open-Source AI Weekly: Bonsai, GigaChat 4 Lead

Claude Claims Proof of Riemann Hypothesis After 167 Years

How to Build an AI Agent with LangGraph in 30 Minutes