AG-2 in Practice #8 – Deploying AG-2 Agents in Real-World Applications
Welcome back!
This post is all about **deploying your AG-2 workflows in real-world applications** — not just testing locally, but actually integrating agents into products, services, and pipelines.
You’ll learn:
* Deployment strategies for AG-2 systems
* How to expose agents as APIs
* Scheduling agent runs (CRON-style)
* Tips for scaling and monitoring
Let’s go!
## 1. Why Deploy AG-2?
Once your agents are working reliably, you can:
* Offer them as **APIs** or **web services**
* Integrate them into **chatbots** , dashboards, or apps
* Automate backend tasks (reports, monitoring, classification, etc.)
* Add intelligent behavior to any product
## 2. Strategy A – Expose as a REST API
One common pattern is to **wrap AG-2 in a FastAPI or Flask server** :
from fastapi import FastAPI
from ag2 import Agent, Orchestrator, Conversation
app = FastAPI()
agent = Agent(name="assistant", llm="openai/gpt-4", system_message="Answer helpdesk questions.")
@app.post("/ask")
def ask(prompt: str):
conv = Conversation(agent)
response = conv.send(prompt)
return {"reply": response}
Then run with:
uvicorn main:app --reload
Your AG-2 agent is now available via `POST /ask`.
## 3. Strategy B – Scheduled Agent Jobs
For automation tasks (e.g. daily summaries, data pulls, file analysis), use Python scheduling:
import schedule
import time
def run_agent():
conv = Conversation(agent)
result = conv.send("Summarize today's GitHub issues.")
# Save, email, or store result
print(result)
schedule.every().day.at("09:00").do(run_agent)
while True:
schedule.run_pending()
time.sleep(60)
You can run this on a server, Docker container, or cloud function.
## 4. Strategy C – Cloud or Serverless Hosting
For bigger projects or production:
* **Dockerize** your AG-2 system
* Deploy to **Render** , **Fly.io** , **AWS Lambda** , **Google Cloud Run**
* Use **Redis/DBs** for persistent conversations
* Add monitoring via **Sentry** , **Prometheus** , etc.
Dockerfile example:
FROM python:3.11
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "main.py"]
## 5. Tips for Stability + Monitoring
* Use `Conversation(log_level="DEBUG")` during testing
* Store logs for later replay (`conv.save()` or custom log handler)
* Monitor LLM usage/costs with OpenAI's API dashboard
* Implement fallbacks for timeouts, rate limits, or tool failures
* Keep sensitive actions behind HITL approval
## What’s Next?
In Lesson #9, we’ll go beyond deployment and look at:
* **Advanced agent design** : memory, long-term goals, modular reasoning
* How to avoid prompt drift or hallucinations
* Multi-session agents that evolve over time
Keep coding