Skip to main content

kt-rag

Knowledge-graph-powered RAG sidecar for infrastructure querying

Version CI MIT License PyPI


Turn your infrastructure graph into a searchable knowledge base. kt-rag feeds discovered infrastructure data — services, databases, Kubernetes clusters, DNS zones, CI/CD pipelines — into a LightRAG knowledge graph, then lets you ask questions in plain English.

What you can do:

  • "Which services depend on Postgres?" — get a structured answer with confidence scoring
  • "What's the blast radius if us-east-1 goes down?" — trace dependencies across providers
  • "Show me all EC2 instances tagged production in eu-west-1" — filter by metadata
  • "Generate a runbook for the auth service" — auto-document from live graph data
  • "What changed in my infrastructure since last week?" — incremental sync awareness

How It Works

┌─────────────┐    ┌──────────┐    ┌──────────┐    ┌──────┐
│ Discovery   │───▶│ kt-rag   │───▶│ LightRAG │───▶│ LLM  │
│ Plugins     │    │ seed/sync│    │ KG Store │    │      │
└─────────────┘    └──────────┘    └──────────┘    └──────┘
                        │                                
                   ┌────┴────┐                    ┌─────┴──────┐
                   │   API   │                    │  Query CLI  │
                   │  Server │                    │  / Library  │
                   └─────────┘                    └─────────────┘
  1. Plugins discover your infrastructure (AWS, GitHub, K8s, etc.)
  2. kt-rag seeds entities and relationships into LightRAG's graph store
  3. LightRAG indexes everything for hybrid search (graph + vector)
  4. Your question triggers retrieval-augmented generation through an LLM

Quick Start

Install

pip install knowledgetree-rag

Or from source:

git clone https://github.com/knowledgetree-dev/kt-rag.git
cd kt-rag
pip install -e .

Configure

Copy the template and set your LLM API key:

cp .env.example .env
# Edit .env with your LLM provider and API key

Minimal .env:

LLM_BINDING_API_KEY=sk-...

Seed the graph

Populate LightRAG from your infrastructure data:

kt-rag-seed

Query

kt-rag-query "which services depend on postgres?"

Example output:

Response: The following services depend on Postgres:
  - auth-service (production, us-east-1)
  - user-api (staging, eu-west-1)
  - analytics-backend (production, us-west-2)
  - docs-service (production, us-east-1)

Confidence: 0.82

Start the API server

kt-rag-server
# Listening on http://0.0.0.0:8085
curl -s http://localhost:8085/api/v1/rag/health

CLI Commands

Command Description
kt-rag-seed Full seed from Knowledge Tree into LightRAG
kt-rag-query "..." Natural-language query (supports --filter, --min-score, --profile)
kt-rag-server REST API on port 8085

API

Query

POST /api/v1/rag/query
{
  "question": "which services depend on postgres?",
  "mode": "hybrid",
  "filter": {"provider": "aws", "region": "us-east-1"},
  "profile_name": "claude"
}

Response:

{
  "response": "The following services depend on Postgres...",
  "confidence": 0.82,
  "metadata": {
    "mode": "hybrid",
    "profile": "claude",
    "tokens_used": 1247
  }
}

Profile Management

# List profiles
GET /api/v1/rag/profiles

# Create profile
POST /api/v1/rag/profiles
{
  "name": "claude",
  "provider": "anthropic",
  "api_key": "sk-ant-...",
  "model": "claude-sonnet-4-20250514"
}

# Delete profile
DELETE /api/v1/rag/profiles/{name}

Multi-LLM Profiles

Switch between LLM providers at query time:

# Named profiles (persisted)
kt-rag-query "what's my blast radius?" --profile claude

# Ad-hoc via headers (server mode)
curl -H "X-KT-LLM-Provider: anthropic" \
     -H "X-KT-LLM-Api-Key: sk-ant-..." \
     -d '{"question": "..."}' \
     http://localhost:8085/api/v1/rag/query

Configuration

All config via environment variables or .env:

Variable Default Description
LLM_BINDING openai LLM provider (openai, anthropic, ollama, azure)
LLM_BINDING_API_KEY — API key
LLM_BINDING_MODEL gpt-4o Model name
EMBEDDING_BINDING openai Embedding provider
EMBEDDING_BINDING_MODEL text-embedding-3-small Embedding model
LIGHTRAG_DIR ./output LightRAG working directory
LIGHTRAG_PORT 8085 API server port
KT_API_URL http://localhost:8080 Knowledge Tree API URL
KT_API_TOKEN — Knowledge Tree auth token
PG_URI postgresql://localhost:5432/knowledgetree Database (read-only seed)
SYNC_INTERVAL 300 Sync interval in seconds

Confidence Scoring

Every query response includes a 0.0–1.0 confidence score based on:

  • Retrieval quality (30%) — how well the query matched stored entities
  • Context coverage (30%) — how completely the retrieved data covers the question
  • Factual support (40%) — whether the LLM's answer is grounded in retrieved facts

Set a minimum threshold:

kt-rag-query "what repos are production?" --min-score 0.6

Contributing

PRs are welcome. For feature requests or major changes, open an issue first.

# Dev setup
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"

# Run tests
pytest tests/ -v --tb=short

# Type check
mypy .

Project Structure

├── seed.py              # Full graph seed
├── sync.py              # Incremental sync
├── query.py             # CLI query entry point
├── server.py            # FastAPI server
├── config.py            # Environment config loader
├── profile_store.py     # LLM provider profile persistence
├── scorer.py            # Confidence scoring
├── kt_client.py         # Knowledge Tree API client
├── tests/               # Test suite
└── .env.example         # Config template

License

MIT

Metadata

Release files for knowledgetree-rag 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for knowledgetree-rag 0.1.0
File Size Uploaded Provenance Actions
knowledgetree_rag-0.1.0.tar.gz 10.6 kB With provenance Details for knowledgetree_rag-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for knowledgetree-rag 0.1.0
File Interpreter ABI Platform Actions
knowledgetree_rag-0.1.0-py3-none-any.whl Python 3 none any Details for knowledgetree_rag-0.1.0-py3-none-any.whl

Total release size: 25.1 kB

Release files / knowledgetree_rag-0.1.0.tar.gz

Download URL knowledgetree_rag-0.1.0.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
62d97a7d0c3012eb40c35ba09975f6dc2ec5700a4fc91a70f7e61cad0679eaff
BLAKE2b-256 checksum
How to use checksums
98cfa9bcc034952178fb995f9e9cacd0fd13d96f8e0a31c28cd5def0e48b49b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 9, 2026.

Transparency log

Release files / knowledgetree_rag-0.1.0-py3-none-any.whl

Download URL knowledgetree_rag-0.1.0-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
95ad310aeddf0fe63591eee8671e86f8f2ed3f702607a29243b5e4c560fd431d
BLAKE2b-256 checksum
How to use checksums
e5737ce5d634a30f5dfee61335d18a327b868cda1475a0e49a479eef20da898b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 9, 2026.

Transparency log

Security for knowledgetree-rag 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for knowledgetree_rag-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page