Optimizing Text-to-SQL Pipeline Using Agent Experiments

idinsight · Tech · 2024 · source Agentic: yes · promotional bias: low · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: Text-to-SQL analytics for social-impact decision makers

IDInsight needed Ask-a-Metric to answer natural-language SQL questions over WhatsApp accurately enough for non-technical users, while keeping latency, cost, safety, and pipeline maintainability within production limits.

Constraints:

Architecture

Ask-a-Metric moved from a tightly coupled functional pipeline to a pseudo-agent architecture. IDInsight used CrewAI agents as an experimental search process to discover useful tool order, prompt wording, and task decomposition, then hardcoded the recurring agent behavior into a deterministic object-oriented pipeline. The deployed pipeline breaks text-to-SQL into smaller LLM/tool calls, shares only the required context at each step, uses fixed-scope database tools and guardrails, and returns answers through the WhatsApp-facing product.

Diagram confidence: reconstructed (reconstructed from prose).

flowchart LR
  WhatsApp_user_question["WhatsApp user question"] --> Query_processing["Query processing"]
  Query_processing --> Guardrails["Guardrails"]
  Guardrails --> Query_processing
  Query_processing --> Fixed_scope_database_tools["Fixed-scope database tools"]
  Fixed_scope_database_tools --> SQL_database["SQL database"]
  SQL_database --> Fixed_scope_database_tools
  Query_processing --> LLM_prompts["LLM prompts"]
  LLM_prompts --> Answer_response["Answer response"]
  CrewAI_experiment_harness["CrewAI experiment harness"] --> Query_processing
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
WhatsApp user question Natural-language analytics request from a non-technical user. Query processing
Query processing Coordinates the pseudo-agent flow and decomposes the question into smaller text-to-SQL steps. Guardrails, Fixed-scope database tools, LLM prompts
Guardrails Safety and security layer that can be modified independently from query processing. Query processing
Fixed-scope database tools Custom tools for actions such as schema reading, SQL execution, table selection, column selection, and sample-value extraction. SQL database, Query processing
SQL database Structured data source queried by the system. Fixed-scope database tools
LLM prompts Prompted model calls that execute bounded subtasks and synthesize responses. Query processing, Answer response
Answer response Final natural-language answer returned to the WhatsApp user. —
CrewAI experiment harness Development-time multi-agent experiment used to discover effective tools, prompts, and step sequences. Query processing

Data flow

A WhatsApp question is processed by an object-oriented pseudo-agent pipeline. The pipeline decomposes the request into steps such as selecting relevant tables, finding columns, extracting sample values, running scoped SQL/database tools, and synthesizing an answer, rather than passing the whole task to a free-form agent at runtime.

3 supporting quotes
“On a high level, Ask-a-Metric collects a user’s question–say, “How many beds are there in hospitals in Chennai?”–analyzes the SQL database it is connected to, and returns the answer to the user.”article, stated
“Break tasks into smaller steps: identify tables, find the best columns, extract sample column values, etc., each as separate steps.”article, stated
“Further, we refactored our original code into an object-oriented paradigm, breaking down the pipeline into different components - processing user queries, guardrails, and tools.”article, stated

Why this architecture

The team kept the useful reasoning pattern discovered by agents but removed the production cost of autonomous planning and reflection. A deterministic pseudo-agent pipeline gave them the agentic flow's accuracy while restoring latency, cost control, modularity, and maintainability.

3 supporting quotes
“The agentic pipeline gave us the desired performance, but we could not achieve the targeted response time or cost.”article, stated
“The agentic pipeline was predictable: The agent was very consistent in the sequence of actions it took to answer every question.”article, stated
“Only incorporate the actions that the agentic flow executed. Eliminate self-reflection and planning API calls.”article, stated

Models used: Large Language Model · CrewAI agents

Scale

Alternatives considered (roads not taken)

Keep the initial simple pipeline

Rejected because: It produced inaccurate responses, was tightly coupled, and made prompt experimentation slow.

“The pipeline resulted in LLM responses that were not accurate enough. We needed to break the problem into smaller steps and give the LLM more information”article, stated
“Every step was tightly coupled with every other step, which meant that changing a prompt could cause the entire pipeline to need modifications.”article, stated

Deploy CrewAI agents directly in production

Rejected because: The agentic pipeline answered test questions correctly but was too slow and expensive for partner use.

“In our tests, the CrewAI-based agentic pipeline performed excellently. It was able to answer all questions from our test database correctly.”article, stated
“However, the response time and cost of this version of the pipeline were prohibitively high”article, stated

Let the runtime agent keep full self-reflection and planning logs

Rejected because: The production pseudo-agent flow removed self-reflection, planning calls, and broad flow-log sharing to reduce cost and latency.

“Only incorporate the actions that the agentic flow executed. Eliminate self-reflection and planning API calls.”article, stated
“Share only the required data for each API call. Stop sharing flow logs with the LLM at each step.”article, stated

Use general-purpose agent tools at runtime

Rejected because: The deployed pipeline prefers fixed-scope custom tools that produce required outputs quickly.

“Build tools with fixed scope that quickly produce the required outputs.”article, stated

Patterns

