Building AI Agentic Systems with AG2 and FastAPI
AG2 is a multi-agent conversation framework facilitating cooperation among multiple agents. It enables agents to collaborate to solve…
Building AI Agentic Systems with AG2 and FastAPI

AG2 is a multi-agent conversation framework facilitating cooperation among multiple agents. It enables agents to collaborate to solve complex tasks. Also, AG2 smooths the way to integrate human oversight and input into agent workflows allowing more interactive and context-aware human handoff.
Today, we will explore how AI agentic systems are gaining traction, enabling intelligent agents to work together seamlessly. This article will explore how to build such a system using AG2 (AutoGen 2) and FastAPI.
To follow along, ensure you have the following:
- Python 3.8+
- FastAPI
- AutoGen (AG2)
- Uvicorn (for running FastAPI)
- Postman (optional, for API testing)
You can install dependencies with:
!pip install fastapi uvicorn autogen python-dotenv
Setting Up Agentic Interaction
To simplify the interaction, we chose two AI agents that alternate presenting news items in a set of predefined news categories.
Each agent is an instance ofConversableAgent, enables structured conversations.
The below logic was used in defining agent roles:
- Alternating News Delivery: Ensures that Joe and Cathy take turns presenting news.
- Predefined News Order
- Session End Condition: The conversation will end after the Weather category.
- No human intervention is needed (
human_input_mode="NEVER").
# process.py
import os
from autogen import ConversableAgent
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
llm_config = {"model": "gpt-4o-mini", "api_key": OPENAI_API_KEY}
cathy = ConversableAgent(
name="cathy",
system_message="Your name is Cathy, and you are a news presenter on the nighttime BBC News.",
llm_config=llm_config,
human_input_mode="NEVER",
)
joe = ConversableAgent(
name="joe",
system_message="Your name is Joe, and you are also a news presenter on the nighttime BBC News."
"You and Cathy alternate presenting news items, ensuring no two consecutive items are presented by you."
"The news categories should be presented in this order: Local, Foreign, Entertainment, Sports, and Weather."
"Conclude after the Weather category.",
llm_config=llm_config,
human_input_mode="NEVER",
)
Creating a FastAPI Endpoint

