Skip to main content
Define and version reusable SQL queries as components for consistent data operations across ensembles.

Overview

Query components enable you to:
  • Reuse SQL queries across multiple agents and ensembles
  • Version queries for reproducibility and rollback
  • Organize complex multi-step queries
  • Share queries across teams
  • A/B test different query strategies

Quick Start

1. Create a Query Component

Create a SQL file:

2. Add to Edgit

3. Reference in Your Ensemble

URI Format and Versioning

All query components use the standardized URI format:
Format breakdown:
  • query:// - Protocol identifier for query components
  • {path} - Logical path to the query (e.g., get-customer-profile, analytics/daily-sales)
  • [@{version}] - Optional version identifier (defaults to @latest)
Version format:
  • @latest - Always uses the most recent version
  • @v1 - Uses latest patch of major version (v1.x.x)
  • @v1.0.0 - Specific semantic version (immutable)
  • @prod - Custom tag for production queries
  • @staging - Custom tag for staging queries

Example URIs

How to Reference in Ensembles

There are three ways to reference queries in your ensembles: Use the query:// URI format to reference versioned query components:

2. Template Expression Format

Use ${components.query_name@version} to embed query references:

3. Inline Query

For simple operations or during development, use inline SQL directly:

Using Query Components

With Dynamic Parameters

Multi-Step Data Workflow

Query Types and Examples

Customer/User Queries

Aggregation Queries

Join Queries

Time Series Queries

Search/Filter Queries

Caching and Performance

Query components are automatically cached for 1 hour (3600 seconds) after first load.

Default Caching

Custom Cache TTL

Bypass Cache

Best Practices

1. Use Parameterized Queries

Always use parameters to prevent SQL injection:

2. Add Indexes for Performance

3. Optimize with LIMIT

4. Use Clear Naming

5. Comment Complex Queries

6. Version for Changes

Track query changes with semantic versioning:

7. Test Query Performance

Versioning Strategy

Development Workflow

Staged Testing

Rollback Strategy

Using ctx API in Agents

When building custom agents with TypeScript handlers, you can access SQL queries through the ctx API:

ctx.queries.getSql(name)

Get a SQL query template by name:

Using Query with D1

Parameterized Query Execution

Dynamic Query Selection

Batch Query Execution

Query with Result Processing

Troubleshooting

Query Not Found

Error: Component not found: query://get-customer-profile@v1.0.0 Solution:
  1. Check query exists: edgit list queries
  2. Check version: edgit tag list get-customer-profile
  3. Verify deployment: edgit tag show get-customer-profile@v1.0.0

SQL Syntax Error

Error: SQL parse error in query Solution:
  1. Test query directly in database client
  2. Check parameter names match query placeholders
  3. Verify SQL dialect matches database engine

Missing Parameters

Error: Parameter not found: ${customer_id} Solution:
  1. Ensure all ${param} placeholders have values
  2. Check parameter names match exactly (case-sensitive)
  3. Provide default values if needed

Performance Issues

Issue: Query takes too long Solutions:
  1. Add indexes on filtered columns
  2. Limit result set size
  3. Use EXPLAIN to analyze query plan
  4. Consider materializing common queries

Next Steps

Prompt Components

Reusable AI instructions

Config Components

JSON settings as components

Data Operation

Database query operations

Edgit Versioning

Version control for components