Agents reference v7

Reference for agent-related functions, views, and types. For guide-style documentation, see Agents.

Catalog views

aidb.agents

Agent configuration. There's no dedicated "list agents" function — query this table directly.

ColumnTypeDescription
iduuidInternal identifier.
nametextUnique agent name.
instructionstextThe agent's instructions.
modeltextThe model backing the agent.
purposetextName of the purpose in aidb.purpose_registry whose role the agent's tool calls run as. NULL means tool calls run as the caller.
delegatestext[]Names of agents this agent may delegate to.
toolstext[]Names of tools this agent may call.
output_typejsonbStructured output schema, built with aidb.output_type().
input_token_budgetintegerInput token budget per call.
output_token_budgetintegerOutput token budget per call.
max_iterationsintegerMaximum reasoning iterations per call.
timeout_secondsintegerMaximum wall-clock time per call.
budget_strategyaidb.budget_strategyBehavior when a budget is exceeded. See Types below.
presettextStored, but not currently resolved to any behavior.
created_attimestamptzCreation time.
updated_attimestamptzLast update time.

aidb.purpose_registry

Purposes an agent can hold. Each purpose maps a name to the Postgres role that an agent's tool calls run as. Members of aidb_users can read this view. Change it with the purpose management functions.

ColumnTypeDescription
nametextUnique purpose name.
roletextPostgres role that agents holding this purpose run their tool calls as.
descriptiontextFree-text description.
created_attimestamptzWhen the purpose was created.
updated_attimestamptzWhen the purpose was last changed.
deleted_attimestamptzWhen the purpose was deleted. NULL for an active purpose.

aidb.agent_tasks

One row per agent_converse call (a "task"), including its lifecycle status.

ColumnTypeDescription
task_iduuidInternal task identifier.
agent_iduuidThe agent that ran this task (aidb.agents.id).
conversation_iduuidThe conversation this task belongs to.
request_message_iduuidThe triggering message's id in aidb.conversation_log.
response_message_iduuidThe resulting answer's id in aidb.conversation_log, once complete.
caller_roletextPostgres role that invoked agent_converse.
statusaidb.task_statusCurrent lifecycle status. See Types below.
blockingbooleanAlways true today — every task runs synchronously.
errortextError message, if the task failed.
created_attimestamptzWhen the task started.
completed_attimestamptzWhen the task finished, if it has.

aidb.conversation_log

Every user prompt and agent answer, across every conversation — the same rows aidb.get_conversation() returns, unfiltered by conversation_id. Tool calls, model requests, and other internal activity aren't included; query aidb_internal.action_log directly for the full activity history.

ColumnTypeDescription
iduuidMessage id.
conversation_iduuidThe conversation this message belongs to.
agent_iduuidThe agent involved.
task_iduuidThe task this message was part of.
action_typeaidb.action_typeuser_prompt or answer.
payloadjsonbThe message's raw payload (a user_message or llm_response struct).
created_attimestamptzWhen the message was created.
completed_attimestamptzWhen it was completed.

aidb.conversations

One row per conversation, aggregated from aidb.conversation_log.

ColumnTypeDescription
conversation_iduuidThe conversation's id.
agent_iduuidThe agent involved.
parent_conversation_iduuidAlways NULL — conversation forking isn't implemented.
forked_from_message_iduuidAlways NULL — conversation forking isn't implemented.
statusaidb.task_statusStatus of the most recent task in this conversation.
message_countbigintNumber of messages in the conversation.
last_message_iduuidId of the most recent message.
created_attimestamptzWhen the conversation started.
updated_attimestamptzWhen the conversation was last updated.

Agent management functions

aidb.create_agent

Registers a new agent.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredUnique name for the agent.
instructionsTEXTRequiredInstructions that define the agent's behavior.
modelTEXTRequiredName of the model backing the agent. See Choosing a model.
toolsTEXT[]NULLNames of tools (from aidb.tools) the agent may call.
delegatesTEXT[]NULLNames of other agents this agent may delegate to.
purposeTEXTNULLName of a purpose from aidb.purpose_registry. The purpose's role is looked up each time the agent is invoked, and the caller must be a member of that role. See Purpose management functions.
output_typeJSONBNULLStructured output schema, built with aidb.output_type().
input_token_budgetINTEGERNULL (no limit)Input token budget per agent_converse call.
output_token_budgetINTEGERNULL (no limit)Output token budget per call.
max_iterationsINTEGERNULLMaximum reasoning iterations per call. A hard ceiling of 25 always applies regardless.
timeoutINTEGERNULL (300 applied at runtime)Maximum wall-clock seconds per call.
budget_strategyTEXT'attempt_complete'One of 'ignore', 'error', 'summarize', 'attempt_complete'. See Budgets and limits.
presetTEXTNULLStored, but not currently resolved to any behavior.

