2.2 — Autogen: Building Conversational Agents
Autogen: Building Conversational Agents
While CrewAI is great for tasks, Microsoft Autogen is the king of conversational agents that can "Talk and Code" their way to a solution. In this lesson, we learn how to architect an Autogen swarm where agents iterate on each other's work autonomously.
🏗️ The Autogen Conversation Logic
- The User Proxy: Acts as the bridge between you and the swarm.
- The Assistant Agent: The primary worker (e.g., The Coder).
- The Critic Agent: Reviews the output and provides feedback loops until the goal is met.
Technical Snippet: A Basic Autogen Swarm
import autogen
config_list = [{"model": "gpt-4", "api_key": "..."}]
assistant = autogen.AssistantAgent("assistant", llm_config={"config_list": config_list})
user_proxy = autogen.UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding"})
user_proxy.initiate_chat(assistant, message="Write a Python script to scrape 10 leads from LinkedIn.")
Nuance: Code Execution Safety
Autogen agents can execute the code they write. This is powerful but dangerous. A professional architect always runs Autogen in a Docker Container to ensure the agents cannot accidentally delete files on the host machine.
Practice Lab: The Feedback Loop
- Setup: Create an Assistant and a Critic agent.
- Task: Ask them to "Write a viral headline."
- Loop: Watch the Critic reject the first 3 headlines and force the Assistant to improve the "Emotional Hook."
- Verify: Note how the final output is 10x better than the first attempt.
🇵🇰 Pakistan Application: The Upwork Proposal Bot
Build an Autogen system that writes Upwork proposals for Pakistani freelancers:
Agent Setup:
- User Proxy: Feeds the job description
- Assistant: Writes the initial proposal (mentioning PKR pricing, Karachi timezone, relevant skills)
- Critic: Reviews the proposal and checks for: (1) Grammar errors (2) Missing keywords from the job post (3) Price competitiveness
The Loop:
- Assistant writes proposal → Critic reviews → "Your opening line is too generic. Add a specific technical insight about the client's tech stack."
- Assistant rewrites → Critic approves → "This is ready to submit."
Why this matters for Pakistani freelancers: On Upwork, 90% of proposals are generic copy-paste. An Autogen swarm that iterates 3-4 times produces proposals that read like they were hand-crafted. Pakistani freelancers using this approach report 3x higher response rates.
📺 Recommended Videos & Resources
-
Microsoft AutoGen Documentation — Official guide to conversational agents and code execution
- Type: Documentation
- Link description: Visit microsoft.github.io/autogen for examples
-
AutoGen Code Execution & Agents — GitHub repo with working examples
- Type: Code Repository
- Link description: Search GitHub for "microsoft/autogen" examples folder
-
Building AutoGen Swarms — Real-world multi-agent code generation systems
- Type: YouTube
- Link description: Search YouTube for "AutoGen tutorial 2025" or "agent code execution"
-
Docker Safety for Agent Code Execution — How to sandbox agent-generated code safely
- Type: Documentation
- Link description: Visit docker.com docs for containerization
-
Feedback Loops in Autonomous Systems — How agents learn from test failures and iterate
- Type: YouTube
- Link description: Search YouTube for "agent feedback loops 2025"
🎯 Mini-Challenge
Build a Self-Healing Chatbot (5 minutes)
Your mission: Create a tiny AutoGen-like system with 2 agents:
- Assistant Agent — Writes a Python function to solve a problem (e.g., "Reverse a string")
- Critic Agent — Tests the function with 3 test cases and reports pass/fail
Code it as pseudo-Python or use two AI chat windows:
- Assistant writes code
- Critic runs the code, finds bugs
- If bug: Critic sends error message back to Assistant to fix
- If pass: Critic approves
Output: Print the final working code.
Hint: Pakistani developers love this because it removes manual testing overhead.
🖼️ Visual Reference
📊 AutoGen Conversational Loop
┌─────────────────────────────────────┐
│ USER PROXY AGENT (You) │
│ "Write code to scrape 10 restaurants"
└────────────┬────────────────────────┘
│
↓
┌────────────────┐
│ ASSISTANT │
│ AGENT │
│ (The Coder) │
└────┬───────────┘
│
│ "Here's my Python code..."
↓
┌────────────────┐
│ CRITIC AGENT │
│ (Test & Review)│
└────┬───────────┘
│
│ "Tests passed ✓" OR
│ "ERROR: NameError on line 5"
↓
┌────────────────┐
│ Loop Decision │
│ Pass? → Stop │
│ Fail? → Retry │
└────┬───────────┘
│
└──→ Back to ASSISTANT AGENT
(Max 5 iterations)
Homework: The Self-Correcting Coder
Design an Autogen system that: (1) Writes a Python function (2) Runs a test on it (3) If the test fails, the agent must read the error and fix the code autonomously. Use Gemini 2.5 Flash as the model (cheaper than GPT-4 for Pakistani developers).
Lesson Summary
Quiz: Autogen Conversational Agents
5 questions to test your understanding. Score 60% or higher to pass.