Building a multi-tenant AI agent with tool-use loops
The architecture behind our 9-tool AI agent that handles database queries, file analysis, and report generation.
The tool-use pattern
Modern AI agents aren’t just chat interfaces — they’re orchestration engines. The key innovation is the tool-use loop: the AI model receives a user message, decides which tool to call, executes it, reads the result, and decides whether to call another tool or respond to the user.
Our agent supports up to 8 consecutive tool calls per conversation turn. This means a single user message like “Compare this month’s survey responses with last quarter and write a report” can trigger a cascade of database queries, data analysis, and workspace file writes — all without additional user input.
Nine purpose-built tools
Each tool in our system is designed for a specific data operation: query_database for SQL queries, read_project_file and list_project_files for file access, read_project_excel for spreadsheet analysis, write_workspace_file for report generation, fetch_webpage for web research, and three survey management tools for the full survey lifecycle.
The tools are dynamically assembled based on the user’s project assignments. If a user has access to two projects — one with a database and one with files only — the agent receives exactly the tools relevant to those projects. This prevents information leakage between tenants.
Multi-tenancy at every layer
Multi-tenancy isn’t just about database isolation. Every layer of the system enforces user boundaries: conversation history is scoped per user, tool parameters include project-specific credentials, workspace file writes are sandboxed to the user’s directory, and the system prompt is dynamically generated with the user’s specific context.
The AI model never sees another user’s data, conversation history, or project configuration. Each conversation is a fresh, isolated session with context loaded from the user’s state.
“The hardest part of building an AI agent isn’t the AI. It’s the plumbing that makes it safe and reliable.”


