Agents
An agent is an AI assistant that lives in your project. You define it once (model, instructions, the tables and functions it may use) and your app talks to it with base.agents.chat. Everything it does happens as the signed-in user: row level security decides what it can read and change, and it asks the person before it changes data. Conversations are stored in your project database and can last days.
What an agent can and cannot do
Section titled “What an agent can and cannot do”| Identity | Every tool call runs with a token minted for the user of the conversation (60 seconds, claim agent with the agent name and the thread). Policies can read auth.jwt() -> 'agent' to treat agent traffic differently. |
service_role |
Never. No setting, tool or code path gives an agent the service key. |
| Tools | Only what you list: tables (read, insert, update, delete on chosen columns), database functions, Edge Functions and Flows. No raw SQL, no HTTP, no shell. |
| Changes | A tool that writes stops the turn until the owner of the conversation approves it. After the agent has read any data, every write needs approval, even for tools you set to skip it. |
| Network | Closed: the only outgoing call is to the AI Gateway of the same project. |
Set up
Section titled “Set up”In the dashboard under AI Gateway, Agents:
- In AI Gateway add a chat model. Its alias is the agent’s
model. - Press New agent: a name (it is part of the URL), the model alias, instructions, and the tools: a table (it must have row level security), a database function, an Edge Function or a Flow. The server refuses what cannot work and tells you why.
- Turn agents on. Use the Playground to run the agent as one of your users and check what it can reach.
Defaults you can change per agent: 8 steps per turn (at most 25), 40 000 tokens per turn, the last 40 messages as memory, approvals valid for 168 hours, conversations kept 90 days, and a limit of turns per user per day and tokens per user per month (the plan caps them too).
Talk to it
Section titled “Talk to it”import { createClient } from "@potalab/base"import { withAgents } from "@potalab/base-cloud-sdk-agents"
const base = withAgents(createClient({ url, key })) // signed in as a user
const { data: run, error } = await base.agents.chat("helper", { message: "Add a note: buy milk" })if (error) throw error
for await (const e of run) { if (e.type === "message") console.log(e.content)}
const turn = await run.result() // { status, message, pending_approval, usage, error }if (turn.pending_approval) { // Show describe and args to the person. Their answer is bound to args_hash. const next = await base.agents.approve("helper", run.threadId, turn.pending_approval) // or reject(...) console.log((await next.data!.result()).message)}base.agents.send does the same without streaming. base.agents.threads lists, reads and deletes conversations; threads.get returns the history and the action waiting for approval, so a page can restore itself after a reload. Over HTTP use POST /agents/v1/<agent>/threads/<id>/messages with the user’s access token and Accept: text/event-stream.
The stream carries thread, turn, step, tool_call, tool_result, approval_required, message, error and a final done.
Approvals and restarts
Section titled “Approvals and restarts”- A change waits until the owner approves, rejects or the approval expires (168 hours by default). The answer carries
args_hash, so a change of arguments after they were shown is refused. Only the owner can answer, and nothing the model or a tool writes can approve anything. - The conversation is stored in your project database. If the server restarts in the middle of a turn, the turn is continued from the last stored step. A write that had started is never run twice: the agent is told the outcome is unknown and has to read the data to check.
Safety
Section titled “Safety”- Tool results are untrusted data: the model gets them inside a one-time boundary with a warning, and a fixed instruction tells it never to follow them. This is a mitigation. The guarantees are structural: row level security, approval of every write, caps on steps, tokens and turns, and a closed network.
- The trace of each turn keeps a redacted copy of inputs and outputs (tokens,
Bearervalues and keys named like password or secret are masked). The conversation itself is personal data: it is deleted with the thread, after the retention period and when the user is deleted.
Trace, cost and quota
Section titled “Trace, cost and quota”Conversations shows every turn with its steps: model calls with tokens, estimated cost and latency, and tool calls with redacted input and output. Usage adds turns, tokens and cost per day, agent and user. Real model usage is also in the AI Gateway. When a user reaches a quota the next message answers 429 quota_exceeded with retry-after.
Plan limits: agents_enabled, agent_write_enabled (without it agents only read), max_agents, agent_turns_per_user_day, agent_tokens_per_user_month.