From a fixed graph to a ReAct agent: rebuilding a text-to-SQL agent on seven views

The first version of the agent answered single questions well. It broke on follow-ups, on questions that needed two views, and on questions the data could not answer. We replaced the fixed graph with a ReAct agent that chooses its own next step, and kept the guardrails in code, outside the model.

Đorđe SpasojevićAI EngineerPublished 3 November 2026 · 2 min read · Life Sciences · Python · LangGraph · SQL views · A2A

01

What the fixed graph did

The first version was a LangGraph pipeline with one path: classify the question, pick a view, write SQL, run it, format the answer. Each step ran once, in order. For a question that fitted one view, it worked. More than half of real user questions did not fit one view.

02

Where it broke

—

Follow-ups lost the thread."And for Germany?" started a new run with no memory of the first question.

—

Two views meant failure.The pipeline picked one view, so any question that needed a join failed at that step.

—

No answer still produced SQL.When the data held no answer, the graph still wrote a query, and the user got an empty or wrong table.

—

Every new question type meant a new branch.The graph grew with each exception.

03

What replaced it

A ReAct agent reasons, calls a tool, reads the result and decides the next step. It stops when it can answer, or when it can say why it cannot. The tools decide what it may do, not the prompt.

python
from langchain.agents import create_agent
from langchain.tools import tool
from langgraph.checkpoint.memory import InMemorySaver

@tool
def list_views() -> str:
    """List the seven reporting views: name and one line on what each holds."""
    return catalog.summary()

@tool
def describe_view(name: str) -> str:
    """Columns, types and allowed values for one view."""
    return catalog.describe(name)

@tool
def run_sql(query: str) -> str:
    """Run one read-only SELECT on the seven views. Returns at most 200 rows."""
    checked = guard.check(query)  # parses the SQL; SELECT only; the seven views only
    return db.fetch(checked, limit=200)

@tool
def search_documents(question: str) -> str:
    """Search assessment reports and guidance when the answer is in text, not a table."""
    return docs.search(question, k=5)

@tool
def search_web(question: str) -> str:
    """Last resort: public sources, always cited in the answer."""
    return web.search(question)

agent = create_agent(
    model,
    tools=[list_views, describe_view, run_sql, search_documents, search_web],
    system_prompt=SYSTEM_PROMPT,
    checkpointer=InMemorySaver(),  # keeps the conversation, so follow-ups work
)

Simplified. In production the checkpointer is SQL Server, LangSmith traces every run, and guard and catalog are the parts we spent most time on.

04

Why seven views, not the raw schema

The agent never sees the source tables. Seven views carry the definitions: what counts as a reimbursement decision, which date is the decision date, how a country is named. The model chooses between seven well-described things instead of 500+ tables, most of them junction tables, and the definitions live in one place the data team owns.

05

Enrichment and fallback, in a fixed order

SQL first, documents second, the web last and always cited. The system prompt states the order and the tool descriptions repeat it. Documents are less a fallback than enrichment: about 30% of answers draw on them. The web is the true fallback, used in under 5% of answers.

06

Talking to other agents

The agent is exposed over A2A, so other agents on the platform can ask it questions and it can ask them. Agents built by other internal teams call it when they need the data it can reach.

07

What we would do the same way again

—

Guardrails in code.The SQL guard runs on whatever the model writes, so a bad query never reaches the database.

—

An evaluation set before the switch.Run side by side with the live system on the same questions: completed without an error, 91% → 100%.

—

Speed measured on the same questions.10% quicker than the live system.

Build Notes · for engineersMore in Insights →

Bring the system you’re trying to make work.

Schedule a call with a partnerFree until 11 December