# compresr-ai/context-gateway architecture

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

- Interactive diagram: https://gitdiagram.com/compresr-ai/context-gateway
- Repository: https://github.com/compresr-ai/context-gateway

## Overview

Context Gateway is a local proxy between AI-agent clients and LLM APIs. Its main workflow accepts agent requests, applies configured context and tool optimizations, forwards requests to providers, and returns responses; preemptive summarization prepares compaction in the background. The repository also includes configuration, authentication, monitoring, and a browser dashboard. Several subsystem links are documented or visible in the source index, but unsampled implementations and dynamic wiring are not assumed.

Main entry points: [`internal/gateway/handler.go`](https://github.com/compresr-ai/context-gateway/blob/main/internal/gateway/handler.go) (HTTP gateway), [`internal/pipes/tool_output/tool_output.go`](https://github.com/compresr-ai/context-gateway/blob/main/internal/pipes/tool_output/tool_output.go) (Tool output pipes), [`internal/adapters`](https://github.com/compresr-ai/context-gateway/tree/main/internal/adapters) (Provider adapters), [`internal/preemptive/manager.go`](https://github.com/compresr-ai/context-gateway/blob/main/internal/preemptive/manager.go) (Preemptive compaction)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Gateway runtime"]
  node_gateway["HTTP gateway<br/>[handler.go]"]
  node_providers["Provider adapters"]
  node_auth["Provider auth"]
end

subgraph group_optimization["Context optimization"]
  node_preemptive["Preemptive compaction<br/>[manager.go]"]
  node_tooloutput["Tool output pipes<br/>[tool_output.go]"]
  node_search["Tool search dispatch"]
  node_expand["Context expansion"]
  node_store[("Expanded content store<br/>[store.go]")]
  node_llm_client["Compression LLM client<br/>[llm.go]"]
end

subgraph group_operations["Operations"]
  node_config["Runtime configuration<br/>[reloader.go]"]
  node_monitoring["Usage monitoring"]
  node_dashboard_api["Dashboard handlers<br/>[handlers.go]"]
end

subgraph group_dashboard["Dashboard"]
  node_webapp["Dashboard interface<br/>[App.tsx]"]
end

node_agent(("AI agent"))
node_llm(("LLM API"))
node_compression_api(("Compresr service"))

node_agent -->|"sends requests"| node_gateway
node_gateway -->|"dispatches requests"| node_providers
node_gateway -->|"uses auth"| node_auth
node_providers -.->|"forwards requests"| node_llm
node_gateway -->|"checks compaction"| node_preemptive
node_preemptive -.->|"summarizes context"| node_llm_client
node_gateway -->|"processes tool output"| node_tooloutput
node_gateway -->|"dispatches tool search"| node_search
node_gateway -->|"handles expansion"| node_expand
node_expand -->|"reads content"| node_store
node_gateway -->|"loads configuration"| node_config
node_gateway -->|"records activity"| node_monitoring
node_gateway -.->|"serves dashboard data"| node_dashboard_api
node_webapp -.->|"requests dashboard data"| node_dashboard_api
node_tooloutput -.->|"compresses output"| node_compression_api
node_tooloutput -.->|"uses LLM compression"| node_llm_client

click node_gateway "https://github.com/compresr-ai/context-gateway/blob/main/internal/gateway/handler.go"
click node_providers "https://github.com/compresr-ai/context-gateway/tree/main/internal/adapters"
click node_auth "https://github.com/compresr-ai/context-gateway/tree/main/internal/auth"
click node_preemptive "https://github.com/compresr-ai/context-gateway/blob/main/internal/preemptive/manager.go"
click node_tooloutput "https://github.com/compresr-ai/context-gateway/blob/main/internal/pipes/tool_output/tool_output.go"
click node_search "https://github.com/compresr-ai/context-gateway/blob/main/internal/gateway/search_tool_handler.go"
click node_expand "https://github.com/compresr-ai/context-gateway/blob/main/internal/gateway/expand_context_handler.go"
click node_store "https://github.com/compresr-ai/context-gateway/blob/main/internal/store/store.go"
click node_config "https://github.com/compresr-ai/context-gateway/blob/main/internal/config/reloader.go"
click node_monitoring "https://github.com/compresr-ai/context-gateway/tree/main/internal/monitoring"
click node_dashboard_api "https://github.com/compresr-ai/context-gateway/blob/main/internal/dashboard/handlers.go"
click node_webapp "https://github.com/compresr-ai/context-gateway/blob/main/web/dashboard/src/App.tsx"
click node_llm_client "https://github.com/compresr-ai/context-gateway/blob/main/external/llm.go"

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_gateway,node_providers,node_auth toneBlue
class node_preemptive,node_tooloutput,node_search,node_expand,node_store,node_llm_client toneAmber
class node_config,node_monitoring,node_dashboard_api,node_llm toneMint
class node_webapp toneRose
class node_agent,node_compression_api toneIndigo
```

## Components

### Gateway runtime

- [HTTP gateway](https://github.com/compresr-ai/context-gateway/blob/main/internal/gateway/handler.go): `internal/gateway/handler.go`
- [Provider adapters](https://github.com/compresr-ai/context-gateway/tree/main/internal/adapters): `internal/adapters`
- [Provider auth](https://github.com/compresr-ai/context-gateway/tree/main/internal/auth): `internal/auth`

### Context optimization

- [Preemptive compaction](https://github.com/compresr-ai/context-gateway/blob/main/internal/preemptive/manager.go): `internal/preemptive/manager.go`
- [Tool output pipes](https://github.com/compresr-ai/context-gateway/blob/main/internal/pipes/tool_output/tool_output.go): `internal/pipes/tool_output/tool_output.go`
- [Tool search dispatch](https://github.com/compresr-ai/context-gateway/blob/main/internal/gateway/search_tool_handler.go): `internal/gateway/search_tool_handler.go`
- [Context expansion](https://github.com/compresr-ai/context-gateway/blob/main/internal/gateway/expand_context_handler.go): `internal/gateway/expand_context_handler.go`
- [Expanded content store](https://github.com/compresr-ai/context-gateway/blob/main/internal/store/store.go): `internal/store/store.go`
- [Compression LLM client](https://github.com/compresr-ai/context-gateway/blob/main/external/llm.go): `external/llm.go`

### Operations

- [Runtime configuration](https://github.com/compresr-ai/context-gateway/blob/main/internal/config/reloader.go): `internal/config/reloader.go`
- [Usage monitoring](https://github.com/compresr-ai/context-gateway/tree/main/internal/monitoring): `internal/monitoring`
- [Dashboard handlers](https://github.com/compresr-ai/context-gateway/blob/main/internal/dashboard/handlers.go): `internal/dashboard/handlers.go`

### Dashboard

- [Dashboard interface](https://github.com/compresr-ai/context-gateway/blob/main/web/dashboard/src/App.tsx): `web/dashboard/src/App.tsx`

### Other components

- **AI agent**
- **LLM API**
- **Compresr service**

## Connections

- AI agent → HTTP gateway: sends requests (evidence: `README.md`)
- HTTP gateway → Provider adapters: dispatches requests (evidence: `internal/gateway/handler.go`)
- HTTP gateway → Provider auth: uses auth (evidence: `internal/gateway/handler.go`)
- Provider adapters → LLM API: forwards requests
- HTTP gateway → Preemptive compaction: checks compaction (evidence: `internal/gateway/handler.go`)
- Preemptive compaction → Compression LLM client: summarizes context
- HTTP gateway → Tool output pipes: processes tool output
- HTTP gateway → Tool search dispatch: dispatches tool search (evidence: `internal/gateway/search_tool_handler.go`)
- HTTP gateway → Context expansion: handles expansion (evidence: `internal/gateway/expand_context_handler.go`)
- Context expansion → Expanded content store: reads content (evidence: `internal/gateway/expand_context_handler.go`)
- HTTP gateway → Runtime configuration: loads configuration
- HTTP gateway → Usage monitoring: records activity (evidence: `internal/gateway/handler.go`)
- HTTP gateway → Dashboard handlers: serves dashboard data
- Dashboard interface → Dashboard handlers: requests dashboard data
- Tool output pipes → Compresr service: compresses output
- Tool output pipes → Compression LLM client: uses LLM compression
