How to Run AutoGen (AG2) with Ollama Locally in 30 Minutes
Why run AutoGen on your own machine
AutoGen, now maintained by the ag2ai community as AG2, is the framework where several LLM-powered agents talk to each other to finish a job: one writes code, another reviews it, a third checks the result. The catch with cloud models is that every round of that conversation costs tokens, and a long agent run can fire off hundreds of calls. Point AG2 at Ollama instead, and the same loop runs on a model sitting on your own disk. No API key, no per-token bill, and your code never leaves the machine.
That trade only makes sense if the local model can keep up. Small models stumble on long, fuzzy instructions, so the trick is to give each agent a narrow job and a short system message. A 7B model that would ramble through an open-ended chat can do solid work when its only task is "write this function" or "find the bug in this code." This guide sets up exactly that: a two-agent coding team running on a local 7B model, end to end, in about half an hour.
Source: Microsoft Research AutoGen paper, via AINOW — six multi-agent conversation patterns.
What you need
- Python 3.10 or newer (AG2 supports Python 3.10 through 3.13).
- Ollama installed and running. On Linux:
curl -fsSL https://ollama.com/install.sh | sh. On macOS and Windows, download it from ollama.com. - At least 8 GB of RAM for a 7B model; 16 GB makes the whole thing noticeably smoother.
- A terminal and a text editor. Nothing else.
Step 1: Pull a model that can call tools
AG2 talks to Ollama through a dedicated client, and native tool calling only works with models that support it. Qwen2.5 7B is a safe pick: it handles tools well, writes decent code, and fits in 8 GB of RAM.
ollama pull qwen2.5:7b
ollama list
If your main job is code, pull qwen2.5-coder:7b instead and use that name in the client below. Check the model actually responds before going further:
ollama run qwen2.5:7b "Write a one-line Python function that doubles a number."
Step 2: Install AG2
AutoGen went through a full rewrite as v0.4, and the project now lives as AG2 under the ag2ai community. The modern packages are autogen-agentchat for the agent API and autogen-ext for model clients. Install both, with the Ollama extra:
pip install autogen-agentchat "autogen-ext[ollama]"
The older v0.2-style install, pip install autogen[ollama], still exists in the classic docs, but new projects should start on the v0.4 API used below. If you are choosing between frameworks first, our LangGraph vs CrewAI vs AutoGen comparison breaks down where each one fits.
Step 3: Connect AG2 to Ollama
Save this as test_client.py and run it. It creates the Ollama model client and asks one question, which proves the wiring works before any agent logic gets involved.
import asyncio
from autogen_core.models import UserMessage
from autogen_ext.models.ollama import OllamaChatCompletionClient
async def main():
client = OllamaChatCompletionClient(model="qwen2.5:7b")
result = await client.create(
[UserMessage(content="What is the capital of France?", source="user")]
)
print(result.content)
await client.close()
asyncio.run(main())
Run it with python test_client.py. You should see "Paris" and a short answer. If it hangs, Ollama is probably not running: check with curl http://localhost:11434/api/tags and start the Ollama app or service.
Step 4: Build a two-agent coding team
Now the actual multi-agent part. A coder agent writes a function, a reviewer agent checks it, and a round-robin group chat passes messages back and forth until the run hits eight messages. Save this as team.py:
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.ui import Console
from autogen_ext.models.ollama import OllamaChatCompletionClient
model_client = OllamaChatCompletionClient(model="qwen2.5:7b")
coder = AssistantAgent(
name="coder",
model_client=model_client,
system_message="You write clean, working Python code. Return only the code and a one-line explanation.",
)
reviewer = AssistantAgent(
name="reviewer",
model_client=model_client,
system_message="You review Python code for bugs and edge cases. If the code is correct, reply with APPROVED. Otherwise list the problems.",
)
team = RoundRobinGroupChat(
participants=[coder, reviewer],
termination_condition=MaxMessageTermination(8),
)
async def main():
await Console(team.run_stream(
task="Write a Python function that returns the first 10 prime numbers."
))
await model_client.close()
asyncio.run(main())
Run it with python team.py. The console prints every message as it happens, so you can watch the coder propose code and the reviewer push back or approve it. The whole exchange runs locally: no request ever leaves your machine.
Step 5: Give an agent a tool
Agents become a lot more useful once they can act instead of just talking. A tool is an ordinary Python function with a docstring; AG2 reads the signature and hands it to the model. Add this to team.py and pass it to the coder:
def count_lines(path: str) -> int:
"""Count the lines in a text file."""
with open(path) as f:
return sum(1 for _ in f)
coder = AssistantAgent(
name="coder",
model_client=model_client,
tools=[count_lines],
system_message="You write clean, working Python code. Use the count_lines tool when you need to check a file's length.",
)
Two things to know about tools with local models. First, keep the tool list short: a 7B model picks the right tool more reliably from three options than from fifteen. Second, if the model keeps calling a tool in a loop, that is usually a sign the system message is vague, not that the model is broken. Tighten the instruction and the loop stops. Our guide to giving a local LLM tools with Ollama covers the same pattern in more depth.
Troubleshooting
- "Model not found" errors: the model name in
OllamaChatCompletionClienthas to matchollama listexactly, tag included.qwen2.5:7bandqwen2.5are different names to Ollama. - Tool calls fail or come back as plain text: the model does not support native tool calling. Switch to a model that does, such as Qwen2.5 or Llama 3.1, or set the client to manual tool calling, which works with any model but leans harder on the model's instruction following.
- Everything is slow: a 7B model on CPU answers at a few tokens per second, and a multi-agent loop multiplies that. A GPU, or a smaller 3B model for drafting, fixes most of the pain. If the team still feels sluggish, cut the message cap in
MaxMessageTerminationdown to 6. - Ollama connection refused: Ollama listens on port 11434 by default. If you run it on another machine, pass the address when you build the client, for example
OllamaChatCompletionClient(model="qwen2.5:7b", host="http://192.168.1.10:11434").
AG2 earns its keep when a task improves with back-and-forth: code, then review, then fix. For a fixed pipeline where each step has one job, a role-based crew is simpler to reason about; our CrewAI and Ollama tutorial builds that shape with the same local setup. And if you need strict control over state and branching, LangGraph fits better. The common thread is the same: the model runs locally, the framework decides how the agents talk, and you keep the API bill at zero.
Start with the two-agent team above, then add a third agent when you feel the limit: a tester that runs the code the coder writes, or a researcher that reads files before the coder starts. Each new agent is ten lines of setup and one system message. That is the whole pitch of local multi-agent work: the team grows one narrow job at a time, and the only cost is the electricity to run it.


Comments
Post a Comment