# kartik-707/semantic-cache-gateway architecture

> Architecture diagram of the GitHub repository kartik-707/semantic-cache-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kartik-707/semantic-cache-gateway
- Repository: https://github.com/kartik-707/semantic-cache-gateway

## Overview

Semantic Cache Gateway is a policy-governed FastAPI front door for compatible chat APIs. A client request is validated, matched to a feature policy, and checked for cache eligibility and compatibility. Eligible requests try Redis exact lookup, then local embeddings and Qdrant semantic search; the decision logic either serves a cached response or lets the gateway call the provider. Successful eligible responses can be written to both stores. The README also documents cache seeding and Prometheus metrics. Some links are based on README descriptions or SOURCE INDEX references because the relevant implementation excerpts are incomplete.

Main entry points: [`gateway/proxy.py`](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/proxy.py) (Chat Gateway), [`gateway/cache_store.py`](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/cache_store.py) (Exact Cache), [`gateway/vector_store.py`](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/vector_store.py) (Vector Store), [`gateway/cache_rules.py`](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/cache_rules.py) (Compatibility Rules)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Gateway API"]
  node_proxy["Chat Gateway<br/>[proxy.py]"]
  node_adapter["Provider Adapter<br/>[adapter.py]"]
  node_seed["Cache Seeding<br/>[proxy.py]"]
  node_metrics["Metrics Endpoint<br/>[metrics.py]"]
end

subgraph group_policy["Policy and Contracts"]
  node_policy_loader["Policy Loader<br/>[policy_loader.py]"]
  node_policy_files["Feature Policies"]
  node_policy_contract["Policy Contract<br/>[policy.py]"]
  node_request_contract["Chat Contracts<br/>[proxy.py]"]
  node_cache_contract["Cache Entry<br/>[cache.py]"]
  node_decision_contract["Decision Metadata<br/>[decision.py]"]
  node_near_miss["Near-Miss Record<br/>[near_miss.py]"]
end

subgraph group_cache["Cache Runtime"]
  node_cache_rules["Compatibility Rules<br/>[cache_rules.py]"]
  node_exact_store[("Exact Cache<br/>[cache_store.py]")]
  node_vector_store[("Vector Store<br/>[vector_store.py]")]
end

subgraph group_intelligence["Semantic Matching"]
  node_embeddings["Local Embeddings<br/>[embeddings.py]"]
  node_similarity["Similarity Decisions<br/>[similarity.py]"]
end

node_client(("Chat Client"))
node_provider["Chat Provider"]
node_prometheus["Prometheus"]
node_redis[("Redis")]
node_qdrant[("Qdrant")]

node_client -->|"submits chat"| node_proxy
node_proxy -->|"validates"| node_request_contract
node_proxy -->|"loads policy"| node_policy_loader
node_policy_loader -->|"reads YAML"| node_policy_files
node_policy_loader -->|"validates against"| node_policy_contract
node_proxy -->|"builds context"| node_cache_rules
node_cache_rules -->|"uses policy"| node_policy_contract
node_proxy -->|"looks up exact"| node_exact_store
node_exact_store -->|"reads and writes"| node_redis
node_exact_store -->|"uses context"| node_cache_rules
node_proxy -->|"embeds prompt"| node_embeddings
node_proxy -->|"searches candidates"| node_vector_store
node_vector_store -->|"queries and stores"| node_qdrant
node_vector_store -->|"filters by context"| node_cache_rules
node_proxy -->|"decides semantic hit"| node_similarity
node_similarity -->|"produces decision"| node_decision_contract
node_proxy -->|"returns cache metadata"| node_decision_contract
node_proxy -->|"forwards misses"| node_adapter
node_adapter -->|"calls API"| node_provider
node_proxy -->|"stores eligible result"| node_vector_store
node_proxy -->|"stores eligible result"| node_exact_store
node_proxy -->|"creates entries"| node_cache_contract
node_seed -->|"embeds seeded prompt"| node_embeddings
node_seed -->|"stores seeded answer"| node_exact_store
node_seed -->|"stores seeded answer"| node_vector_store
node_proxy -->|"exposes metrics"| node_metrics
node_metrics -->|"exports counters"| node_prometheus

