Loading...
Loading...
Configure and use ktx to build an executable context layer for AI agents querying data warehouses with semantic layers, wiki knowledge, and approved metrics
npx skill4agent add aradotso/ai-agent-skills ktx-ai-data-context-layerSkill by ara.so — AI Agent Skills collection.
npm install -g @kaelio/ktxnpm install --save-dev @kaelio/ktx
npx ktx setupktx --version
ktx statusktx setupktx.yamlmy-analytics-project/
├── ktx.yaml # Main configuration (commit this)
├── semantic-layer/warehouse/ # Generated semantic sources (commit)
├── wiki/global/ # Shared business context (commit)
├── wiki/user/alice/ # User-scoped notes (commit)
├── raw-sources/warehouse/ # Ingest artifacts (commit)
└── .ktx/ # Local state and secrets (git-ignore)ktx.yamlversion: 1
project:
name: my-analytics
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
schema: public
# Credentials from env vars or .ktx/secrets.yaml
context_sources:
dbt_main:
type: dbt
project_dir: ../dbt-project
profiles_dir: ~/.dbt
target: prod.ktx/secrets.yamldatabases:
warehouse:
user: ${POSTGRES_USER}
password: ${POSTGRES_PASSWORD}
llm:
anthropic_api_key: ${ANTHROPIC_API_KEY}
embeddings:
openai_api_key: ${OPENAI_API_KEY}ktx 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, looker_main)
ktx context built: yes
Agent integration ready: yes (codex:project)ktx ingestktx ingest --connection warehousektx ingest --source dbt_mainktx ingest --force# Search for metrics and dimensions
ktx sl "revenue"
ktx sl "customer lifetime value"
# JSON output for scripting
ktx sl "churn rate" --json# Search business knowledge
ktx wiki "refund policy"
ktx wiki "how to calculate mrr"
# JSON output
ktx wiki "data retention" --json# Auto-detect project and start MCP server
ktx mcp start
# Specify project directory
ktx mcp start --project-dir /path/to/project
# Custom port
ktx mcp start --port 3000llm:
provider: anthropic
model: claude-sonnet-4-6
api_key: ${ANTHROPIC_API_KEY} # In .ktx/secrets.yamlllm:
provider: vertex
model: claude-sonnet-4-6@20250514
project_id: my-gcp-project
region: us-central1
# Uses Application Default Credentialsllm:
provider: ai-gateway
model: claude-sonnet-4-6
base_url: https://gateway.example.com/v1
api_key: ${GATEWAY_API_KEY}llm:
provider: claude-code
# Uses local Claude Code session via SDKdatabases:
analytics:
type: postgres
host: localhost
port: 5432
database: analytics
schema: public
user: ${PG_USER}
password: ${PG_PASSWORD}databases:
warehouse:
type: snowflake
account: xy12345.us-east-1
warehouse: COMPUTE_WH
database: ANALYTICS
schema: PUBLIC
user: ${SNOWFLAKE_USER}
password: ${SNOWFLAKE_PASSWORD}
role: ANALYSTdatabases:
bigquery:
type: bigquery
project_id: my-gcp-project
dataset: analytics
credentials_path: ${GOOGLE_APPLICATION_CREDENTIALS}databases:
events:
type: clickhouse
host: localhost
port: 8123
database: analytics
user: ${CLICKHOUSE_USER}
password: ${CLICKHOUSE_PASSWORD}context_sources:
dbt_prod:
type: dbt
project_dir: ../dbt-project
profiles_dir: ~/.dbt
target: prod
include_tests: true
include_docs: truecontext_sources:
looker_main:
type: looker
base_url: https://company.looker.com
client_id: ${LOOKER_CLIENT_ID}
client_secret: ${LOOKER_CLIENT_SECRET}
project: analyticscontext_sources:
metabase:
type: metabase
base_url: https://metabase.company.com
username: ${METABASE_USER}
password: ${METABASE_PASSWORD}
database_id: 1context_sources:
notion_wiki:
type: notion
api_key: ${NOTION_API_KEY}
database_ids:
- abc123def456
- ghi789jkl012// Via CLI
$ ktx sl "monthly recurring revenue"{
"results": [
{
"type": "metric",
"name": "mrr",
"description": "Monthly Recurring Revenue",
"calculation": "SUM(subscription_amount)",
"dimensions": ["customer_id", "plan_type"],
"filters": ["status = 'active'"],
"source": "dbt_prod",
"score": 0.95
}
]
}# Install ktx semantic layer engine
# uv add ktx-sl
from ktx_sl import SemanticLayer, Query
# Load semantic layer from ktx project
sl = SemanticLayer.from_project("/path/to/ktx-project")
# Define query
query = Query(
metrics=["mrr", "customer_count"],
dimensions=["plan_type", "region"],
filters=[{"field": "signup_date", "op": ">=", "value": "2024-01-01"}],
order_by=[{"field": "mrr", "direction": "desc"}]
)
# Generate SQL
sql = sl.plan(query)
print(sql)semantic-layer/warehouse/subscriptions.yamlversion: 1
source: warehouse
table: public.subscriptions
metrics:
- name: mrr
description: Monthly Recurring Revenue
type: sum
sql: amount
filters:
- status = 'active'
- billing_period = 'monthly'
- name: customer_count
description: Distinct active customers
type: count_distinct
sql: customer_id
filters:
- status = 'active'
dimensions:
- name: customer_id
type: string
sql: customer_id
- name: plan_type
type: string
sql: plan_type
- name: region
type: string
sql: customer_region
joins:
- to: customers
type: left
on: subscriptions.customer_id = customers.idwiki/global/refund-policy.md---
title: Refund Policy
tags: [finance, customer-success]
---
# Refund Policy
Customers can request refunds within 30 days of purchase.
## Calculation Rules
- Full refund: within 7 days
- Prorated refund: 8-30 days
- No refund: after 30 days
## Impact on Metrics
Refunds reduce `net_revenue` but not `gross_revenue`.ktx wiki "refund policy"
ktx wiki "how to calculate churn"wiki/
├── global/ # Shared team knowledge
│ ├── metrics-glossary.md
│ ├── refund-policy.md
│ └── data-quality-rules.md
└── user/alice/ # User-scoped notes
└── analysis-notes.md# Auto-detect project
ktx mcp start
# Explicit project path
ktx mcp start --project-dir /path/to/analytics~/Library/Application Support/Claude/claude_desktop_config.json{
"mcpServers": {
"ktx-analytics": {
"command": "ktx",
"args": [
"mcp",
"start",
"--project-dir",
"/path/to/analytics"
]
}
}
}# Install ktx skill in Codex project
npx skills add Kaelio/ktx --skill ktx
# Or via natural language
# "Run npx skills add Kaelio/ktx --skill ktx and configure ktx for this project"ktx_search_semantic_layerktx_search_wikiktx_get_metric_definitionktx_list_connectionsktx_validate_query# 1. Install ktx
npm install -g @kaelio/ktx
# 2. Create project
cd /path/to/analytics
ktx setup
# 3. Configure via interactive prompts:
# - LLM: Anthropic API with claude-sonnet-4-6
# - Embeddings: OpenAI text-embedding-3-small
# - Database: PostgreSQL connection
# - Sources: dbt project
# 4. Verify setup
ktx status
# 5. Build context
ktx ingest
# 6. Test search
ktx sl "revenue"
ktx wiki "metric definitions"
# 7. Start MCP for agents
ktx mcp start# Add new dbt models
cd ../dbt-project
dbt run --models new_model
# Ingest updates into ktx
cd ../analytics
ktx ingest --source dbt_prod
# Verify new metrics are available
ktx sl "new_metric"databases:
production:
type: postgres
host: prod.db.company.com
database: analytics
schema: public
user: ${PROD_DB_USER}
password: ${PROD_DB_PASSWORD}
events:
type: clickhouse
host: events.company.com
database: analytics
user: ${CLICKHOUSE_USER}
password: ${CLICKHOUSE_PASSWORD}
context_sources:
dbt_prod:
type: dbt
project_dir: ../dbt-project
target: prod
connection: production
events_raw:
type: raw
connection: events# .github/workflows/ktx-ingest.yml
name: Update ktx Context
on:
push:
branches: [main]
paths:
- 'dbt-project/**'
- 'wiki/**'
jobs:
ingest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install ktx
run: npm install -g @kaelio/ktx
- name: Configure secrets
run: |
mkdir -p .ktx
echo "databases:" > .ktx/secrets.yaml
echo " warehouse:" >> .ktx/secrets.yaml
echo " user: ${{ secrets.DB_USER }}" >> .ktx/secrets.yaml
echo " password: ${{ secrets.DB_PASSWORD }}" >> .ktx/secrets.yaml
- name: Ingest context
run: ktx ingest --project-dir .
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- name: Commit updates
run: |
git config user.name "ktx-bot"
git config user.email "bot@company.com"
git add semantic-layer/ raw-sources/
git commit -m "Update ktx context" || exit 0
git pushktx setupktx.yamlllm:
provider: anthropic
model: claude-sonnet-4-6.ktx/secrets.yamlllm:
anthropic_api_key: ${ANTHROPIC_API_KEY}.ktx/secrets.yaml# PostgreSQL example
psql -h localhost -U ${POSTGRES_USER} -d analytics -c "SELECT 1"ktx ingest --forcektx statusls semantic-layer/ls wiki/global/ktx ingest --source dbt_prod --forcelsof -i :3000ktx.yamlktx mcp start --project-dir /full/path/to/projectprofiles_dirktx.yamltargetdbt compile# Verify dbt configuration
cd ../dbt-project
dbt debug --profiles-dir ~/.dbt --target prod
# Check ktx can read dbt
ktx ingest --source dbt_prod --log-level debugembeddings:
provider: voyage
model: voyage-3metrics:
- name: mrr
description: Monthly Recurring Revenue from active subscriptions
tags: [revenue, subscription, saas]
business_context: |
MRR is the primary metric for tracking subscription health.
Includes only active, monthly-billed customers.context_sources:
dbt_prod:
type: dbt
include_patterns:
- "marts/**"
- "metrics/**"
exclude_patterns:
- "staging/**"
- "tests/**"databases:
warehouse:
type: postgres
sampling:
max_tables: 500
rows_per_table: 10000# Only ingest changed sources
ktx ingest --incrementaldatabases:
warehouse:
type: postgres
sampling:
enabled: true
max_tables: 200
rows_per_table: 5000
include_patterns:
- "public.*"
- "analytics.*"
exclude_patterns:
- "*_tmp"
- "*_staging"project:
join_detection:
enabled: true
min_confidence: 0.85
max_cardinality_ratio: 0.1
require_foreign_keys: falsecontext_sources:
team_wiki:
type: git
repo_url: https://github.com/company/analytics-wiki
branch: main
path: docs/
auth_token: ${GITHUB_TOKEN}--project-dirKTX_PROJECT_DIRktx.yamlktx ingest --project-dir /path/to/project| Variable | Purpose |
|---|---|
| Default project directory |
| Anthropic API key |
| OpenAI API key |
| PostgreSQL username |
| PostgreSQL password |
| Snowflake username |
| Snowflake password |
| GCP service account key path |
| GitHub personal access token |
.env.ktx/secrets.yaml| Command | Purpose |
|---|---|
| Create/resume project, configure providers |
| Check project readiness |
| Build context from configured sources |
| Search semantic layer |
| Search wiki knowledge |
| Start MCP server for agents |
| Show current configuration |
| Update configuration |
| List database connections |
| List context sources |