FastAPI allows us to expose our agentic system through an API.
# main.py
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
from process import *
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI is working!"}
class ChatRequest(BaseModel):
message: str
max_turns: int = 2
@app.post("/chat/")
async def chat(request: ChatRequest):
try:
chat_result = joe.initiate_chat(
recipient=cathy,
message=request.message,
max_turns=request.max_turns
)
return {
"chat_history": chat_result.chat_history,
"summary": chat_result.summary
}
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
**joe.initiate_chat()**: Starts a conversation where:
recipient=cathy: Joe will initiate a conversation with Cathy.message=request.message: The input message from the user is passed to the conversation.max_turns=request.max_turns: Limits the number of exchanges between Joe and Cathy.
Running the API
Now you can start the FastAPI server with:
uvicorn main:app --reload
Your API will be available at [http://localhost:8000/](http://localhost:8000/.)
Testing with Postman
- Open Postman and create a new POST request to
[http://localhost:8000/chat/.](http://localhost:8000/chat/.) - Set the request body to JSON:
{
"message": "Good evening World! You are tuned in with BBC news.",
"max_turns": 5
}
Now you can send the request and check the response, which is an AI-generated conversation.
"chat_history": [
{
"content": "Good evening World! You are tuned in with BBC news.",
"role": "assistant",
"name": "joe"
},
{
"content": "Good evening and welcome to BBC News. I'm Joe, bringing you the latest updates from around the globe. We have an exciting lineup for you tonight, covering everything from breaking news to in-depth analysis on the stories shaping our world. Let’s get started.",
"role": "user",
"name": "cathy"
},
{
"content": "Thank you, Cathy! We’ll kick things off with our Local news segment. \n\nIn local news tonight, the city’s council has announced a major initiative to improve public transportation, aiming to reduce congestion and increase accessibility. Officials say this will include new bus routes and upgraded services for residents.\n\nAdditionally, a local charity has launched a drive to support underprivileged families this winter, collecting food and warm clothing items. If you’d like to donate, drop-off centers are available all over the area.\n\nNow over to Cathy for our Foreign news.",
"role": "assistant",
"name": "joe"
},
{
"content": "Thank you, Joe! \n\nIn foreign news tonight, tensions continue to escalate in the Middle East as diplomatic efforts to broker peace see little progress. Leaders from involved countries are set to meet next week in an attempt to address the ongoing conflict and humanitarian crisis.\n\nMeanwhile, in Southeast Asia, a powerful earthquake has struck off the coast of Indonesia, triggering fears of a potential tsunami. Emergency services are on high alert, and local authorities are assessing the situation as residents are urged to stay cautious.\n\nWe’ll keep you updated on these developments as they unfold. Now, back to Joe with news from the world of business.",
"role": "user",
"name": "cathy"
},
{
"content": "Thank you, Cathy, but I believe we need to continue with our Entertainment segment next. \n\nIn Entertainment news, the highly anticipated film festival is just around the corner, featuring a lineup of both established filmmakers and emerging talents. This year, there’s a strong focus on diversity, with a record number of films directed by women and people of color.\n\nAdditionally, a popular British pop star has announced a surprise reunion tour after a decade-long hiatus. Fans are eagerly awaiting ticket sales, which are expected to sell out quickly.\n\nNow, over to Cathy for the latest updates in Sports.",
"role": "assistant",
"name": "joe"
},
{
"content": "Thank you, Joe!\n\nIn sports news tonight, the excitement is palpable as the Premier League gears up for a thrilling weekend. Several top teams are set to clash, including a much-anticipated match between last year's champions and their closest rivals. Fans are eager to see if their team can secure a crucial victory ahead of the upcoming international break.\n\nOn the international stage, the Rugby World Cup continues to captivate audiences, with thrilling matches leading to the knockout stages. Teams from around the world are showcasing their talents, and the atmosphere is electric as fans rally behind their countries.\n\nAnd that wraps up our sports segment! Now, let’s return to Joe for an update on the weather.",
"role": "user",
"name": "cathy"
},
{
"content": "Thank you, Cathy!\n\nIn our Weather segment tonight, we can expect some varied conditions across the country. The southeast will see sunny spells with mild temperatures, a perfect evening to enjoy outdoors. However, in the northwest, be prepared for rain showers and cooler temperatures as a weather front moves in.\n\nThe weather outlook for the weekend indicates a mix of sunshine and clouds, with some regions experiencing occasional rain. Be sure to keep your umbrellas handy if you're in the northern areas.\n\nThat concludes our weather update for now, and it brings us to the end of tonight's news session. Thank you for joining us. We'll see you again tomorrow night right here on BBC News! Good night!",
"role": "assistant",
"name": "joe"
},
{
"content": "Good night, everyone! Thank you for tuning in to BBC News. We appreciate your viewership and hope to see you again for more updates and stories that matter. Remember to stay informed and take care. Good night!",
"role": "user",
"name": "cathy"
}
]
I've taken you on the journey of building an AI agentic system using AG2 and FastAPI, demonstrating how agents can interact in structured conversations.
This setup can be extended to various use cases, such as automated customer support, collaborative problem-solving, and more.
Go ahead and build your agentic system with AG2!!
Happy coding!
메타데이터
- post_id
- f1bf8f478f42
- slug
- building-ai-agentic-systems-with-ag2-and-fastapi-f1bf8f478f42
- url
- https://pub.towardsai.net/building-ai-agentic-systems-with-ag2-and-fastapi-f1bf8f478f42
- canonical_url
- https://pub.towardsai.net/building-ai-agentic-systems-with-ag2-and-fastapi-f1bf8f478f42
- author_url
- https://medium.com/@sandanisesanika
- status
- ok
- fetched_at
- 2026-06-10 08:17:25