Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ lineagelens ontology [path] measured capability for this installation
lineagelens mcp [path] run the MCP server over stdio
lineagelens serve [path] HTTP surface for the dashboard (loopback default)
lineagelens report [path] local SQLite token savings & MCP ROI reporter
lineagelens gateway ... ContextServe Hybrid Token Gateway & Laya System 1/2
lineagelens telemetry ... enable, disable, status -- off until you enable it
lineagelens auth ... login, logout, status, token, switch-env
```
Expand Down Expand Up @@ -516,6 +517,53 @@ lineagelens index --upload --upload-required

---

## ⚡ Hybrid AI Token Gateway (LiteLLM + Laya)

LineageLens includes a 100% self-hosted **Hybrid Token Gateway** that couples the **LiteLLM Proxy** with the **Laya Decision Engine** (`NandhaKishorM/laya`). It splits high-frequency decision primitives (System 1) from generative reasoning (System 2) — saving 40%–60% of agent token costs with sub-20ms latency.

### System 1 vs. System 2 Routing

| Tier | Engine / Model | Latency | Cost / 1M Tokens | Role & Primitives |
|---|---|---|---|---|
| **System 1** | Laya Decision Engine (`laya-decision`, `laya-relevance-scorer`) | **< 20ms** | **$0.05** | AST relevance filtering, binary gating, continuation checks, error classification |
| **System 2** | Frontier LLMs (Claude 3.5 Sonnet, GPT-4o, DeepSeek) | 400ms – 1500ms | $2.50 – $15.00 | Deep architectural planning, code generation, complex refactoring |

### Docker Compose Stack

Start the gateway proxy (:4000) and Laya decision service (:8000):

```bash
docker compose -f docker/docker-compose.gateway.yml up -d
```

### Gateway CLI Commands

```bash
# Verify gateway connectivity & engine status
lineagelens gateway status

# Benchmark sub-20ms System 1 decision & relevance scoring latency
lineagelens gateway test --prompt "Is order payment service relevant to payment processing?"

# Issue a virtual API key with a hard budget ceiling ($25.00 USD)
lineagelens gateway keys --create "ProductionAgent" --tenant "org_engineering" --budget 25.00

# List virtual keys and real-time spend
lineagelens gateway keys --list

# View double-entry token ledger summary and cost savings
lineagelens gateway report
```

### Double-Entry Token Ledger & Hard Quotas

- **Tenant Isolation & Prepaid Balances**: Enforced atomically via SQLite/PostgreSQL (`.lineagelens/ledger.sqlite`).
- **Virtual API Keys (`sk-cs-...`)**: SHA-256 hashed keys with custom budget caps (`max_budget_usd`), rate limits, and model restriction lists.
- **Fail-Closed Quota Cutoffs**: When a budget ceiling is reached, the gateway immediately returns `402 Payment Required`.
- **Local Heuristics Fallback**: If the gateway is offline or unreachable, LineageLens automatically falls back to local single-pass compiler heuristics.

---

## 🐳 Docker Sandbox & Distribution

The `Dockerfile` uses a multi-stage architecture providing isolated environments with clean APT caches:
Expand Down
44 changes: 44 additions & 0 deletions docker/docker-compose.gateway.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3.8'

services:
# -------------------------------------------------------------
# LAYA SYSTEM 1 DECISION ENGINE
# -------------------------------------------------------------
laya-service:
build:
context: ../services/laya-service
dockerfile: Dockerfile
container_name: contextserve-laya
restart: unless-stopped
ports:
- "8000:8000"
environment:
- TORCH_BACKEND=auto
- WORKERS=4
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/healthz')"]
interval: 10s
timeout: 5s
retries: 3

# -------------------------------------------------------------
# LITELLM GATEWAY & PROXY
# -------------------------------------------------------------
litellm-proxy:
image: ghcr.io/berriai/litellm:main-latest
container_name: contextserve-gateway
restart: unless-stopped
ports:
- "4000:4000"
volumes:
- ./litellm/config.yaml:/app/config.yaml:ro
- ../data/gateway:/data
environment:
- LITELLM_MASTER_KEY=${LITELLM_MASTER_KEY:-sk-cs-master-secret}
- DATABASE_URL=sqlite:////data/ledger.sqlite
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
- OPENAI_API_KEY=${OPENAI_API_KEY:-}
depends_on:
laya-service:
condition: service_healthy
command: ["--config", "/app/config.yaml", "--port", "4000", "--num_workers", "8"]
65 changes: 65 additions & 0 deletions docker/litellm/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
model_list:
# ========================================================
# SYSTEM 1: LAYA ULTRA-FAST DECISION & SCORING ENGINES
# ========================================================
- model_name: laya-decision
litellm_params:
model: openai/laya-decision
api_base: http://laya-service:8000/v1
api_key: "internal-laya-secret"
input_cost_per_token: 0.00000005 # $0.05 per 1M input tokens
output_cost_per_token: 0.00000005 # $0.05 per 1M output tokens

- model_name: laya-relevance-scorer
litellm_params:
model: openai/laya-relevance-scorer
api_base: http://laya-service:8000/v1
api_key: "internal-laya-secret"
input_cost_per_token: 0.00000005
output_cost_per_token: 0.00000005

# ========================================================
# SYSTEM 2: FRONTIER REASONING (DIRECT PROVIDER PRICING)
# ========================================================
- model_name: claude-3-5-sonnet
litellm_params:
model: anthropic/claude-3-5-sonnet-20241022
api_key: os.environ/ANTHROPIC_API_KEY
input_cost_per_token: 0.000003 # $3.00 per 1M input tokens
output_cost_per_token: 0.000015 # $15.00 per 1M output tokens

- model_name: gpt-4o
litellm_params:
model: openai/gpt-4o
api_key: os.environ/OPENAI_API_KEY
input_cost_per_token: 0.0000025 # $2.50 per 1M input tokens
output_cost_per_token: 0.0000100 # $10.00 per 1M output tokens

# ========================================================
# OPTIONAL: ON-PREMISE vLLM REASONING (OPEN WEIGHTS)
# ========================================================
- model_name: deepseek-coder
litellm_params:
model: openai/deepseek-ai/DeepSeek-Coder-V2-Lite
api_base: http://vllm-service:8000/v1
api_key: "vllm-local-key"
input_cost_per_token: 0.0000002
output_cost_per_token: 0.0000002

litellm_settings:
drop_params: true
telemetry: false
request_timeout: 45

general_settings:
master_key: os.environ/LITELLM_MASTER_KEY
database_url: os.environ/DATABASE_URL
store_model_in_db: true
alerting: ["slack", "email"]

router_settings:
routing_strategy: "latency-based-routing"
enable_pre_call_checks: true
num_retries: 2
fallbacks:
- laya-decision: ["claude-3-5-sonnet"]
Loading
Loading