Returns

TABLE(error TEXT) — no rows on success; a single row with error set on failure (never raises for a validation/name-collision failure).

Example

SELECT aidb.create_agent(
    name                 => 'db_helper',
    instructions         => 'You are a helpful assistant that answers questions about this database.',
    model                => 'my_gpt',
    tools                => ARRAY['run_sql_query'],  -- optional; tool names from aidb.tools. Default: NULL (no tools)
    delegates            => NULL,                    -- optional; names of other agents to delegate to. Default: NULL (no delegation)
    purpose              => NULL,                    -- optional; name of a purpose from aidb.purpose_registry. Default: NULL (tool calls run as the calling role)
    output_type          => NULL,                    -- optional; structured output schema from aidb.output_type(). Default: NULL (plain-text answer)
    input_token_budget   => NULL,                    -- optional; input token cap per agent_converse call. Default: NULL (no limit)
    output_token_budget  => NULL,                    -- optional; output token cap per agent_converse call. Default: NULL (no limit)
    max_iterations       => NULL,                    -- optional; reasoning-round cap per call. Default: NULL (a hard ceiling of 25 always applies)
    timeout              => NULL,                    -- optional; wall-clock seconds cap per call. Default: NULL (300 seconds applied at runtime)
    budget_strategy      => 'attempt_complete',      -- optional; behavior when a budget is exceeded. Default: 'attempt_complete'
    preset               => NULL                     -- optional; reserved for future use, has no effect today. Default: NULL
);

aidb.update_agent

Updates an existing agent. Every parameter except name is optional and defaults to NULL, meaning "leave unchanged" — including budget_strategy, whose default here is NULL, not 'attempt_complete'.

Parameters

Same parameters as create_agent, all optional (defaulting to NULL) except name.

Returns

TABLE(error TEXT) — no rows on success; a single row with error set on failure (agent not found, invalid field value).

Example

SELECT aidb.update_agent('db_helper', model => 'my_new_gpt', max_iterations => 15);

aidb.delete_agent

Deletes an agent by name.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredName of the agent to delete.
forceBOOLEANNULL (false)If true, also deletes the agent's internal task/action-queue records so deletion can proceed even if it has conversation history. The conversation transcript itself (aidb.conversation_log) is preserved either way.

Returns

TABLE(error TEXT) — no rows on success; a single row with error set on failure, including when the agent has conversation history and force wasn't passed.

Example

SELECT aidb.delete_agent('db_helper', force => true);

Purpose management functions

Only members of the aidb_governance role can call these functions.

aidb.create_purpose

Creates a purpose. If a purpose with this name was deleted earlier, it's reactivated with the new role and description.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredUnique purpose name.
roleTEXTRequiredPostgres role that agents holding this purpose run tool calls as.
descriptionTEXTNULLFree-text description.

Returns

VOID. Raises an error if an active purpose with this name already exists.

Example

SELECT aidb.create_purpose('sales_reporting', 'reporting_role', 'Read-only access to the sales schema');

aidb.update_purpose

Changes a purpose's role or description. The change applies to every agent holding the purpose from its next invocation.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredName of the purpose to change.
roleTEXTNULLNew Postgres role. NULL leaves it unchanged.
descriptionTEXTNULLNew description. NULL leaves it unchanged.

Returns

VOID. Raises an error if no active purpose has this name.

Example

SELECT aidb.update_purpose('sales_reporting', role => 'sales_readonly');

aidb.delete_purpose

Deletes a purpose. The row stays in aidb.purpose_registry with deleted_at set. An agent holding a deleted purpose is refused when invoked.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredName of the purpose to delete.

Returns

VOID. Raises an error if the purpose doesn't exist or is already deleted.

Example

SELECT aidb.delete_purpose('sales_reporting');

Conversation functions

aidb.agent_converse

Runs an agent's reasoning loop against a prompt and returns its answer.

Parameters

