Skip to main content

Module context_query

Module context_query 

Source
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):

  1. Sort sections by priority
  2. For each section: a. Estimate token cost b. If fits in remaining budget, include fully c. Else, try to include truncated version
  3. Return assembled context in TOON format

§Section Types

  • GET: Fetch data from path expression
  • LAST N FROM: Fetch recent rows from table
  • SEARCH BY SIMILARITY: Vector similarity search
  • SELECT: Standard SQL subquery

Structs§

AgentContextIntegration
Integration between CONTEXT SELECT and AgentContext
ContextQueryBuilder
Builder for constructing CONTEXT SELECT queries programmatically
ContextQueryOptions
Query-level options (WITH clause)
ContextQueryParser
CONTEXT SELECT parser
ContextQueryResult
Result of executing a CONTEXT SELECT query
ContextRecipe
A reusable context recipe that can be saved, versioned, and bound to sessions.
ContextRecipeStore
Repository for storing and retrieving context recipes
ContextResult
Result of executing a CONTEXT SELECT query
ContextSection
A section in CONTEXT SELECT
ContextSelectQuery
A CONTEXT SELECT query
HnswVectorIndex
Production-ready vector index backed by HNSW from sochdb-index.
PathExpression
Path expression for GET
RecipeMetadata
Metadata for a context recipe
SectionPriority
Priority-based section ordering helper
SectionResult
Result of processing a single section
SimpleVectorIndex
In-memory vector index for CONTEXT SELECT SEARCH
VectorIndexStats
Statistics about a vector index collection
VectorSearchResult
Result of a vector similarity search

Enums§

ContextParseError
Parse error
ContextQueryError
Context execution error
OutputFormat
Output format
SectionContent
Content definition for a section
SectionTransform
Transform to apply to section output
SessionBinding
How a recipe is bound to sessions
SessionReference
Session reference (FROM clause)
SimilarityQuery
Similarity query for SEARCH
TruncationStrategy
Truncation strategy when budget exceeded

Traits§

EmbeddingProvider
Trait for providing text-to-embedding conversion
VectorIndex
Trait for vector index implementations