# zenithfoundry/slm-gate architecture

> Architecture diagram of the GitHub repository zenithfoundry/slm-gate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zenithfoundry/slm-gate
- Repository: https://github.com/zenithfoundry/slm-gate

## Overview

slm-gate is a local preprocessing and routing layer for coding assistants. Its two entry points handle different traffic: MCP tool results are distilled, grounded, and clarified, while cloud-model requests may be answered locally or compressed before forwarding. The ledger records routing and savings for CLI metrics and dashboards. README capabilities are included, but unsampled modules are connected only where the README or sampled callers support the relationship.

Main entry points: [`src/mcp-gate/pipeline.ts`](https://github.com/zenithfoundry/slm-gate/blob/main/src/mcp-gate/pipeline.ts) (MCP conditioning), [`src/llm-gate/pipeline.ts`](https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/pipeline.ts) (LLM routing), [`src/ledger/index.ts`](https://github.com/zenithfoundry/slm-gate/blob/main/src/ledger/index.ts) (SQLite ledger), [`src/llm-gate/server.ts`](https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/server.ts) (LLM HTTP server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gates["Traffic gates"]
  node_mcpserver["MCP server<br/>[server.ts]"]
  node_mcppipeline["MCP conditioning<br/>[pipeline.ts]"]
  node_llmserver["LLM HTTP server<br/>[server.ts]"]
  node_llmpipeline["LLM routing<br/>[pipeline.ts]"]
  node_formats["Provider formats<br/>[contract.ts]"]
  node_forward["Cloud forwarding<br/>[forward.ts]"]
end

subgraph group_intelligence["Local intelligence"]
  node_distill["Tool-result distillation<br/>[elision.ts]"]
  node_ground["Repository grounding<br/>[ground.ts]"]
  node_resolver["Ambiguity resolver<br/>[index.ts]"]
  node_localanswer["Local-first answers<br/>[local-first.ts]"]
  node_compress["Context compression<br/>[compression.ts]"]
  node_models["SLM runtime<br/>[slm.ts]"]
  node_verifier["Answer verifier<br/>[index.ts]"]
end

subgraph group_state["Ledger and analytics"]
  node_ledger[("SQLite ledger<br/>[index.ts]")]
  node_cache[("Semantic cache<br/>[index.ts]")]
  node_pricing["Provider pricing<br/>[index.ts]"]
  node_metrics["CLI metrics<br/>[report.ts]"]
  node_dashboard["Savings dashboard<br/>[serve.ts]"]
end

subgraph group_operations["Setup and configuration"]
  node_cli["CLI<br/>[cli.ts]"]
  node_config["Configuration<br/>[config.ts]"]
  node_setup["Local model setup<br/>[startup.ts]"]
end

node_user(("Developer / client"))
node_downstream["Downstream MCP"]
node_cloud["Cloud model API"]
node_langfuse["Langfuse"]
node_ollama["Ollama"]

node_user -->|"sends MCP traffic"| node_mcpserver
node_mcpserver -->|"proxies tool calls"| node_downstream
node_downstream -->|"returns tool results"| node_mcpserver
node_mcpserver -->|"conditions results"| node_mcppipeline
node_mcppipeline -->|"distills results"| node_distill
node_mcppipeline -->|"scans repository"| node_ground
node_mcppipeline -->|"resolves ambiguity"| node_resolver
node_mcppipeline -->|"checks and stores"| node_cache
node_mcppipeline -->|"writes events"| node_ledger
node_resolver -->|"generates decisions"| node_models
node_user -->|"sends model requests"| node_llmserver
node_llmserver -->|"routes requests"| node_llmpipeline
node_llmpipeline -->|"tries local answer"| node_localanswer
node_localanswer -->|"queries local model"| node_models
node_llmpipeline -->|"compresses context"| node_compress
node_llmpipeline -->|"normalizes requests"| node_formats
node_llmserver -->|"forwards upstream"| node_forward
node_forward -->|"sends cloud request"| node_cloud
node_llmserver -->|"records routing"| node_ledger
node_llmpipeline -->|"checks and stores"| node_cache
node_llmpipeline -->|"calculates cost"| node_pricing
node_ledger -->|"supplies report data"| node_metrics
node_ledger -->|"supplies dashboard data"| node_dashboard
node_ledger -.->|"flushes traces"| node_langfuse
node_user -->|"runs commands"| node_cli
node_cli -->|"loads settings"| node_config
node_cli -->|"starts diagnostics"| node_setup
node_setup -->|"checks local models"| node_ollama
node_models -->|"runs inference"| node_ollama

click node_mcpserver "https://github.com/zenithfoundry/slm-gate/blob/main/src/mcp-gate/server.ts"
click node_mcppipeline "https://github.com/zenithfoundry/slm-gate/blob/main/src/mcp-gate/pipeline.ts"
click node_llmserver "https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/server.ts"
click node_llmpipeline "https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/pipeline.ts"
click node_formats "https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/formats/contract.ts"
click node_forward "https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/forward.ts"
click node_distill "https://github.com/zenithfoundry/slm-gate/blob/main/src/utils/elision.ts"
click node_ground "https://github.com/zenithfoundry/slm-gate/blob/main/src/mcp-gate/ground.ts"
click node_resolver "https://github.com/zenithfoundry/slm-gate/blob/main/src/resolver/index.ts"
click node_localanswer "https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/local-first.ts"
click node_compress "https://github.com/zenithfoundry/slm-gate/blob/main/src/utils/compression.ts"
click node_models "https://github.com/zenithfoundry/slm-gate/blob/main/src/models/slm.ts"
click node_verifier "https://github.com/zenithfoundry/slm-gate/blob/main/src/verifier/index.ts"
click node_ledger "https://github.com/zenithfoundry/slm-gate/blob/main/src/ledger/index.ts"
click node_cache "https://github.com/zenithfoundry/slm-gate/blob/main/src/cache/index.ts"
click node_pricing "https://github.com/zenithfoundry/slm-gate/blob/main/src/pricing/index.ts"
click node_metrics "https://github.com/zenithfoundry/slm-gate/blob/main/src/ledger/report.ts"
click node_dashboard "https://github.com/zenithfoundry/slm-gate/blob/main/src/dashboard/serve.ts"
click node_cli "https://github.com/zenithfoundry/slm-gate/blob/main/src/cli.ts"
click node_config "https://github.com/zenithfoundry/slm-gate/blob/main/src/config.ts"
click node_setup "https://github.com/zenithfoundry/slm-gate/blob/main/src/setup/startup.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mcpserver,node_mcppipeline,node_llmserver,node_llmpipeline,node_formats,node_forward,node_user toneBlue
class node_distill,node_ground,node_resolver,node_localanswer,node_compress,node_models,node_verifier toneAmber
class node_ledger,node_cache,node_pricing,node_metrics,node_dashboard,node_cloud toneMint
class node_cli,node_config,node_setup toneRose
class node_downstream,node_langfuse,node_ollama toneIndigo
```

## Components

### Traffic gates

- [MCP server](https://github.com/zenithfoundry/slm-gate/blob/main/src/mcp-gate/server.ts): `src/mcp-gate/server.ts`
- [MCP conditioning](https://github.com/zenithfoundry/slm-gate/blob/main/src/mcp-gate/pipeline.ts): `src/mcp-gate/pipeline.ts`
- [LLM HTTP server](https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/server.ts): `src/llm-gate/server.ts`
- [LLM routing](https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/pipeline.ts): `src/llm-gate/pipeline.ts`
- [Provider formats](https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/formats/contract.ts): `src/llm-gate/formats/contract.ts`
- [Cloud forwarding](https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/forward.ts): `src/llm-gate/forward.ts`

### Local intelligence

- [Tool-result distillation](https://github.com/zenithfoundry/slm-gate/blob/main/src/utils/elision.ts): `src/utils/elision.ts`
- [Repository grounding](https://github.com/zenithfoundry/slm-gate/blob/main/src/mcp-gate/ground.ts): `src/mcp-gate/ground.ts`
- [Ambiguity resolver](https://github.com/zenithfoundry/slm-gate/blob/main/src/resolver/index.ts): `src/resolver/index.ts`
- [Local-first answers](https://github.com/zenithfoundry/slm-gate/blob/main/src/llm-gate/local-first.ts): `src/llm-gate/local-first.ts`
- [Context compression](https://github.com/zenithfoundry/slm-gate/blob/main/src/utils/compression.ts): `src/utils/compression.ts`
- [SLM runtime](https://github.com/zenithfoundry/slm-gate/blob/main/src/models/slm.ts): `src/models/slm.ts`
- [Answer verifier](https://github.com/zenithfoundry/slm-gate/blob/main/src/verifier/index.ts): `src/verifier/index.ts`

### Ledger and analytics

- [SQLite ledger](https://github.com/zenithfoundry/slm-gate/blob/main/src/ledger/index.ts): `src/ledger/index.ts`
- [Semantic cache](https://github.com/zenithfoundry/slm-gate/blob/main/src/cache/index.ts): `src/cache/index.ts`
- [Provider pricing](https://github.com/zenithfoundry/slm-gate/blob/main/src/pricing/index.ts): `src/pricing/index.ts`
- [CLI metrics](https://github.com/zenithfoundry/slm-gate/blob/main/src/ledger/report.ts): `src/ledger/report.ts`
- [Savings dashboard](https://github.com/zenithfoundry/slm-gate/blob/main/src/dashboard/serve.ts): `src/dashboard/serve.ts`

### Setup and configuration

- [CLI](https://github.com/zenithfoundry/slm-gate/blob/main/src/cli.ts): `src/cli.ts`
- [Configuration](https://github.com/zenithfoundry/slm-gate/blob/main/src/config.ts): `src/config.ts`
- [Local model setup](https://github.com/zenithfoundry/slm-gate/blob/main/src/setup/startup.ts): `src/setup/startup.ts`

### Other components

- **Developer / client**
- **Downstream MCP**
- **Cloud model API**
- **Langfuse**
- **Ollama**

## Connections

- Developer / client → MCP server: sends MCP traffic
- MCP server → Downstream MCP: proxies tool calls
- Downstream MCP → MCP server: returns tool results
- MCP server → MCP conditioning: conditions results
- MCP conditioning → Tool-result distillation: distills results
- MCP conditioning → Repository grounding: scans repository
- MCP conditioning → Ambiguity resolver: resolves ambiguity
- MCP conditioning → Semantic cache: checks and stores
- MCP conditioning → SQLite ledger: writes events
- Ambiguity resolver → SLM runtime: generates decisions
- Developer / client → LLM HTTP server: sends model requests
- LLM HTTP server → LLM routing: routes requests
- LLM routing → Local-first answers: tries local answer
- Local-first answers → SLM runtime: queries local model
- LLM routing → Context compression: compresses context
- LLM routing → Provider formats: normalizes requests
- LLM HTTP server → Cloud forwarding: forwards upstream
- Cloud forwarding → Cloud model API: sends cloud request
- LLM HTTP server → SQLite ledger: records routing
- LLM routing → Semantic cache: checks and stores
- LLM routing → Provider pricing: calculates cost
- SQLite ledger → CLI metrics: supplies report data
- SQLite ledger → Savings dashboard: supplies dashboard data
- SQLite ledger → Langfuse: flushes traces
- Developer / client → CLI: runs commands
- CLI → Configuration: loads settings
- CLI → Local model setup: starts diagnostics
- Local model setup → Ollama: checks local models
- SLM runtime → Ollama: runs inference
