This project demonstrates how to build an AI agent that can analyze SaaS usage data stored in a Neon Serverless Postgres database and explain billing anomalies in natural language using Azure AI Agent Service.
- No need for complex frameworks like LangChain, Semantic Kernel, or AutoGen
- Leverages Azure AI Foundry's built-in agent orchestration
- Connects to a real Postgres backend (Neon) to fetch and analyze data
- Beginner-friendly setup with clear steps
Imagine you run a SaaS platform and want to:
- Spot billing spikes (e.g., sudden API call increases)
- Ask natural questions like: "Why did my bill go up this week?"
- Get AI-powered insights directly from your usage database
.
├── billing_anomaly_agent.py # Main script to run the agent
├── billing_agent_tools.py # Tool functions used by the agent
├── init_usage_data.py # Optional: Populate sample data in Neon
├── requirements.txt # Python dependencies
├── .env # Environment variables (not committed)
└── README.md # This file
- An Azure subscription - Create one for free.
- Make sure you have the Azure AI Developer RBAC role assigned
- Install Python 3.11.x.
Open the new Neon Resource page on the Azure portal, and it brings up the form to create a Neon Serverless Postgres Resource. Fill out the form with the required fields and deploy it.
- After the resource is created, go to the Neon Serverless Postgres Organization service and click on the Portal URL. This brings you to the Neon Console
- Click “New Project”
- Choose an Azure region
- Give your project a name (e.g., “Postgres AI Agent”)
- Click “Create Project”
- Once the project is created successfully, copy the Neon connection string and note down. You can find the connection details in the Connection Details widget on the Neon Dashboard.
postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require
Create a new hub and project in the Azure AI Foundry portal by following the guide in the Microsoft docs. You also need to deploy a model like GPT-4o.
You only need the Project connection string and Model Deployment Name from the Azure AI Foundry portal. You can also find your connection string in the overview for your project in the Azure AI Foundry portal, under Project details > Project connection string.
Once you have all three values on hand: Neon connection string, Project connection string, and Model Deployment Name, you are ready to set up the Python project to create an Agent.
git clone https://github.com/your-org/billing-anomaly-agent.git
cd billing-anomaly-agent
python -m venv .venv
source .venv/bin/activate # or .venv\Scripts\activate for Windows
pip install -r requirements.txt
PROJECT_CONNECTION_STRING="<Your AI Foundry connection string>"
AZURE_OPENAI_DEPLOYMENT_NAME=""
NEON_DB_CONNECTION_STRING="<Your Neon connection string>"
python init_usage_data.py
python billing_anomaly_agent.py
- Go to Azure AI Foundry → Agents
- Select your billing agent
- Use the playground to ask:
- "Why did tenant_456 have a spike in API usage?"
- "What happened with storage for tenant_789 last week?"
Delete the agent from your script or the Azure portal after testing:
project_client.agents.delete_agent(agent.id)
Made with ❤️ using Python, Azure AI, and Neon.