Loading...
Loading...
Context layer for AI data agents - teach Claude Code, Codex, and AI agents to query data warehouses accurately with semantic layer, wiki knowledge, and MCP tools
npx skill4agent add aradotso/ai-agent-skills ktx-ai-data-agents-contextSkill by ara.so — AI Agent Skills collection.
npm install -g @kaelio/ktxnpm install --save-dev @kaelio/ktxpnpm add -D @kaelio/ktxktx setupktx statusktx project: /home/user/analytics
Project ready: yes
LLM ready: yes (claude-sonnet-4-6)
Embeddings ready: yes (text-embedding-3-small)
Databases configured: yes (warehouse)
Context sources configured: yes (dbt_main)
ktx context built: yes
Agent integration ready: yes (codex:project)my-project/
├── ktx.yaml # Project configuration
├── semantic-layer/<connection-id>/ # YAML semantic sources
├── wiki/global/ # Shared business context
├── wiki/user/<user-id>/ # User-scoped notes
├── raw-sources/<connection-id>/ # Ingest artifacts and reports
└── .ktx/ # Local state and secrets (git-ignored)version: 1
project_id: analytics-warehouse
llm:
provider: anthropic
model: claude-sonnet-4-6
embeddings:
provider: openai
model: text-embedding-3-small
databases:
warehouse:
type: postgres
host: localhost
port: 5432
database: analytics
user: readonly_user
# Password stored in .ktx/secrets.yaml or env var
context_sources:
dbt_main:
type: dbt
project_dir: ./dbt
profiles_dir: ~/.dbt
notion_docs:
type: notion
# Token stored in .ktx/secrets.yaml or env var NOTION_TOKEN# LLM configuration
export ANTHROPIC_API_KEY=your_key_here
export OPENAI_API_KEY=your_key_here
# Database credentials
export DB_PASSWORD=your_db_password
# Context sources
export NOTION_TOKEN=your_notion_token
export DBT_PROFILES_DIR=~/.dbt
# Project resolution
export KTX_PROJECT_DIR=/path/to/project# Build context from all configured sources
ktx ingest
# Build from specific connection
ktx ingest --connection warehouse
# Rebuild everything from scratch
ktx ingest --rebuild# Search semantic layer (metrics, dimensions, sources)
ktx sl "revenue"
ktx sl "customer lifetime value"
# Search wiki content
ktx wiki "refund policy"
ktx wiki "revenue recognition rules"
# Combined search
ktx search "monthly recurring revenue"# Start MCP server for agent integration
ktx mcp start
# Start with custom project directory
ktx mcp start --project-dir /path/to/project
# List available MCP tools
ktx mcp tools# Validate semantic layer YAML
ktx sl validate
# List all semantic sources
ktx sl list
# Show specific metric details
ktx sl show monthly_revenuedatabases:
warehouse:
type: postgres
host: localhost
port: 5432
database: analytics
user: readonly_user
ssl: truedatabases:
snowflake_prod:
type: snowflake
account: xy12345.us-east-1
warehouse: COMPUTE_WH
database: ANALYTICS
schema: PUBLIC
user: readonly_user
role: ANALYSTdatabases:
bigquery_prod:
type: bigquery
project_id: my-gcp-project
dataset: analytics
credentials_file: /path/to/service-account.jsondatabases:
clickhouse:
type: clickhouse
host: localhost
port: 9000
database: analytics
user: readonlycontext_sources:
dbt_main:
type: dbt
project_dir: ./dbt
profiles_dir: ~/.dbt
target: prodcontext_sources:
looker:
type: lookml
project_dir: ./looker-project
models:
- sales
- marketingcontext_sources:
metabase:
type: metabase
url: https://metabase.company.com
# Token in .ktx/secrets.yaml or METABASE_TOKEN env var
collections:
- Sales Dashboards
- Marketing Analyticscontext_sources:
notion_wiki:
type: notion
# Token in .ktx/secrets.yaml or NOTION_TOKEN env var
page_ids:
- abc123def456
- ghi789jkl012# semantic-layer/warehouse/metrics/revenue.yaml
type: metric
name: monthly_revenue
description: Total revenue aggregated by month
sql: |
SELECT
DATE_TRUNC('month', order_date) as month,
SUM(amount) as revenue
FROM orders
WHERE status = 'completed'
GROUP BY 1
dimensions:
- month
measures:
- revenue
tags:
- finance
- reporting# semantic-layer/warehouse/dimensions/customer_segment.yaml
type: dimension
name: customer_segment
description: Customer segmentation based on lifetime value
sql: |
CASE
WHEN lifetime_value >= 10000 THEN 'Enterprise'
WHEN lifetime_value >= 1000 THEN 'Mid-Market'
ELSE 'SMB'
END
source_table: customers# semantic-layer/warehouse/sources/orders.yaml
type: source
name: orders
description: All customer orders
table: public.orders
primary_key: order_id
joins:
- to: customers
type: many_to_one
on: orders.customer_id = customers.customer_id
columns:
- name: order_id
type: integer
description: Unique order identifier
- name: customer_id
type: integer
description: Foreign key to customers
- name: amount
type: decimal
description: Order total in USD
- name: order_date
type: timestamp
description: When order was placedktx setupktx_search_semantic_layerktx_search_wikiktx_get_metricktx_query_previewUse ktx to find our revenue metrics
What wiki pages discuss refund policies?
Show me the SQL for monthly_recurring_revenue metric# Install ktx skill
npx skills add Kaelio/ktx --skill ktx
# Then in your project
ktx setupQuery our warehouse for Q1 2024 revenue by customer segment
Find documentation about our churn calculationktx mcp start# 1. Install ktx
npm install -g @kaelio/ktx
# 2. Navigate to your project
cd /path/to/analytics-project
# 3. Run interactive setup
ktx setup
# 4. Verify configuration
ktx status
# 5. Build initial context
ktx ingest
# 6. Test search
ktx sl "revenue"
ktx wiki "metric definitions"# Rebuild context for specific connection
ktx ingest --connection warehouse --rebuild
# Or rebuild everything
ktx ingest --rebuild# 1. Create metric YAML file
cat > semantic-layer/warehouse/metrics/arr.yaml <<EOF
type: metric
name: annual_recurring_revenue
description: ARR calculated from active subscriptions
sql: |
SELECT
DATE_TRUNC('year', subscription_start) as year,
SUM(monthly_amount * 12) as arr
FROM subscriptions
WHERE status = 'active'
GROUP BY 1
dimensions:
- year
measures:
- arr
tags:
- finance
- saas
EOF
# 2. Validate
ktx sl validate
# 3. Rebuild context
ktx ingest --connection warehouse
# 4. Test search
ktx sl "annual recurring revenue"# Create a wiki page
mkdir -p wiki/global/metrics
cat > wiki/global/metrics/revenue-recognition.md <<EOF
# Revenue Recognition
## Overview
Our revenue recognition follows ASC 606 guidelines.
## Key Rules
1. Revenue is recognized when service is delivered
2. Refunds are deducted in the month issued
3. Deferred revenue is amortized monthly
## Related Metrics
- monthly_revenue
- deferred_revenue
- recognized_revenue
EOF
# Rebuild wiki index
ktx ingest
# Search it
ktx wiki "revenue recognition"ktx setup
# Follow prompts to configure Anthropic, OpenAI, or Vertex AIexport ANTHROPIC_API_KEY=your_key_here
ktx status# Check credentials in ktx.yaml or .ktx/secrets.yaml
ktx ingest --connection warehouse --verbose# Validate specific file
ktx sl validate semantic-layer/warehouse/metrics/revenue.yaml
# Common issues:
# - Invalid YAML syntax
# - Missing required fields (name, type, sql)
# - Referenced tables don't exist
# - Invalid join definitions# Check if project is properly initialized
ktx status
# Ensure context is built
ktx ingest
# Start with verbose logging
ktx mcp start --verbose
# Check for port conflicts (default: 3000)
ktx mcp start --port 3001# Rebuild search indexes
ktx ingest --rebuild
# Check if context sources are configured
cat ktx.yaml
# Verify semantic layer files exist
ls -la semantic-layer/
# Verify wiki files exist
ls -la wiki/global/ktx statusktx mcp start --verbosesemantic-layer/
warehouse/
metrics/
finance/
revenue.yaml
arr.yaml
product/
dau.yaml
retention.yaml
dimensions/
time.yaml
geography.yaml
sources/
orders.yaml
customers.yaml# semantic-layer/warehouse/metrics/revenue.yaml
type: metric
name: monthly_revenue
description: See wiki/global/metrics/revenue-recognition.md for full details
# ... rest of definitiontype: metric
name: customer_churn_rate
tags:
- saas
- retention
- executive-dashboard
- monthly-reporting# .gitignore
.ktx/secrets.yaml
.ktx/cache/
.ktx/*.dbktx.yamlsemantic-layer/wiki/global/-- PostgreSQL example
CREATE USER ktx_readonly WITH PASSWORD 'secure_password';
GRANT CONNECT ON DATABASE analytics TO ktx_readonly;
GRANT USAGE ON SCHEMA public TO ktx_readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ktx_readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO ktx_readonly;#!/bin/bash
# daily-context-update.sh
cd /path/to/project
ktx ingest --connection warehouse
ktx sl validatellm:
provider: vertex
model: claude-3-5-sonnet-v2@20250219
project_id: my-gcp-project
region: us-central1
temperature: 0.1
max_tokens: 4096databases:
prod_warehouse:
type: snowflake
account: prod.us-east-1
database: ANALYTICS_PROD
staging_warehouse:
type: snowflake
account: staging.us-east-1
database: ANALYTICS_STAGING
postgres_app:
type: postgres
host: app-db.internal
database: applicationimport { KtxContext } from '@kaelio/ktx';
const ctx = await KtxContext.load('/path/to/project');
// Search semantic layer
const results = await ctx.searchSemanticLayer('revenue');
console.log(results);
// Get metric definition
const metric = await ctx.getMetric('monthly_revenue');
console.log(metric.sql);
// Search wiki
const wikiResults = await ctx.searchWiki('refund policy');
console.log(wikiResults);