VillageSQL is a drop-in replacement for MySQL with extensions.
All examples in this guide work on VillageSQL. Install Now →
vsql_ai extension lets you call AI language models directly from SQL queries. Before you can run your first ai_prompt(), you need the extension installed and an API key ready. This guide covers setup, provider options, and the security considerations that matter before you go to production.
Install the Extension
Setting Your API Key
Theai_prompt() and ai_embedding() functions take the API key as a parameter. The recommended pattern is a session variable set at connection time — never embed a key literal in a query that could end up in a log file. For the 'local' provider, pass an empty string — no API key is required.
Choosing a Provider and Model
Anthropic
Best for: complex reasoning, long documents, code generation, and instruction-following tasks.ai_embedding().
Google Gemini
Best for: embedding generation and multimodal tasks.OpenAI
Best for: embedding generation with the widely-adoptedtext-embedding-3 family.
Local (Ollama)
Best for: local development and private data that can’t leave your network. Requires Ollama running on127.0.0.1:11434. No API key needed — pass an empty string.
llama3.2, mistral, gemma2. Common embedding models: nomic-embed-text, mxbai-embed-large.
Provider comparison
| Provider | Prompting | Embeddings | Best for |
|---|---|---|---|
'anthropic' | Yes | No | Reasoning, instructions, code |
'google' | Yes | Yes | Balanced; Gemini embeddings |
'openai' | Yes | Yes | OpenAI ecosystem; text-embedding-3 |
'local' | Yes | Yes | Local Ollama; no API key; private data |
Timeout and Rate Limit Considerations
AI API calls take 5–30 seconds. MySQL’s default query timeout may cut them off. For sessions that run AI queries, raise the execution time limit:ai_prompt() on each row can take 30+ minutes.
All providers enforce rate limits on how many requests you can make per minute. Hitting a rate limit causes ai_prompt() to return NULL. For bulk operations, process in batches of 50–100 rows and add a pause between batches in your application loop. See Running AI Models from MySQL Queries for batch patterns.
Frequently Asked Questions
Can I store API keys in a MySQL table instead of session variables?
You can, but you’re trading one security risk for another. A table approach requires a SELECT to read the key — that query shows up in logs too. Session variables set programmatically by your application framework are the cleanest option.What happens if the API call fails?
The function returns NULL. Check for NULL in your results when testing. Common causes: invalid API key, network timeout, rate limit hit, or an unsupported model name.Can I use different providers in the same query?
Yes —ai_prompt() is a regular function. You can call it with different provider/model combinations in the same SELECT.
Are model names stable?
Providers change model names over time. The names listed here match the extension’s current tested models. Check your provider’s documentation for the latest available model identifiers.Troubleshooting
| Problem | Solution |
|---|---|
FUNCTION ai_prompt does not exist | Run INSTALL EXTENSION vsql_ai |
ai_prompt() returns NULL | Check API key is valid; check max_execution_time is set high enough; verify model name |
| Query times out | Set SET SESSION max_execution_time = 60000 |
| Key shows up in slow query log | Use session variable set by application code, not a SQL string literal |

