WorkData · Backend · AI · Cloud
Data platform with an AI assistant
I unified a school network's scattered data in PostgreSQL and built an assistant that answers natural-language questions through tool calling on Amazon Bedrock. Permissions are enforced in the database, not in the prompt.
- Role
- Technical lead. Data, backend, AI and infrastructure
- Period
- Oct 2026 — Present
- Status
- In production
- Team
- Two people. A teammate built the web dashboard on top of the data contract I defined
Professional project: the name and client details are anonymized. The technical description is real.
Context
A network of technical high schools had its information spread across systems that do not talk to each other: the digital class register, the staff attendance system, the virtual classroom, HR spreadsheets and the ministry's official data.
I am the technical lead. I designed the data model, the pipelines, the assistant and the infrastructure, and I defined the data contract a teammate used to build the web dashboard.
The problem
Answering simple questions, such as how many students a school has or what a class's attendance looks like, meant opening spreadsheets by hand. Class register exports are Excel files between 65 MB and 1 GB, and every source names and structures its data differently.
On top of that, this is data about minors and employees: it cannot be exposed freely or sent unchecked to an AI model.
What I built
A unified PostgreSQL 16 database with 4 schemas, 37 tables, 57 views and 43 functions, versioned in 32 numbered SQL migrations and backed by 24 data-quality verification queries.
Idempotent, audited Python pipelines. They read the Excel files straight from S3 using byte ranges, without downloading them, and reduce tens of millions of attendance rows to one summary per student per day, loaded with
COPY.Three external sources integrated through read-only clients: the staff attendance API and the virtual classroom, with automated nightly loads, and the ministry's open data, loaded with a script whenever a new year is published.
An assistant that answers in natural language through 48 query tools (tool calling). Each tool queries a view and returns the finished calculation, along with the source and the date of the data.
A swappable AI provider layer (Amazon Bedrock in production, Gemini in development) with retries, a fallback model, timeouts and repair of malformed tool calls.
A regression set of 83 questions checked automatically: 55 against the result of a SQL query and the rest against expected text, required tools and forbidden patterns. Plus 155 unit tests that run with no database, no S3 and no AI.
"Teach the assistant": notes that are added to its instructions without a deploy, with mandatory approval by a second person, size limits and an audit trail.
In the web app (Next.js), the admin sections: assistant usage per person, errors, actual AI cost and the questions the assistant could not answer. The metrics dashboard and the chat were built by my teammate.
The AWS deployment: an EC2 Graviton server running Docker Compose, encrypted daily backups to S3 and CI/CD with GitHub Actions.
Architecture
Everything runs on a single EC2 server with Docker Compose, except the heavy class register and ministry loads, which run off-server and are uploaded as a database dump. PostgreSQL and the assistant only listen on the internal network; nginx is the only public entry point.
- Client
- Edge and network
- Compute
- Managed service
- Data
- External system
- Access:
- Network staff: Google Workspace
- nginx: TLS · rate limit
- Application:
- Web app: Next.js · Auth.js
- Assistant: Python · 48 tools
- AI and data:
- Amazon Bedrock: Converse API
- PostgreSQL 16: 57 views · 4 roles
- Amazon S3: raw files · backups
- Pipelines:
- Python pipelines: idempotent · audited
- Sources:
- Class register: Excel · up to 1 GB
- Staff attendance: REST API
- Virtual classroom: CSV export
- Ministry: open data
Connections:
- Network staff → nginx
- nginx → Web app
- Web app → Assistant
- Web app → PostgreSQL 16
- Assistant → Amazon Bedrock (tool calling)
- Assistant → PostgreSQL 16 (read-only)
- PostgreSQL 16 → Amazon S3
- Python pipelines → PostgreSQL 16 (COPY)
- Class register → Python pipelines
- Staff attendance → Python pipelines
- Virtual classroom → Python pipelines
- Ministry → Python pipelines
Technical decisions
Permissions live in the database, not in the prompt
A language model can be talked into almost anything, so it does not decide who sees what. The assistant connects with a read-only role that can only reach aggregated views, sensitive data goes through
security definerfunctions, and the asker's identity comes from the session, never from an argument the model could make up.The model does not do the math
Models are bad at counting, adding and sorting. Totals, rankings and comparisons come precomputed from SQL or Python; the model only picks the tool and writes the answer.
Choosing the model with evidence
I ran the same regression against several models. The two cheapest ones failed more than half of the cases (13 and 16 correct out of 34, against 29 for the one I chose in that same run); once the regression was expanded, the one I chose reached 75 out of 78. I kept the cheapest model that passed, and every change to a tool description is re-tested before it ships.
Privacy by design
Only the columns that are needed get loaded. In behavior notes, national ID numbers, phone numbers and emails are masked and sensitive text is left out, and staff views only show groups of five or more people. It is the data minimization principle of Chile's data protection law (Ley 21.719) applied to the data model.
One small, locked-down server
For an internal system I chose a single server running Docker Compose over managed services: it is cheap and simple to operate. In exchange I hardened it: read-only containers with no Linux capabilities, administration through Session Manager instead of SSH, a least-privilege IAM role and nginx with TLS as the only public entry point.
Stack
- Data
- PostgreSQL 16
- PL/pgSQL
- Materialized views
- psycopg 3
- Backend and pipelines
- Python
- boto3
- openpyxl
- httpx
- pytest
- ruff
- AI
- Amazon Bedrock
- Converse API
- Tool calling
- Gemini API
- Web
- Next.js 15
- React 19
- TypeScript
- Auth.js
- Tailwind CSS 4
- Infrastructure
- AWS EC2 Graviton
- Amazon S3
- IAM
- Systems Manager
- Docker Compose
- nginx
- Let's Encrypt
- CI/CD
- GitHub Actions
- OIDC
- SSM Run Command
Outcome
In production for internal use, with an automatic deploy on every merge to
main: the pipeline assumes an AWS role through OIDC, with no stored keys, and checks service health when it finishes.The chosen model answered between 72 and 75 of the 78 questions correctly across the four most recent full regression runs.
20% fewer tokens per question after shortening the instructions and the tool descriptions, with actual AI cost tracked per day and per model.
Data validated against the official source: the track inferred for each class matches in every class that has official data, and the computed grade average is on average 0.04 away from the official one.
86 pull requests merged through a feature branch →
develop→mainflow; the last 24, since CI was set up, with automated tests on each one.
What I learned
Tool descriptions are part of the assistant's behavior: they get tested just like code.
Source data is never what its documentation says: field names that change between endpoints, grades exported twice, sheets renamed from one year to the next.
Some metrics should not be computed from your own data: the repetition rate derived from grades came out too high, so the official figure is used instead.
In PostgreSQL, reused CTEs and
grouping setsprevented filtering by school. Splitting the total out withunion alland using materialized views fixed performance.SQL migrations stay out of the automatic deploy: they are applied by hand and the pipeline warns when one is pending.