|
| 1 | +# SQLite Agent API Reference |
| 2 | + |
| 3 | +## Dependencies |
| 4 | + |
| 5 | +**Required:** |
| 6 | +- [sqlite-mcp](../sqlite-mcp) – MCP client for tool access |
| 7 | +- [sqlite-ai](../sqlite-ai) – LLM inference and embeddings |
| 8 | + |
| 9 | +**Optional:** |
| 10 | +- [sqlite-vector](../sqlite-vector) – For vector search (auto-enabled in table mode) |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Functions |
| 15 | + |
| 16 | +### `agent_version()` |
| 17 | + |
| 18 | +Returns the extension version. |
| 19 | + |
| 20 | +**Syntax:** |
| 21 | +```sql |
| 22 | +SELECT agent_version(); |
| 23 | +``` |
| 24 | + |
| 25 | +**Returns:** `TEXT` – Version string (e.g., "0.1.0") |
| 26 | + |
| 27 | +**Example:** |
| 28 | +```sql |
| 29 | +SELECT agent_version(); |
| 30 | +-- 0.1.0 |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +### `agent_run()` |
| 36 | + |
| 37 | +Runs an autonomous AI agent that uses MCP tools to accomplish a goal. |
| 38 | + |
| 39 | +**Syntax:** |
| 40 | +```sql |
| 41 | +-- MODE 1: Text response |
| 42 | +SELECT agent_run(goal); |
| 43 | +SELECT agent_run(goal, max_iterations); |
| 44 | + |
| 45 | +-- MODE 2: Table extraction |
| 46 | +SELECT agent_run(goal, table_name); |
| 47 | +SELECT agent_run(goal, table_name, max_iterations); |
| 48 | +SELECT agent_run(goal, table_name, max_iterations, system_prompt); |
| 49 | +``` |
| 50 | + |
| 51 | +**Parameters:** |
| 52 | + |
| 53 | +| Parameter | Type | Required | Default | Description | |
| 54 | +|-----------|------|----------|---------|-------------| |
| 55 | +| `goal` | TEXT | Yes | - | Task or goal for the agent | |
| 56 | +| `table_name` | TEXT | No | NULL | Target table (NULL for text mode) | |
| 57 | +| `max_iterations` | INTEGER | No | 5 | Maximum iterations | |
| 58 | +| `system_prompt` | TEXT | No | NULL | Custom system prompt | |
| 59 | + |
| 60 | +**Returns:** |
| 61 | +- **MODE 1 (text):** TEXT – Agent's final response |
| 62 | +- **MODE 2 (table):** TEXT – Status message (e.g., "Inserted 5 rows into listings") |
| 63 | + |
| 64 | +**MODE 1: Text Response** |
| 65 | + |
| 66 | +Agent returns text after using tools. |
| 67 | + |
| 68 | +```sql |
| 69 | +SELECT agent_run('Find affordable apartments in Rome', 8); |
| 70 | +``` |
| 71 | + |
| 72 | +Returns: |
| 73 | +``` |
| 74 | +I found several affordable apartments in Rome: |
| 75 | +1. Bright Studio - €85/night |
| 76 | +2. Cozy Room - €72/night |
| 77 | +3. Modern Apartment - €95/night |
| 78 | +``` |
| 79 | + |
| 80 | +**MODE 2: Table Extraction** |
| 81 | + |
| 82 | +Agent fetches data and populates a table. |
| 83 | + |
| 84 | +```sql |
| 85 | +CREATE TABLE listings ( |
| 86 | + id INTEGER PRIMARY KEY, |
| 87 | + title TEXT, |
| 88 | + price REAL, |
| 89 | + embedding BLOB |
| 90 | +); |
| 91 | + |
| 92 | +SELECT agent_run('Find apartments in Rome under 100 EUR', 'listings', 8); |
| 93 | +-- Returns: "Inserted 5 rows into listings" |
| 94 | +``` |
| 95 | + |
| 96 | +**Auto-Features (MODE 2):** |
| 97 | + |
| 98 | +1. **Schema Inspection** – Reads table schema to understand target data structure |
| 99 | +2. **Structured Extraction** – Extracts data matching column names and types |
| 100 | +3. **Transaction Safety** – Wraps all insertions in BEGIN/COMMIT |
| 101 | +4. **Auto-Embeddings** – Generates embeddings for BLOB columns named `*_embedding` |
| 102 | +5. **Auto-Vector Index** – Initializes vector indices when embeddings are created |
| 103 | + |
| 104 | +**Custom System Prompt:** |
| 105 | + |
| 106 | +```sql |
| 107 | +SELECT agent_run( |
| 108 | + 'Find vegan restaurants', |
| 109 | + 'restaurants', |
| 110 | + 10, |
| 111 | + 'You are a restaurant finder. Focus on highly-rated establishments.' |
| 112 | +); |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Error Handling |
| 118 | + |
| 119 | +**Common Errors:** |
| 120 | + |
| 121 | +```sql |
| 122 | +-- Not connected to MCP |
| 123 | +SELECT agent_run('Find apartments', 5); |
| 124 | +-- Error: Not connected. Call mcp_connect() first |
| 125 | + |
| 126 | +-- Table doesn't exist |
| 127 | +SELECT agent_run('Find apartments', 'nonexistent', 5); |
| 128 | +-- Error: Table does not exist or has no columns |
| 129 | + |
| 130 | +-- LLM not loaded (table mode) |
| 131 | +SELECT agent_run('Find apartments', 'listings', 5); |
| 132 | +-- Error: Failed to create LLM chat context |
| 133 | +``` |
| 134 | + |
| 135 | +**Checking for Errors:** |
| 136 | + |
| 137 | +```sql |
| 138 | +SELECT |
| 139 | + CASE |
| 140 | + WHEN result LIKE '%error%' OR result LIKE '%ERROR%' |
| 141 | + THEN 'Error occurred' |
| 142 | + ELSE 'Success' |
| 143 | + END |
| 144 | +FROM (SELECT agent_run('Find apartments', 5) as result); |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Performance |
| 150 | + |
| 151 | +**Timing:** |
| 152 | +- **Agent Iterations**: 100ms-10s per iteration (LLM inference) |
| 153 | +- **Tool Calls**: 10-1000ms (network latency) |
| 154 | +- **Embedding Generation**: 10-100ms per text (model-dependent) |
| 155 | +- **Vector Index**: <100ms (initialization) |
| 156 | + |
| 157 | +**Tips:** |
| 158 | +- Use appropriate `max_iterations` (default: 5) |
| 159 | +- Reuse MCP connections (global client persists) |
| 160 | +- Use the Agent in a separated thread |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## See Also |
| 165 | + |
| 166 | +- [USAGE.md](USAGE.md) – Usage guide and examples |
| 167 | +- [README.md](README.md) – Project overview |
| 168 | +- [sqlite-mcp API](https://github.com/sqliteai/sqlite-mcp/blob/main/API.md) – MCP extension API |
| 169 | +- [sqlite-ai API](https://github.com/sqliteai/sqlite-ai/blob/main/API.md) – AI extension API |
| 170 | +- [sqlite-vector API](https://github.com/sqliteai/sqlite-vector/blob/main/API.md) – Vector extension API |
0 commit comments