# Chat with Database

Introspects a database schema, converts questions to SQL, and runs read-only queries.

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown variants are available by appending `.md` to any URL or sending an `Accept: text/markdown` header. An agent skill is available at [/.well-known/agent-skills/site-skill.md](/.well-known/agent-skills/site-skill.md).



<DocsBaseSwitcher base="flue" agent="text-to-sql" />

<AgentPreview
  agent="text-to-sql"
  framework="flue"
  inputFields="[
  {
    name: &#x22;question&#x22;,
    label: &#x22;Question&#x22;,
    placeholder: &#x22;Show me the top 5 customers by revenue last quarter.&#x22;,
    type: &#x22;text&#x22;,
  },
]"
/>

## Summary [#summary]

The **Chat with Database Agent** answers questions about your data in plain
English. It introspects the schema, writes the SQL, runs it read-only, and
explains the results. Reach for it to give non-SQL users a safe way to query a
database, or to prototype analytics without hand-writing queries.

## Install [#install]

```bash
npx shadcn@latest add https://agentcn.vercel.app/r/flue/text-to-sql.json
```

## Composition [#composition]

```text
agents/
└── text-to-sql.ts            # Agent with the two database tools
tools/
├── introspect-schema.ts      # Lists tables and columns
└── run-query.ts              # Runs a read-only SELECT
lib/
└── db.ts                     # Database client (SQLite by default)
```

## Customization [#customization]

* **Swap the database.** Replace the client in `lib/db.ts` with Postgres or MySQL.
* **Allow writes (carefully).** `run-query.ts` blocks non-`SELECT` SQL by default.
* **Add a workflow.** Wrap introspect + query in a workflow with a typed result.
* **Swap the model.** Change the `model` field on the agent.
