VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page 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.
SET @key = '...' statement itself is captured verbatim, key included, in performance_schema.events_statements_history — on by default in MySQL 8.4 — as well as any general or slow query log you enable. A session variable keeps the key out of the query that uses it, but not out of the statement that sets it. The only way to avoid this is to never send the key as literal SQL text at all: bind it as a prepared-statement parameter, or use a client-side substitution mechanism that never puts the literal in the SQL text the server parses.
Choosing a Provider and Model
Anthropic
Best for: complex reasoning, long documents, code generation, and instruction-following tasks.claude-fable-5— most capable, for demanding reasoning and long-horizon workclaude-opus-5— recommended default; complex agentic coding and enterprise workclaude-sonnet-5— best combination of speed and intelligenceclaude-haiku-4-5— fastest and most cost-effective
claude-opus-4-8, claude-opus-4-7, claude-opus-4-6, claude-opus-4-5-20251101, claude-sonnet-4-6, claude-sonnet-4-5-20250929.
Anthropic does not support 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
Timeout and Rate Limit Considerations
AI API calls take 5–30 seconds. MySQL does not limit query duration by default (max_execution_time is 0, unlimited), but if your application or a connection pool sets it, a 30-second AI call can be cut off. For sessions that run AI queries, raise or confirm the limit explicitly:
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. RunSHOW WARNINGS immediately after the call — vsql_ai raises a Warning 3200 with the actual failure reason (invalid key, unknown provider, unsupported model, etc.), which is far more diagnostic than a bare NULL. 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
See also
- Running AI Models from MySQL Queries — using ai_prompt() once the extension is configured
- Generating Vector Embeddings in MySQL — using ai_embedding() for semantic search

