How to Configure LangChain ClickHouse Guardrails

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up langchain clickhouse guardrails by pairing agent tool rules with strict database roles. You restrict the database account to read-only queries on approved columns and tables. Most engineering teams start by giving an AI agent a shared database account. That broad account lets the model read every table or change live data. When a user prompt tricks the model, broad permissions turn a bad query into an incident. How to set up langchain clickhouse guardrails in the database Your firs

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up langchain clickhouse guardrails by pairing agent tool rules with strict database roles. You restrict the database account to read-only queries on approved columns and tables. Most engineering teams start by giving an AI agent a shared database account. That broad account lets the model read every table or change live data. When a user prompt tricks the model, broad permissions turn a bad query into an incident.

How to set up langchain clickhouse guardrails in the database

Your first layer of defense lives inside the database server itself. ClickHouse manages users, roles, row policies, settings profiles, and quotas as SQL access entities. To turn on SQL access control, you set a server storage path for access rules. You also need at least one admin account with user management rights enabled. An admin account can only grant permissions that it already holds itself.

With SQL access control active, you create a dedicated account for your LangChain agent. The user creation step lets you pick the login method and allowed client hosts. You can also assign default roles and specific user settings at creation time. Instead of granting access to an entire database, you grant read rights on specific columns. When you grant select rights on two columns, the agent can query only those columns. If the agent tries to query an unapproved column, the database blocks the query. A wildcard query that asks for all columns also fails and returns no data. It only works if the table holds just the columns granted to that user. In versions since 24.10, you can also grant select rights across table name prefixes.

How agent tools build and run database queries

On the app side, an AI agent pairs a chat model with one or more tools. The agent reads the user prompt and decides which tool to call for the task. A database agent translates natural language questions into SQL queries, runs them, and returns results. Framework settings often let you list which tables the agent can see in prompts. Some teams also attach custom code tools or workflow tools to check inputs before execution.

Prompt instructions and table lists help guide the model toward valid queries. Yet prompt rules alone do not stop a model from writing an unsafe statement. A model can still guess table names or write heavy joins across large datasets. That is why app checks must work alongside database roles, quotas, and row policies.

ClickHouse Access Management + AI Guardrails: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native setup solves and what it leaves open

Database roles and column grants solve the basic problem of who can log in. They also draw hard lines around which tables and columns an account can read. Quotas and settings profiles help cap how much compute a single user account consumes. These built-in controls stop an agent from dropping tables or reading forbidden columns.

Still, static database grants cannot inspect what an individual query does in flight. Every agent tool call shares the same database login and inherits the same static rights. If an approved column holds emails, phone numbers, card numbers, or national IDs, queries return them. The database sends those raw values straight into the model context unless another step rewrites them. Static roles also cannot pause a risky query to ask a human for approval. Teams still need a way to inspect statements, log agent actions, and rewrite sensitive fields.

Add runtime guardrails and masking beside ClickHouse

Database grants and network rules control who can reach ClickHouse. Even so, an allowed client or agent can send an alter mutation that deletes rows or read raw personal data. In practice, hoop.dev runs beside the server as a sidecar for your ClickHouse and checks native TCP queries with no change to your drivers.

With hoop.dev ClickHouse support, guardrails refuse banned queries before they run, and data masking rewrites matched string columns in results. The audit trail logs which entity types were masked and how many without storing raw values. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Can a LangChain agent run wildcard select queries when column grants are active?

No, ClickHouse refuses a wildcard select query when a user only has specific column grants. The query returns no data unless the table contains only the granted columns.

Why are prompt instructions not enough for langchain clickhouse guardrails?

Prompts only guide the chat model and can fail when users send tricky inputs. Database roles, column grants, and runtime checks enforce hard rules that the model enforces policy on.

How do you limit which hosts an AI agent can use to log in?

When you create the database user in ClickHouse, you specify the allowed client hosts. The database will reject login attempts from that agent user if they come from other hosts.

The query runs. The emails stay hidden.