DEV Community

Cover image for AG-2 in Practice #8 – Deploying AG-2 Agents in Real-World Applications
Daniel Azevedo
Daniel Azevedo

Posted on

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}
Enter fullscreen mode Exit fullscreen mode

Then run with:

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
dartaryan profile image
Ben Akiva

This whole series has been such a great ride! clear explanations, thoughtful pacing, and just the right mix of depth and accessibility. I really hope the next lesson drops soon. can’t wait!