ParameterTypeDefaultDescription
agent_nameTEXTRequiredName of the agent to converse with.
promptTEXTRequiredThe prompt to send.
conversation_idTEXTNULLContinue an existing conversation. Omit to start a new one (an id is generated automatically).
read_onlyBOOLEANNULLForce read-only mode on or off. Auto-enabled on a read replica when omitted.
output_typeJSONBNULLOverrides the agent's configured structured output schema for this call only.
debugBOOLEANNULLIf true, also emit every action as a NOTICE. See Debug mode.

Returns

TABLE(message TEXT, conversation_id TEXT, error TEXT) — one row. Never raises for an agent-logic failure; error is populated instead, and message/conversation_id are NULL. conversation_id is also NULL for any read-only run, since nothing is persisted to attach an id to.

Example

SELECT * FROM aidb.agent_converse(
    agent_name      => 'db_helper',
    prompt          => 'How many rows are in the orders table?',
    conversation_id => NULL,  -- optional; continue an existing conversation by id. Default: NULL (starts a new conversation)
    read_only       => NULL,  -- optional; force read-only mode on/off. Default: NULL (auto: on for a read replica, off otherwise)
    output_type     => NULL,  -- optional; override the agent's structured output schema for this call. Default: NULL (use the agent's own configuration)
    debug           => NULL   -- optional; also emit every action as a NOTICE. Default: NULL (false)
);

aidb.start_agent_session

Mints a fresh conversation id for a named agent, ahead of the first agent_converse call that will use it.

Parameters

ParameterTypeDescription
agent_nameTEXTName of the agent to converse with.

Returns

TABLE(conversation_id TEXT). Unlike agent_converse, this function raises (rather than returning an error column) if agent_name is empty or unknown.

Example

SELECT * FROM aidb.start_agent_session('db_helper');

aidb.get_conversation

Retrieves a conversation's messages — every user prompt and final answer, in chronological order. Excludes tool calls and other internal activity; see aidb_internal.action_log for the full activity history.

Parameters

ParameterTypeDescription
conversation_idTEXTThe conversation's id.

Returns

TABLE(message_id TEXT, task_id TEXT, action_type TEXT, role TEXT, sender_id TEXT, contents TEXT) — one row per message. role is "user" or "agent"; sender_id is the specific sender (the prompt's own user field, or the agent's id). Raises (rather than returning an error column) if conversation_id is empty.

Example

SELECT * FROM aidb.get_conversation('8f14e45f-ceea-467e-9575-a3d1a2c1a123');

aidb.get_message

Retrieves one message's plain-text contents by id.

Parameters

ParameterTypeDescription
message_idTEXTThe message's id.

Returns

TABLE(message TEXT, error TEXT) — one row. error (not message) is populated when no such message exists; doesn't raise.

Example

SELECT * FROM aidb.get_message('3fa85f64-5717-4562-b3fc-2c963f66afa6');

Structured output helpers

aidb.output_field

Builds one field of an agent's structured output schema.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredThe field's name.
field_typeTEXTRequiredThe field's type (for example, 'TEXT', 'FLOAT', 'BOOLEAN').
descriptionTEXTNULLDescription shown to the model.

Returns

JSONB — pass one or more of these to aidb.output_type().


aidb.output_type

Builds a complete structured output schema from one or more fields.

Parameters

ParameterTypeDescription
fieldsVARIADIC JSONB[]One or more aidb.output_field() results.

Returns

JSONB — pass to create_agent/update_agent's output_type parameter, or agent_converse's.

Example

SELECT aidb.create_agent(
    name         => 'sentiment_tagger',
    instructions => 'Classify the sentiment of the given text.',
    model        => 'my_gpt',
    output_type  => aidb.output_type(
        aidb.output_field('sentiment', 'TEXT', 'One of: positive, negative, neutral'),
        aidb.output_field('confidence', 'FLOAT')
    )
);

Types

aidb.budget_strategy

CREATE TYPE aidb.budget_strategy AS ENUM (
    'ignore',
    'error',
    'summarize',
    'attempt_complete'
);

See Budgets and limits for what each value does.

aidb.task_status

CREATE TYPE aidb.task_status AS ENUM (
    'PENDING',
    'IN_PROGRESS',
    'RECOVERY',
    'AWAIT_APPROVAL',
    'DENIED',
    'ERROR',
    'TIMEOUT',
    'CANCELED',
    'SUCCESS',
    'EVALUATING',
    'COMPLETE'
);

Surfaced through aidb.agent_tasks.status and aidb.conversations.status. Since every task today runs synchronously to completion within one agent_converse call, a task's terminal status is normally SUCCESS or ERROR; the remaining values are reserved for asynchronous/approval workflows that aren't implemented yet.