|
| 1 | +--- |
| 2 | +title: Neon |
| 3 | +description: Postgres for agents |
| 4 | +--- |
| 5 | + |
| 6 | +## Description |
| 7 | +Neon is a serverless Postgres platform that lets your agents create and manage databases on demand. It supports ephemeral or long-lived Postgres instances for storing structured data. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | +1. A Neon account. If you do not have one, sign up at [Neon](https://neon.tech). |
| 11 | +2. Get your API key from the [Neon Console](https://console.neon.tech/app/settings/api-keys). |
| 12 | +3. The tool will automatically install required dependencies: |
| 13 | + - neon-api |
| 14 | + - psycopg2-binary |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +agentstack tools add neon |
| 20 | +``` |
| 21 | + |
| 22 | +## Configuration |
| 23 | +```bash |
| 24 | +NEON_API_KEY=... |
| 25 | +``` |
| 26 | + |
| 27 | +## Available Tools |
| 28 | +Out of the box, these tools are ready to use: |
| 29 | + |
| 30 | +1. **`create_database`** |
| 31 | + Creates a new Neon project and returns a connection URI. Default settings create a database called `neondb` with the `neondb_owner` role. |
| 32 | + |
| 33 | + <Note>The agent's response to errors will depend on its reasoning about the specific situation. It may retry a failed action, but this behavior is probabilistic rather than deterministic.</Note> |
| 34 | + |
| 35 | +2. **`execute_sql_ddl`** |
| 36 | + Run schema commands (e.g., `CREATE TABLE`) using a connection URI and SQL command. Transactions are automatically handled. |
| 37 | + |
| 38 | +3. **`run_sql_query`** |
| 39 | + Run data queries (e.g., `SELECT`, `INSERT`) using a connection URI and SQL query. Results are returned as formatted strings. |
| 40 | + |
| 41 | +## Using the Tools |
| 42 | +Add the tools you need to your agent: |
| 43 | +```python |
| 44 | +@agent |
| 45 | +def db_writer(self) -> Agent: |
| 46 | + return Agent( |
| 47 | + config=self.agents_config['db_writer'], |
| 48 | + tools=[tools.create_database, tools.execute_sql_ddl, tools.run_sql_query], |
| 49 | + verbose=True, |
| 50 | + ) |
| 51 | +``` |
| 52 | + |
| 53 | +For a complete example, see `AgentStack/examples/web_researcher` where an agent stores collected data in Postgres as part of a multi-agent workflow. |
| 54 | + |
| 55 | +## Usage Note |
| 56 | +When you add the Neon tool to an existing project, all its tools will be added to every agent in the `agents.yaml` file. You may need to remove unused tools to keep each agent’s scope clear. |
| 57 | + |
| 58 | +## Customization |
| 59 | +When you run `agentstack tools add neon`, the tool is added to your project as source code, not as a dependency. This means you can add more capabilities, like Neon's database [branching](https://neon.tech/docs/introduction/branching). |
| 60 | + |
| 61 | +For examples of how to implement additional tools, see the existing implementation in `neon_tool.py`. For more on Neon's API capabilities, refer to the [neon.tech/docs/reference/api-reference](https://neon.tech/docs/reference/api-reference). |
0 commit comments