![multi-agent AI solution that not only retrieves data but also performs actions using Azure OpenAI, LangChain, AutoGen, and Neon Serverless Postgres.](/_next/image?url=https%3A%2F%2Fneondatabase.wpengine.com%2Fwp-content%2Fuploads%2F2025%2F02%2Fmulti-au-agent-cover-image.png&w=3840&q=85&dpl=dpl_2bFyT7QWyWYwvrgzu2HTnQ2Q2oeD)
Making AI Work for Your Data
AI agents are becoming active participants in data management. Nowadays instead of writing SQL queries, you can simply ask and chat with your data:
“Show me all shipments currently in transit.”
Or even better, AI agents can act on your data:
“Create a new shipment for Marc and update its status to Departed Origin.”
Sounds very promising? Yes! In this post, we’ll explore how to build a simple multi-agent AI solution that not only retrieves data but also performs actions using Azure OpenAI, LangChain, AutoGen, and Neon Serverless Postgres.
You can find step-by-step instructions on how to run the sample project on the GitHub repository.
What is a Multi-Agent AI?
A multi-agent AI is a collection of multiple agents that work together to accomplish tasks. Each agent has a specific role and communicates with each other to share information and coordinate actions.
Different AI agents may require different levels of access depending on their role. If all agents had access to every table, they would be processing unnecessary data, and introduce data conflicts. We want each agent to work within its domain, keeping data clean and accurate. In our case, shipment management software uses Multi-agents AI where different agents handle shipments, customers, and product data in a structured way. For example, we need to manage:
- Customer & Product Information – Who is buying? What are they purchasing?
- Shipments & Logistics – Where are the shipments? What is their status?
Instead of mixing everything into a single large database (which can become complex and slow). These two datasets serve different purposes:
- A CRM database tracks customers and their orders.
- A shipment database tracks delivery details and logistics.
![Multi-Agent AI Solution with Neon, Langchain, AutoGen and Azure OpenAI](/_next/image?url=https%3A%2F%2Fneondatabase.wpengine.com%2Fwp-content%2Fuploads%2F2025%2F02%2Fmulti-ai-agents.png&w=3840&q=85&dpl=dpl_2bFyT7QWyWYwvrgzu2HTnQ2Q2oeD)
Technologies Used
We combine four technologies to build the example:
Technology | Purpose |
---|---|
Azure OpenAI GPT-4o | Understands natural language and generates SQL queries |
LangChain | Converts text-based user requests into structured database queries |
AutoGen | Manages multiple AI agents working together |
Neon | Stores CRM and Shipment data |
Step 1: Defining AI Agents & Their Roles
We will start by assigning a role for each AI agent with a specific purpose. For example:
- ShipmentAgent → Handles shipment-related queries and actions.
- CRMAgent → Manages customer and product-related data.
- SchemaAgent → Retrieves and shares schema information.
# agents.py
import autogen
from app.database import crm_db, shipment_db
from app.schema_functions import (
get_schema_info,
add_customer,
send_shipment,
)
from langchain_openai import AzureChatOpenAI
from langchain_experimental.sql import SQLDatabaseChain
from app.config import AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_DEPLOYMENT
...
shipment_agent = autogen.ConversableAgent(
name="ShipmentAgent",
llm_config=llm_config,
description="Manage shipments in the main database.",
system_message=(...),
)
crm_agent = autogen.ConversableAgent(
name="CRMAgent",
llm_config=llm_config,
description="Manages customer and product information in the second database.",
system_message=(...),
)
schema_agent = autogen.ConversableAgent(
name="SchemaAgent",
llm_config=llm_config,
description="Understands and shares database schema information.",
system_message=(...),
)
Step 2: Connecting Agents to the Database
Each agent must be able to interact with Neon databases to fetch or update information. To do this, we use the LangChain SQLDatabaseChain, which allows the agents to execute SQL queries on the database.
# agents.py
...
# Initialize the database chains
shipment_chain = SQLDatabaseChain(llm=azure_llm, database=shipment_db, verbose=True)
crm_chain = SQLDatabaseChain(llm=azure_llm, database=crm_db, verbose=True)
# Query functions for each database
def query_shipment(query):
return shipment_chain.invoke(query)
def query_crm(query):
return crm_chain.invoke(query)
Step 3: Assigning Actions to Each AI Agent
Each agent is given specific functions it can execute. For example, the ShipmentAgent should be able to call the send_shipment
function, and the CRMAgent should be able to call the add_customer
function.
# agents.py
...
# Register functions with the agents
shipment_agent.register_function(
function_map={"query_shipment": query_shipment, "send_shipment": send_shipment}
)
crm_agent.register_function(
function_map={"query_crm": query_crm, "add_customer": add_customer}
)
schema_agent.register_function(
function_map={
"get_schema_info": get_schema_info,
"get_shared_schema_info": get_shared_schema_info,
}
)
Step 4: Managing AI Conversations & Collaboration
These agents collaborate using AutoGen, an open-source framework from Microsoft that enables multi-agent orchestration and coordination. AutoGen allows AI agents to:
- Share knowledge by passing messages.
- Make decisions based on structured workflows.
- Take real-world actions like inserting data or calling stored procedures.
For those who prefer low code way to build Agentic workflows, Autogen has a studio.
If we put these three agents together, the flow looks like this:
- A user asks a question: “What products are currently in transit?” Langchain converts text-based user requests into structured database queries.
- ShipmentAgent queries the Neon Shipment database and returns results.
- A user requests an action: “Create a new shipment for Marc.”
- CRMAgent adds Marc as a customer, and ShipmentAgent creates the shipment.
# agents.py
...
# Create a user proxy agent
user_proxy = autogen.UserProxyAgent(
name="User_proxy",
system_message="A human admin.",
code_execution_config={
"last_n_messages": 4,
"work_dir": "groupchat",
"use_docker": False,
},
human_input_mode="ALWAYS", # Using this mode to give input to agents
)
# Set up the group chat and manager
groupchat = autogen.GroupChat(
agents=[user_proxy, schema_agent, shipment_agent, crm_agent],
messages=[],
max_round=30, # Maximum number of rounds in the conversation
)
manager = autogen.GroupChatManager(groupchat=groupchat)
Other Example Queries
Chat With Your Data
- “Is Alice Johnson a customer?”
AI-Assisted Database Management
- “I need a stored procedure to delete customers. Can you generate one?”
AI-Powered Actions
- “Create a new shipment of 1 Laptop and 1 Smartphone for Marc and update its status to Departed Origin from New York to Los Angeles.”
![Multi-Agent AI Solution in action](/_next/image?url=https%3A%2F%2Fneondatabase.wpengine.com%2Fwp-content%2Fuploads%2F2025%2F02%2FScreenshot-2025-02-09-at-2.43.03%E2%80%AFPM.png&w=3840&q=85&dpl=dpl_2bFyT7QWyWYwvrgzu2HTnQ2Q2oeD)
Why Neon for AI Agents?
AI-generated queries can be powerful but unpredictable. Neon’s branching feature allows us to test AI-generated queries on a separate database branch. If an AI agent generates an incorrect SQL query, the damage is contained within its branch rather than impacting the main production database. This means we can run experiments, perform QA, and validate data before merging changes into production and going live.
Oftentimes, AI agents might use databases that only be used for a few minutes before becoming inactive. With Neon, this isn’t a problem—you can have thousands of databases without cost for unused databases. It is possible to wake up databases when needed.
You can also assign different roles to each AI agent and control their access to data using Neon’s Row-Level Security (RLS). For example, the CRM agent could have read and write access to the customers and product tables, while the shipment agent is restricted to shipment-related data.
Ready to Build?
Check out the GitHub Repository
Neon is a serverless Postgres platform that helps teams ship faster via instant provisioning, autoscaling, and database branching. We have a Free Plan – you can get started without a credit card.