Lens Agent
Data analyst agent that queries databases, writes SQL, and generates insights from any messaging channel.
Lens Agent
The Lens Agent is an ephemeral data analyst that translates natural language questions into SQL queries, executes them against your connected databases, and returns structured insights with visualization suggestions. It works from any messaging channel: WhatsApp, Slack, Discord, or Telegram.
What Lens Does
Lens bridges the gap between your team's data questions and the databases that hold the answers. Instead of writing SQL or navigating dashboards, team members ask questions in plain language and receive:
- SQL query generation from natural language questions
- Result explanations in human-readable format
- Insight extraction highlighting patterns, anomalies, and trends
- Visualization suggestions recommending the best chart type for the data
- Follow-up questions to guide deeper exploration
Command
The lens command spawns an ephemeral Lens Agent that processes the question and terminates after delivering the response.
Examples
English
Portuguese
Supported Data Sources
| Database | Status | Connection |
|---|---|---|
| PostgreSQL | Available | Direct connection via DATABASE_URL |
| MySQL | Available | Direct connection via MYSQL_URL |
| BigQuery | Enterprise | Via MCP connector |
| Snowflake | Enterprise | Via MCP connector |
| Redshift | Enterprise | Via MCP connector |
Enterprise data sources (BigQuery, Snowflake, Redshift) are available through MCP connectors in CodeSpar Enterprise. See the Enterprise page for details.
How It Works
When a user sends a lens command, the following pipeline executes:
Response Format
Each Lens response includes five sections:
- Query -- the generated SQL, shown in a code block for transparency
- Explanation -- plain language summary of what the query does
- Insights -- key findings from the results (trends, outliers, comparisons)
- Visualization -- suggested chart type (bar, line, pie, table) with axis labels
- Follow-ups -- 2-3 suggested questions for deeper exploration
Example Response
Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
LENS_MODEL | Claude model used for SQL generation and analysis | claude-sonnet |
The Lens Agent uses the project's existing DATABASE_URL for PostgreSQL or MYSQL_URL for MySQL connections. No additional database configuration is required for these sources.
Query History
All Lens queries are tracked in the audit trail with the following metadata:
- Original question (natural language)
- Generated SQL query
- Execution time
- Row count returned
- User who requested the query
Query history enables teams to review what data was accessed, when, and by whom. It also helps the agent improve future responses by learning from previously successful queries.
RBAC
The lens command requires the view_status permission, which is granted to all roles by default:
| Role | Can use lens? |
|---|---|
| owner | Yes |
| maintainer | Yes |
| operator | Yes |
| reviewer | Yes |
| read-only | Yes |
| emergency_admin | Yes |
Since Lens is a read-only operation (it only runs SELECT queries), it is classified as low risk and never requires approval regardless of autonomy level.
Query Safety
The Lens Agent enforces read-only access:
- Only
SELECTqueries are generated and executed INSERT,UPDATE,DELETE,DROP, andALTERstatements are blocked at the query validation layer- Queries run with a read-only database connection when supported by the driver
- A query timeout of 30 seconds prevents long-running queries from impacting production databases