Expand description
LLM-Native CONTEXT SELECT Query
This module implements the CONTEXT SELECT query extension for assembling LLM context from multiple data sources with token budget enforcement.
§Grammar
CONTEXT SELECT prompt_context
FROM session($SESSION_ID)
WITH (token_limit = 2048, include_schema = true)
SECTIONS (
USER PRIORITY 0: GET user.profile.{name, preferences},
HISTORY PRIORITY 1: LAST 10 FROM traces WHERE type = 'tool_call',
KNOWLEDGE PRIORITY 2: SEARCH knowledge_base BY SIMILARITY($query) TOP 5
);§Token Budget Algorithm
Sections are packed in priority order (lower = higher priority):
- Sort sections by priority
- For each section: a. Estimate token cost b. If fits in remaining budget, include fully c. Else, try to include truncated version
- Return assembled context in TOON format
§Section Types
GET: Fetch data from path expressionLAST N FROM: Fetch recent rows from tableSEARCH BY SIMILARITY: Vector similarity searchSELECT: Standard SQL subquery
Structs§
- Agent
Context Integration - Integration between CONTEXT SELECT and AgentContext
- Context
Query Builder - Builder for constructing CONTEXT SELECT queries programmatically
- Context
Query Options - Query-level options (WITH clause)
- Context
Query Parser - CONTEXT SELECT parser
- Context
Query Result - Result of executing a CONTEXT SELECT query
- Context
Recipe - A reusable context recipe that can be saved, versioned, and bound to sessions.
- Context
Recipe Store - Repository for storing and retrieving context recipes
- Context
Result - Result of executing a CONTEXT SELECT query
- Context
Section - A section in CONTEXT SELECT
- Context
Select Query - A CONTEXT SELECT query
- Hnsw
Vector Index - Production-ready vector index backed by HNSW from sochdb-index.
- Path
Expression - Path expression for GET
- Recipe
Metadata - Metadata for a context recipe
- Section
Priority - Priority-based section ordering helper
- Section
Result - Result of processing a single section
- Simple
Vector Index - In-memory vector index for CONTEXT SELECT SEARCH
- Vector
Index Stats - Statistics about a vector index collection
- Vector
Search Result - Result of a vector similarity search
Enums§
- Context
Parse Error - Parse error
- Context
Query Error - Context execution error
- Output
Format - Output format
- Section
Content - Content definition for a section
- Section
Transform - Transform to apply to section output
- Session
Binding - How a recipe is bound to sessions
- Session
Reference - Session reference (FROM clause)
- Similarity
Query - Similarity query for SEARCH
- Truncation
Strategy - Truncation strategy when budget exceeded
Traits§
- Embedding
Provider - Trait for providing text-to-embedding conversion
- Vector
Index - Trait for vector index implementations