Skip to content

SQL Agent for Laravel

A self-learning text-to-SQL agent that converts natural language questions into SQL queries using LLMs.

Raw LLMs writing SQL hit a wall fast — schemas lack meaning, types are misleading, tribal knowledge is missing, and the same errors repeat endlessly. The root cause is missing context and missing memory.

Knowledge Base

Curated table metadata, business rules, and query patterns give the LLM the context it needs to write accurate SQL.

Self-Learning

When a query fails and the agent recovers, it saves that learning. Next time, it knows.

Multi-LLM Support

Any provider supported by Prism PHP — OpenAI, Anthropic, Ollama, Gemini, Mistral, xAI, and more.

SQL Safety

Configurable guardrails prevent destructive operations with statement restrictions and row limits.

  1. Assemble context — The agent retrieves relevant table metadata, business rules, query patterns, and past learnings based on the question
  2. Enter tool loop — The LLM iteratively introspects schema, searches for additional knowledge, executes SQL, and refines results
  3. Learn and improve — Errors become learnings, successful queries become reusable patterns — both feed back into future context assembly

See the Introduction for a detailed architecture overview.

Terminal window
composer require knobik/sql-agent
php artisan sql-agent:install

Then configure your LLM provider in .env and start asking questions:

use Knobik\SqlAgent\Facades\SqlAgent;
$response = SqlAgent::run('How many users signed up this month?');
echo $response->answer;

See the Installation guide for full setup instructions.

  • Multi-Database Support — MySQL, PostgreSQL, SQLite, and SQL Server
  • Multiple Search Drivers — Database full-text search or pgvector semantic search
  • Agentic Loop — Tool calling to introspect schema, run queries, and refine results
  • Livewire Chat UI — Ready-to-use chat interface with conversation history
  • Evaluation Framework — Test your agent’s accuracy with automated evaluations