<!--# if expr="$arg_server = /^(node|python|go|php|ruby|dotnet)$/" --><!--# set var="stack_server" value="$arg_server" --><!--# else --><!--# set var="stack_server" value="node" --><!--# endif --><!--# if expr="$arg_database = /^(postgresql|redshift|mysql|mssql|clickhouse|snowflake|bigquery|databricks)$/" --><!--# set var="stack_database" value="$arg_database" --><!--# else --><!--# set var="stack_database" value="postgresql" --><!--# endif --><!--# if expr="$arg_cache = /^(none|duckdb|redis)$/" --><!--# set var="stack_cache" value="$arg_cache" --><!--# else --><!--# set var="stack_cache" value="none" --><!--# endif --><!--# if expr="$arg_llm = /^(openrouter|google|anthropic|openai)$/" --><!--# set var="stack_llm" value="$arg_llm" --><!--# else --><!--# set var="stack_llm" value="openrouter" --><!--# endif -->
# Chat

An assistant that answers questions with reports.

> The code on this page is for the stack in its URL's query, by default `?server=node&llm=openrouter&database=postgresql&cache=none`. Server: `node`, `python`, `go`, `php`, `ruby`, `dotnet`. LLM: `openrouter`, `google`, `anthropic`, `openai`. Database: `postgresql`, `redshift`, `mysql`, `mssql`, `clickhouse`, `snowflake`, `bigquery`, `databricks`. Cache: `none`, `duckdb` (`node` only), `redis`. A missing or unknown value is the default, the first listed.

[Open the demo](https://quill.co/docs-demo/reporting/#/chat)

`chat-route.tsx`

Source is included with Quill Enterprise. [Get Quill Enterprise](https://cal.com/quillsql/30min)

`use-reporting-chat.ts`

Source is included with Quill Enterprise. [Get Quill Enterprise](https://cal.com/quillsql/30min)

`client.ts`

Source is included with Quill Enterprise. [Get Quill Enterprise](https://cal.com/quillsql/30min)

`message-report-embed.tsx`

Source is included with Quill Enterprise. [Get Quill Enterprise](https://cal.com/quillsql/30min)

<!--# include virtual="/docs-md/code/server-file/$stack_server/$stack_database/$stack_cache.md" -->

## Connect to your existing agent as a tool call

### 1. Add a new tool

<!--# include virtual="/docs-md/page/use-cases/chat/1/$stack_llm.md" -->

### 2. The tool call is a simple request to the Quill /ai endpoint

```ts
export async function askQuill(question: string, user: User) {
  const res = await fetch("https://api.quill.co/v1/ai", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.QUILL_PRIVATE_KEY}`,
      "Content-Type": "application/json",
      Accept: "text/event-stream",
    },
    body: JSON.stringify({
      publicKey: process.env.QUILL_PUBLIC_KEY,
      messages: [{ role: "user", content: question }],
      // From your signed-in user, never from the model
      tenants: [{ tenantField: "organization_id", tenantIds: [user.orgId] }],
    }),
  });
  const stream = await res.text();
  return stream.match(/"reportId":"(\w+)"/)?.[1];
}
```

### 3. Output of the tool call is the same param that useReport uses

```tsx
import { Chart, useReport } from "@quillsql/react";

export function Message({ message }: { message: ChatMessage }) {
  if (message.reportId) return <ReportMessage reportId={message.reportId} />;
  return <Bubble role={message.role}>{message.text}</Bubble>;
}

function ReportMessage({ reportId }: { reportId: string }) {
  const { name } = useReport(reportId);
  return (
    <Card title={name} action={<Link href={`/reports/${reportId}`}>Open in report builder</Link>}>
      <Chart reportId={reportId} />
    </Card>
  );
}
```

You can build any reporting experience with useReport. Here are some examples: [Chat](https://quill.co/docs/use-cases/chat.md), [Insight](https://quill.co/docs/use-cases/insight.md), [Reporting](https://quill.co/docs/use-cases/reporting.md) and [Drilldowns](https://quill.co/docs/use-cases/drilldowns.md).