tool-use-orchestration (orchestration)

The deployed pseudo-agent orchestrates scoped database, schema, SQL, RAG, and prompt steps instead of giving a runtime agent unrestricted tool choice.

“* SQL DDL schema tool to let the agent read the entire database’s schema,”article, stated

prompt-chaining (prompting)

The team decomposed text-to-SQL into multiple smaller LLM steps rather than asking one prompt to solve the full task.

“Break tasks into smaller steps: identify tables, find the best columns, extract sample column values, etc., each as separate steps.”article, stated

multi-agent-orchestration (orchestration)

CrewAI was used experimentally with a customer-facing manager and data analyst agent to discover a better workflow.

“Our agentic pipeline consisted of two agents—a customer-facing manager and a data analyst. We gave each agent relevant tools to execute their tasks.”article, stated

cost-aware-routing (optimization)

Production design removed expensive agent self-reflection and flow-log sharing to meet per-query cost and latency budgets.

“Share only the required data for each API call. Stop sharing flow logs with the LLM at each step.”article, stated

guardrails-io-filtering (safety)

The object-oriented pseudo-agent architecture separates guardrails so safety controls can change without destabilizing query processing.

“It also simplifies the process of building, managing and modifying our pipeline: for example, we can now make changes to the guardrails, without affecting query processing.”article, stated

Tradeoffs

Use agents as an experimentation tool rather than the production runtime.

Chose A deterministic pseudo-agent pipeline over Deploying the CrewAI agentic pipeline directly.

Rationale: The agentic version found the right decomposition and prompts, but the pseudo-agent version met latency and cost targets.

“though the agentic pipeline did not give us the optimal solution, it was good enough to satisfy our project requirements.”article, stated
“The pseudo-agent flow, on average, has a response time of less than 15 seconds and costs less than USD 0.02 per query.”article, stated

Hardcode the agent's repeatable behavior.

Chose Fixed step sequences and scoped tools over Autonomous runtime tool planning.

Rationale: The observed agent behavior was consistent enough to preserve the useful steps without paying for planning and reflection at runtime.

“We realized that we could use the same prompts the agent gave itself and hard-code the same steps it used to build our own pipeline.”article, stated

Limit context passed to each model call.

Chose Share only required data per API call over Passing full flow logs to the LLM at each step.

Rationale: The narrowed context reduced token usage and cost while preserving the task-specific information needed for each step.

“Share only the required data for each API call. Stop sharing flow logs with the LLM at each step.”article, stated

Gotchas (what bit them)

Prompt improvements did not generalize cleanly across question sets.

Root cause: Prompt changes affected different query types differently inside a tightly coupled sequential pipeline.

Resolution: Use agent experiments and a decomposed pseudo-agent pipeline to isolate steps and prompts.

“Minor changes in prompts to improve performance on one set of questions would reduce performance on another set, making it very difficult to improve the system as a whole.”article, stated

The original pipeline was hard to modify.

Root cause: The simple pipeline's sequential steps were tightly coupled.

Resolution: Refactor into object-oriented components for query processing, guardrails, and tools.

“Every step was tightly coupled with every other step, which meant that changing a prompt could cause the entire pipeline to need modifications.”article, stated
“we refactored our original code into an object-oriented paradigm, breaking down the pipeline into different components - processing user queries, guardrails, and tools.”article, stated

High-accuracy agentic execution was not production-viable.

Root cause: The full CrewAI flow incurred more than one minute of latency and about USD 0.30 per query.

Resolution: Use the agentic flow as a development-time optimizer and deploy a cheaper pseudo-agent flow.

“the response time and cost of this version of the pipeline were prohibitively high: answers took more than a minute to be generated, and the cost per query was approximately USD 0.3.”article, stated

Evaluation

The team compared the simple pipeline, CrewAI agentic experiments, and pseudo-agent production pipeline using accuracy, response time, and cost. CrewAI answered all test database questions correctly but failed latency and cost requirements; the pseudo-agent pipeline preserved the accuracy gain while meeting production economics.

Metrics: CrewAI answered all questions from the test database correctly · CrewAI took more than one minute per query · CrewAI cost approximately USD 0.30 per query · Production target: less than 30 seconds and less than USD 0.03 per query · Pseudo-agent average: less than 15 seconds and less than USD 0.02 per query

3 supporting quotes
“It was able to answer all questions from our test database correctly. However, the response time and cost of this version of the pipeline were prohibitively high”article, stated
“For our partners to be able to use AAM, we needed the query response time to be less than 30 seconds (two to three times faster)”article, stated
“The pseudo-agent flow, on average, has a response time of less than 15 seconds and costs less than USD 0.02 per query.”article, stated

Outcomes

The case reports per-query production targets and averages rather than deployed usage volume; the product is being piloted across multiple contexts in the social-impact sector.

3 supporting quotes
“The pseudo-agentic pipeline now allows us to test and make changes to the pipeline quickly.”article, stated
“we are working on improving our current solution along all three of our key metrics–accuracy, speed, and cost–while also building more features”article, stated
“We are also testing our solution in multiple contexts and using these pilots to get feedback and increase the value our solution brings to users.”article, stated