click node_proxy "https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/proxy.py"
click node_adapter "https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/adapter.py"
click node_seed "https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/proxy.py"
click node_metrics "https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/metrics.py"
click node_policy_loader "https://github.com/kartik-707/semantic-cache-gateway/blob/main/intelligence/policy_loader.py"
click node_policy_files "https://github.com/kartik-707/semantic-cache-gateway/tree/main/policies"
click node_policy_contract "https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/policy.py"
click node_request_contract "https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/proxy.py"
click node_cache_contract "https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/cache.py"
click node_decision_contract "https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/decision.py"
click node_near_miss "https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/near_miss.py"
click node_cache_rules "https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/cache_rules.py"
click node_exact_store "https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/cache_store.py"
click node_vector_store "https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/vector_store.py"
click node_embeddings "https://github.com/kartik-707/semantic-cache-gateway/blob/main/intelligence/embeddings.py"
click node_similarity "https://github.com/kartik-707/semantic-cache-gateway/blob/main/intelligence/similarity.py"

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_proxy,node_adapter,node_seed,node_metrics,node_client toneBlue
class node_policy_loader,node_policy_files,node_policy_contract,node_request_contract,node_cache_contract,node_decision_contract,node_near_miss,node_redis,node_qdrant toneAmber
class node_cache_rules,node_exact_store,node_vector_store toneMint
class node_embeddings,node_similarity toneRose
class node_provider,node_prometheus toneIndigo
```

## Components

### Gateway API

- [Chat Gateway](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/proxy.py): `gateway/proxy.py`
- [Provider Adapter](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/adapter.py): `gateway/adapter.py`
- [Cache Seeding](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/proxy.py): `gateway/proxy.py`
- [Metrics Endpoint](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/metrics.py): `gateway/metrics.py`

### Policy and Contracts

- [Policy Loader](https://github.com/kartik-707/semantic-cache-gateway/blob/main/intelligence/policy_loader.py): `intelligence/policy_loader.py`
- [Feature Policies](https://github.com/kartik-707/semantic-cache-gateway/tree/main/policies): `policies`
- [Policy Contract](https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/policy.py): `contracts/policy.py`
- [Chat Contracts](https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/proxy.py): `contracts/proxy.py`
- [Cache Entry](https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/cache.py): `contracts/cache.py`
- [Decision Metadata](https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/decision.py): `contracts/decision.py`
- [Near-Miss Record](https://github.com/kartik-707/semantic-cache-gateway/blob/main/contracts/near_miss.py): `contracts/near_miss.py`

### Cache Runtime

- [Compatibility Rules](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/cache_rules.py): `gateway/cache_rules.py`
- [Exact Cache](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/cache_store.py): `gateway/cache_store.py`
- [Vector Store](https://github.com/kartik-707/semantic-cache-gateway/blob/main/gateway/vector_store.py): `gateway/vector_store.py`

### Semantic Matching

- [Local Embeddings](https://github.com/kartik-707/semantic-cache-gateway/blob/main/intelligence/embeddings.py): `intelligence/embeddings.py`
- [Similarity Decisions](https://github.com/kartik-707/semantic-cache-gateway/blob/main/intelligence/similarity.py): `intelligence/similarity.py`

### Other components

- **Chat Client**
- **Chat Provider**
- **Prometheus**
- **Redis**
- **Qdrant**

## Connections

- Chat Client → Chat Gateway: submits chat (evidence: `README.md`)
- Chat Gateway → Chat Contracts: validates (evidence: `gateway/proxy.py`)
- Chat Gateway → Policy Loader: loads policy (evidence: `gateway/proxy.py`)
- Policy Loader → Feature Policies: reads YAML (evidence: `intelligence/policy_loader.py`)
- Policy Loader → Policy Contract: validates against (evidence: `intelligence/policy_loader.py`)
- Chat Gateway → Compatibility Rules: builds context (evidence: `gateway/proxy.py`)
- Compatibility Rules → Policy Contract: uses policy (evidence: `gateway/cache_rules.py`)
- Chat Gateway → Exact Cache: looks up exact (evidence: `gateway/proxy.py`)
- Exact Cache → Redis: reads and writes (evidence: `README.md`)
- Exact Cache → Compatibility Rules: uses context (evidence: `gateway/cache_store.py`)
- Chat Gateway → Local Embeddings: embeds prompt (evidence: `gateway/proxy.py`)
- Chat Gateway → Vector Store: searches candidates (evidence: `gateway/proxy.py`)
- Vector Store → Qdrant: queries and stores (evidence: `README.md`)
- Vector Store → Compatibility Rules: filters by context (evidence: `gateway/vector_store.py`)
- Chat Gateway → Similarity Decisions: decides semantic hit (evidence: `gateway/proxy.py`)
- Similarity Decisions → Decision Metadata: produces decision (evidence: `intelligence/similarity.py`)
- Chat Gateway → Decision Metadata: returns cache metadata (evidence: `gateway/proxy.py`)
- Chat Gateway → Provider Adapter: forwards misses (evidence: `gateway/proxy.py`)
- Provider Adapter → Chat Provider: calls API (evidence: `README.md`)
- Chat Gateway → Vector Store: stores eligible result (evidence: `gateway/proxy.py`)
- Chat Gateway → Exact Cache: stores eligible result (evidence: `gateway/proxy.py`)
- Chat Gateway → Cache Entry: creates entries (evidence: `gateway/proxy.py`)
- Cache Seeding → Local Embeddings: embeds seeded prompt (evidence: `gateway/proxy.py`)
- Cache Seeding → Exact Cache: stores seeded answer (evidence: `gateway/proxy.py`)
- Cache Seeding → Vector Store: stores seeded answer (evidence: `gateway/proxy.py`)
- Chat Gateway → Metrics Endpoint: exposes metrics (evidence: `gateway/proxy.py`)
- Metrics Endpoint → Prometheus: exports counters (evidence: `README.md`)
