Agent Backend: One Postgres vs. Redis + Kafka + Pinecone
A 200-line Postgres agent orchestrator shows your database can be the framework. What that agent backend gets right, and what production still needs.
An AI agent backend is the server-side layer that runs an agent's reason-and-act loop and supplies what the model alone lacks: tools it can call, sessions that remember earlier turns, context management for long conversations, approval gates for risky actions, streaming to the user, and a record of every run. Powabase ships that runtime built in, next to each project's Postgres.
Last reviewed: September 24, 2026
Most agents follow the ReAct pattern, short for reason and act, described by Yao et al. in 2022. The model reads the conversation, decides whether it needs a tool, and if so emits a tool call. The runtime executes that call, appends the result, and asks the model again. The loop ends when the model answers in plain text. The loop fits in a few dozen lines of code, and most of the engineering goes into the runtime around it. A good runtime caps the number of steps so a confused model can't spin forever, and forces a final answer on the last one. It runs read-only tool calls in parallel and write calls one at a time. It notices when the model repeats the same call with the same arguments and stops the run. It retries transient model errors, records token usage, and survives a client that disconnects halfway. An agent framework gives you the loop inside your own process, and running it in production stays your job. A backend runs the loop for you, next to your data. For that trade-off in detail, see Powabase vs a LangChain stack.
Tools are how an agent acts on the world, and they come in three kinds. Built-in tools cover common needs, such as reading and writing your database, calling an HTTP API, running code in a sandbox, reading and writing stored files, and searching or scraping the web. Custom tools call your own HTTP endpoints, described by a name, a description, and a JSON Schema for the arguments, so the model can use your business logic without seeing your code. MCP servers expose tools over the Model Context Protocol, an open standard where a client lists a server's tools with tools/list and calls them with tools/call, so one integration works across many agents. Good tool design matters more than tool count. Write precise descriptions and tight schemas, and keep permissions narrow, so a database tool reaches only the schemas and tables the agent needs. Point HTTP tools at fixed endpoints rather than letting the model choose any URL. Retrieval is a tool too. A knowledge-search tool lets the agent search your documents when it decides it needs them, and search again with a better query, instead of taking whatever one up-front search returned.
An agent has two kinds of memory. Short-term memory is the session: the ordered history of a conversation, including each user message, the agent's replies, every tool call, every tool result, and the token usage per run. On the next message, the backend reloads that history and rebuilds the prompt, so the agent picks up where it left off, even days later. Long-term memory is knowledge that outlives one conversation: user preferences, facts learned along the way, and summaries of past work. It is usually stored as rows with embeddings and recalled by meaning when it becomes relevant. Where both live matters. If sessions sit in a Redis cache, memories in a vector store, and users in Postgres, you have three systems to back up and keep in sync. Put sessions, memories, and app data in one Postgres and you can query a user's history with SQL under the same access policies as everything else. When a user asks you to delete their data, one transaction removes all of it. Agent memory in Postgres walks through that design, and what a vector database is covers the recall side.
Every model has a context limit, and agents reach it faster than chatbots because tool results are long: a database query or a scraped page can add thousands of tokens in one step. The backend has to handle this without the user noticing, usually by compaction. Before each model call, the runtime estimates the prompt's size. If it is close to the limit, it first prunes old tool results, replacing them with short placeholders while keeping the most recent turns intact. If that isn't enough, it summarizes the older part of the conversation with a small, cheap model and continues from the summary. If the model still rejects the prompt as too long, the runtime compacts and retries rather than failing the run. The opposite problem is an answer cut off by the output limit, which the runtime handles by asking the model to continue where it stopped. These details decide whether an agent can work through a long task, such as reviewing a large document set or a multi-hour support thread. Long-running agents on Postgres covers the durable-state side of the same problem.
Some actions shouldn't happen just because a model decided they should: refunding a payment, emailing a customer, deleting records, or changing production data. An agent backend handles this with hooks at fixed points in the run, such as before a tool call, after a tool call, and before the final answer. Most teams need only three kinds of hook. A rule hook checks the tool arguments against patterns and denies matches, such as any SQL containing DROP. A webhook hook sends the event to your own policy service, which can allow the call, deny it, or rewrite its arguments or output, for example to redact personal data. An approval hook pauses the run, tells your app which tool the agent wants to call and with what arguments, and waits for a person to approve or reject. On approval the tool runs. On rejection the agent is told no and chooses another approach. A timeout stops a forgotten approval from holding the run forever. Scope approvals to the risky tools only. Asking a person to approve every search makes the agent too slow to use, and people start approving without reading.
An agent run can take seconds or minutes, so a request that returns only at the end feels broken. Streaming fixes this by sending events as the run happens. Server-Sent Events (SSE) is the common choice: a one-way stream of named events over a normal HTTP response, which browsers support natively and proxies generally pass through. A good agent stream carries more than text. It announces each step, each tool call with its arguments, each tool result, the answer's tokens as they are generated, any approval request, and a final completion event with the session ID and usage. That lets your UI show "searching the knowledge base" or "running a query" while the agent works, and lets you render citations next to the answer. The backend also has to handle the user who closes the tab. It should stop the agent so it doesn't keep spending tokens, and save the partial run so the session history stays complete.
Start with one agent and a few good tools. Anthropic's guidance on building agents makes the same point: the most successful systems use simple, composable patterns, and add complexity only when it clearly helps. Reach for multiple agents when one prompt can't cover every domain well, when a task splits into stages, or when you want independent views of the same input. Most multi-agent setups take one of three shapes. A supervisor agent reads each request and delegates to a specialist, such as billing or technical support, then combines what they return. A sequential pipeline passes each agent's output to the next, for example extract, then analyze, then write. A parallel fan-out sends the same input to several agents at once and merges their answers. Each pattern needs the backend to track child runs, share a cancel signal, limit delegation depth, and keep one record of the whole thing. And when the order of steps is known in advance, you may not need agents to coordinate at all. A workflow with agents as steps gives you a fixed path with AI where it helps.
How Powabase does it
We run the agent runtime inside each Powabase project, next to your Postgres, auth, and documents. You define an agent with a model, a system prompt, tools, and knowledge bases, then call POST /api/agents/{id}/run/stream and receive the whole run as Server-Sent Events. Sessions, runs, and tool calls are stored in the project's database, so you can query them with SQL like any other table.
FAQ
A 200-line Postgres agent orchestrator shows your database can be the framework. What that agent backend gets right, and what production still needs.
How to build long-running agents on Postgres with durable, resumable state, so workflows survive failures and pick up where they left off.
Build an AI customer support agent on Postgres: tool-calling, pgvector semantic search, escalate-on-no-match, and one database instead of Pinecone plus a CRM.