Skip to content

Cortex Pipeline

The Cortex is ContextRouter’s AI orchestration layer. All graph logic — RAG, SQL analytics, commerce, and news processing — lives here. Cortex uses the Graph Compiler to transform declarative YAML templates into executable LangGraph state machines.

How Graphs Work

Every graph in Cortex follows the same lifecycle:

  1. Template loading — YAML parsed and validated via Pydantic (TemplateDefinition)
  2. Override merging — consumer project overrides merged with template defaults
  3. Node compilation — each node dispatched via PlatformToolRegistry based on tool_binding
  4. Security wrapping — every node wrapped by make_secure_node() with token scope enforcement
  5. Execution — compiled StateGraph processes messages through the node pipeline
YAML Template → Template Loader → Override Merge → Node Compilation → StateGraph
Security-Wrapped Execution

Pipeline Flow (RAG)

The default retrieval_augmented template processes queries through:

extract_query → detect_intent ─┬─→ retrieve → suggest ─┐
├─→ ground ──────────────→ reflect → __end__
├─→ generate ────────────→ reflect → __end__
├─→ plan → execute_sql → verify → visualize → reflect
└─→ no_results → __end__

Node Types

TypeDescriptionExample
platformInternal Router capability (LLM, retrieval, classification)router_classify, router_generate
federatedExecutes on consumer project via BiDi gRPC streamexport_products, store_news_results
llmDirect LLM call with prompt + model configCustom classifier prompt

Nodes

Each node is a platform tool registered in PlatformToolRegistry. The business logic is preserved in the node implementation files — only the graph wiring is replaced by YAML.

RAG Pipeline Nodes

NodeTool BindingWhat it does
extract_queryrouter_extract_queryReads latest HumanMessage, initialises 12 default state keys
detect_intentrouter_detect_intentLLM intent classification with taxonomy enrichment
retrieverouter_retrieveFull RAG pipeline — vector search + reranking + graph facts
groundrouter_groundGoogle Search grounding with context assembly
generaterouter_generateMulti-source prompt assembly with citation formatting
reflectrouter_reflectSelf-evaluation, quality scoring, retry decision
suggestrouter_suggestSearch suggestion generation
no_resultsrouter_no_resultsEmpathetic no-results response

SQL Analytics Nodes

NodeTool BindingWhat it does
planrouter_sql_planGenerates SQL from natural language question
execute_sqlrouter_sql_executeExecutes SQL with timeout + row limits
verifyrouter_sql_verifyValidates results for correctness
visualizerouter_sql_visualizeFormats output (table, chart, markdown)

Universal Content Tools

These tools are domain-agnostic — any project can compose them:

Tool BindingCapability
router_classifyTaxonomy / intent classification against a schema
router_generate_contentStructured content generation from prompt + context
router_review_contentQuality review + correction pass
router_filter_contentContent filtering / validation against criteria
router_plan_contentEditorial / batch planning from item set
router_match_semanticSemantic similarity matching + reranking

Intent Detection

The detect_intent node uses a fast LLM to classify the user’s query and route to the appropriate pipeline path:

IntentDescriptionRoute
rag_and_webQuestions requiring knowledge retrievalretrievegenerate
sql_analyticsData questions answerable via SQLplanexecute_sql
translateTranslation requestsgenerate (direct)
summarizeSummarisation requestsgenerate (direct)
identityQuestions about the assistant itselfgenerate (direct)

The node also performs taxonomy enrichment — matching the user query against a canonical_map to detect concepts and strengthen retrieval queries.

Domain Templates

Gardener (Product Normalisation)

__start__ → fetch_products → deterministic_pass → classify → write_results → __end__

Uses router_classify for LLM taxonomy classification. Domain-specific logic (deterministic pass) runs as a federated tool on the consumer side.

Enricher (Product Enrichment)

__start__ → prepare → enrich → review → __end__

Uses router_generate_content for LLM content generation and router_review_content for quality review.

News Pipeline

__start__ → harvest → filter → plan → generate → store → __end__

Uses router_filter_content, router_plan_content, and router_generate_content for the LLM processing stages.

Debugging

Enable pipeline debug logging:

Terminal window
export DEBUG_PIPELINE=1 # Per-node structured logs
export DEBUG_WEB_SEARCH=1 # Raw CSE result previews

Output format:

PIPELINE extract_query | user_query="How does RAG work?"
PIPELINE detect_intent.out | intent=rag_and_web taxonomy_concepts=["RAG"]
PIPELINE retrieve.out | docs=5 books=3 videos=1 qa=1 web=0
PIPELINE generate.out | assistant_chars=450 web_sources=0

Further Reading