AI assistant

Sends a prompt to a language model with the schema of the tab's database (from the system catalogs, no table data) and the editor text. The answer is inserted into the editor and never executed by pgtc.

The Ask AI dialog over a query tab
F8 in a query tab.

Setting it up

The AI assistant settings dialog
Settings → AI…; a saved key shows as stars.
FieldMeaning
EnabledOff: F8 reports AI is not set up; no request is ever made.
PresetFills in protocol, base URL and model (table below); every field stays editable.
ProtocolOpenAI-compatible: POST {base URL}/chat/completions, key as Authorization: Bearer. Anthropic: POST {base URL}/v1/messages, key as x-api-key.
ModelThe models the provider lists for the key (GET {base URL}/models; Anthropic: /v1/models), read when the dialog opens and after the preset, base URL or key changes. Other… takes a model ID typed by hand. Default model of the F8 dialog.
EffortDefault sends no effort parameter. The OpenAI-compatible protocol has three levels (reasoning_effort): Extra high and Max are sent as high. Default effort of the F8 dialog.
API keyA saved key shows as ********: leave the stars to keep it, type over them to replace it, delete them to remove it. The key belongs to the saved base URL: another preset or URL empties the field, the saved URL brings the stars back. Empty: no key is sent (local servers).

Providers

PresetProtocolBase URLKey
ClaudeAnthropichttps://api.anthropic.comneeded
OpenAIOpenAI-compatiblehttps://api.openai.com/v1needed
GrokOpenAI-compatiblehttps://api.x.ai/v1needed
GeminiOpenAI-compatiblehttps://generativelanguage.googleapis.com/v1beta/openaineeded
Ollama (local)OpenAI-compatiblehttp://localhost:11434/v1no key

Any other OpenAI-compatible endpoint: (custom) with its base URL and model. With a local model the schema never leaves the machine.

Asking

F8 in a query tab, or Tools → Ask AI…. Refused unless:

When the dialog opens, pgtc reads the schema with one catalog query on the tab's own session (What is sent).

PartKeysNote
PromptAlt+PEnter is a new line. In an empty box ↑ / ↓ recall the prompts of this session. An unsent prompt is kept for the next dialog.
ScopeAlt+OWhole database or one schema — a smaller request.
Model, EffortAlt+M, Alt+EFor this request only; the settings are not changed. The list is the one saved in the settings (no network request).
Info lineSends about N tokens to host: the whole request at four characters per token. Above about 100,000 tokens a warning suggests one schema as scope. The line below it says what is sent: the schema, the editor text (or only the part around the cursor, or none when the editor is empty) and the prompt.
SendAlt+S, Ctrl+EnterWaits while the catalog query is still running.

Naming tables and columns: autocomplete

Autocomplete in the prompt box
After sales.: that schema's objects.

From the schema the dialog has read; no further catalog queries.

What is sent

One HTTPS request per Send, from pgtc directly to the base URL:

Never sent: rows, result sets, pg_stats, row counts, other databases, roles, connection parameters other than the database name.

Waiting, cancelling, errors

Progress in Messages

The steps of an AI request in the Messages tab
Messages while the model works.

The answer is a server-sent-event stream; its steps are written to the tab's Messages (pgtc does not switch there): the request with model, effort, estimated size and host; thinking… and, where the provider sends them, summaries of the model's thinking; writing… about every 1,500 characters; the token usage the provider reports, including the part served from its prompt cache; and the outcome with the elapsed time. The SQL is inserted only when the answer is complete.

The answer

The AI's answer in the editor
The prompt as comments, then the SQL. Nothing has run.

Privacy: AI not allowed

AI not allowed on a saved connection (Edit connection…; ai_not_allowed = true in settings.toml) refuses F8 in every query tab of that server, before any catalog query or request. Without it, the schema goes to the provider on every Send.

If it does not work

MessageCause and fix
HTTP 401 / 403No key, a wrong or expired key, or one not allowed to use the model.
HTTP 404 / model not foundWrong base URL, model name or protocol.
An error naming effort, output_config or reasoning_effortThe model takes no effort setting: choose Default.
HTTP 429Rate limit, or no credit left.
Could not connect to …The host of the base URL is unreachable — network, proxy, or a local model server that is not running.
-- (the answer was cut off here: it reached the model's length limit)Ask for less per request.
The schema of the database could not be readThe catalog query failed on the tab's session, usually in an aborted transaction (SQLSTATE 25P02): Query → Rollback, then F8 again.