Business users want to query analytical databases in plain English. Sonorca does exactly that — natural-language analytics on enterprise data. I came on as CTO and Lead Architect, and the interesting problem turned out not to be the one everyone expects.
Getting an LLM to write SQL is not hard. Building an agentic system a company can price, trust, and operate at scale is. That distinction shaped every architectural decision that followed.
The problem
The naive solution is an "LLM agent" with a single monolithic query the data tool. It demos beautifully and fails as a product:
- Unpredictable cost. An open-ended reasoning loop can run away in tokens and time — and you can't put a price tag on a product whose unit economics you can't forecast.
- Unauditable behavior. When something goes wrong, there's no trace of why the system decided what it decided.
- No error recovery. A failed query is a dead end rather than something the system corrects.
- 1000-line prompts that are really Python logic written in English. Business logic buried in prose can't be tested, versioned, or reasoned about.
The core architectural decision — Controlled Agency
Rather than one open-ended agent, I split the system into two purpose-built architectures, each with its own entry point, prompts, and cost profile.
| Standard Mode | Exploration Mode | |
|---|---|---|
| Pattern | Bounded LangGraph StateGraph pipeline |
True reasoning-loop agent (Think → Act → Observe) |
| Handles | ~80% of queries (metrics, breakdowns, follow-ups) | Root-cause and open-ended investigation |
| Autonomy | LLM decisions at fixed points | Agent selects tools and decides when it's done |
| Cost | Predictable per question | Variable but hard-capped per session |
The key judgment: true agency is powerful but expensive and unpredictable. So it is isolated to a premium tier and hard-capped, while the common path runs a deterministic, auditable pipeline.
Agentic patterns engineered
Bounded loops with graceful exits
A maximum of two clarification loops and two SQL-fix loops. On hitting the limit, the system assumes and explains rather than failing outright or looping forever.
Intent locking
Intent is fixed after the understanding node, so downstream SQL fixes never re-interpret the question — eliminating the oscillation that plagues naive retry loops.
Cross-model validation
A different model validates results than the one that interpreted intent, catching interpretation blind spots a single model would confidently repeat.
Model tiering for cost
The main model is reserved for judgment — understanding and validation. Mini models handle mechanical work like SQL generation and checks, with programmatic escalation when they fall short.
A granular tool ecosystem
In Exploration mode, fine-grained tools (getTables, getRelationships, validateQuery, estimateQueryCost, executeQuery, getUsageStatus) let the agent reason about what it needs instead of firing blind, broad queries.
Context compaction
A mini model summarizes every four to five reasoning steps, preserving findings and hypotheses while discarding verbose traces — which is what makes long investigations possible within token limits.
A two-level schema strategy
A compact conceptual schema supports understanding; the detailed schema is fetched on demand, only for relevant tables, at SQL-generation time.
Deterministic where possible, LLM where necessary
The semantic-onboarding pipeline was redesigned from roughly 25 LLM calls down to about 9. Fact and Dimension classification is handled deterministically from a data-engineering handoff contract. The LLM is reserved for work that is genuinely semantic: column meaning, concept-family assignment, and business context.
Every LLM call you can replace with a contract is a call that cannot hallucinate, cannot drift, and does not cost anything.
Impact
I designed a stateful, observable multi-agent architecture on LangGraph with LangSmith tracing throughout. The work applied production reliability patterns end to end: bounded autonomy, self-correction loops, cross-model validation, and proactive context and memory management (CAS with a sliding window and periodic summarization).
"Controlled agency over open-ended agents" — the discipline of knowing exactly where to grant autonomy and where to constrain it.
Tech & tools
Have a similar challenge?
If you're turning an open-ended LLM agent into something you can price, trust, and operate at scale — let's talk.
Get in touch →