Skip to main content
ClickHouse Client and clickhouse-local include an AI assistant that turns questions in natural language into SQL queries, runs them on your connection, and answers from their results. You ask a question by prefixing it with ? at the interactive prompt. The assistant is an agent: rather than generating one query blindly, it works towards the answer step by step with the help of the following tools:
  • list_databases - list the databases of the server
  • list_tables - list the tables of a database
  • show_create_table - read the CREATE TABLE statement of a table
  • consult_documentation - look an entity up in the embedded documentation (the system.documentation table)
  • read_query_log - read your earlier queries from system.user_query_log
  • run_readonly_query - run a read-only query without asking, under a sandbox (readonly = 1, a 30-second and a 10 GiB limit)
  • run_query - run any other query, after you confirm it
It is also given the last queries you ran in the session, with their results truncated and their error messages, so you can ask about “my last query” or “that error”. The queries it runs are displayed and executed in your terminal exactly as if you typed them. See the reference for what exactly each tool is allowed to do.

Prerequisites

The assistant needs a model provider. The quickest way is an Anthropic or OpenAI key in an environment variable:
Alternatively, you can provide a configuration file. When neither is set, the assistant uses the aiGenerate function of the server you are connected to. This takes two things on the server: default credentials configured for AI functions (the ai_function_text_default_credentials setting), and AI functions not being disabled (the allow_ai_functions setting, which is on by default). Without the credentials the assistant is not available - see providers.

Connecting to the ClickHouse SQL playground

We’re going to explore this feature using the ClickHouse SQL playground. We can connect to the ClickHouse SQL playground using the following command:
We’ll assume you have ClickHouse installed, but if not, refer to the installation guide

Asking ClickHouse questions in natural language

Now it’s time to start asking some questions!

Finding expensive housing markets

Let’s start by asking a question about house prices. The SQL playground contains a UK house prices dataset, which the assistant discovers on its own:
Once we press enter, the assistant prints its tool calls and its thoughts as it works. The output looks like this (shortened):
The assistant followed these steps:
  1. Schema discovery - it explored the databases and the tables of the relevant one
  2. Table analysis - it read the structure of the uk_price_paid table
  3. Query execution - it ran a query built from your question and the schema it discovered. The query only reads an ordinary table, so it ran as a read-only query without asking, and its result is displayed as if you had typed it
  4. Answer - it read the result and answered the question

Asking follow-up questions

The conversation keeps its context between questions in the same session, so a follow-up does not have to repeat what was already said, and the assistant does not have to discover the schema again:
To start a fresh conversation, type ? clear.

Queries that change data

The assistant runs only read-only queries on its own. Anything else - writes, DDL, SET, or a read that can reach outside of the server, such as a table function or a table of an external engine - is shown at the prompt first, and runs only after you confirm it. The one exception is reading the schema: for a table of a database backed by an external system (MySQL, PostgreSQL, a data-lake catalog), the assistant fetches the definition of the table from that system without asking. It reads metadata only, and the credentials in it are masked - see security. The confirmation looks like this:
The assistant acts with the permissions of your connection, so it cannot do anything you could not do yourself. On the SQL playground the demo user is read-only, so such queries fail there even when confirmed.