# tamasmakos/graphknows architecture

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

- Interactive diagram: https://gitdiagram.com/tamasmakos/graphknows
- Repository: https://github.com/tamasmakos/graphknows

## Overview

1. **What this repository is**
   - Graphknows is a multi-service “agentic knowledge graph” system for investigative research.
   - It turns documents and conversations into a persistent semantic memory and serves answers through an agent that combines graph retrieval, vector search, and session memory.

2. **Top-level architectural split**
   - The codebase is divided into four application surfaces:
     - `services/agent`: the main orchestration service
     - `services/graphknows` / `graphknows`: the HybridRAG and memory core
     - `services/search`: document search/indexing
     - `apps/web`: the chat frontend
   - The rest of the repository is shared domain logic, adapters, ontology assets, migrations, and tests.

3. **Primary request path**
   - User input enters the **frontend** over WebSocket.
   - The frontend forwards messages to the **agent service**.
   - The agent uses a LangGraph state machine to plan, search, retrieve, and generate a briefing.
   - During execution it emits streaming events such as thinking, tool start, graph updates, and completion back to the UI.

4. **Agent service boundary**
   - `services/agent` is the orchestration layer and the user-facing API.
   - It contains the LangGraph workflow, event streaming, router endpoints, and session lifecycle hooks.
   - It does not own the knowledge data itself; it coordinates calls into the retrieval and memory systems.

5. **HybridRAG / knowledge-core boundary**
   - The `graphknows` package is the core reasoning and retrieval subsystem.
   - It includes:
     - ingestion/parsing
     - graph generation and extraction
     - ontology loading and relation extraction
     - retrieval and scoring
     - memory management
     - adapters for graph/vector/LLM backends
   - This is the main boundary between abstract domain logic and concrete infrastructure clients.

6. **Persistent and ephemeral storage**
   - **Neo4j** stores the durable knowledge graph: documents, chunks, entities, and relations.
   - **pgvector/Postgres** stores embeddings for hybrid vector retrieval.
   - **Memgraph** stores an ephemeral per-session graph overlay used for short-lived conversational state and session-specific graph reasoning.
   - The architecture therefore separates long-term knowledge from temporary conversation context.

7. **Data modeling and enrichment pipeline**
   - Documents are parsed by ingestion parsers for text, code, and docx.
   - The graph generation layer chunks text, extracts entities/relations, canonicalizes names, and resolves coreference.
   - Ontology-guided extraction shapes the relation schema and entity interpretation.
   - The resulting graph objects and embeddings are written to the persistent stores.

8. **Memory subsystem**
   - `graphknows/memory` is split into short-term memory (STM) and long-term memory (LTM).
   - STM handles dialog-level state, ingest, recall, and session summarization.
   - LTM handles promotion, enrichment, relation/topic services, and durable memory writing.
   - This creates a clear lifecycle boundary: session observations are first captured in STM, then selectively promoted into LTM.

9. **Retrieval subsystem**
   - `graphknows/retrieval` combines graph traversal, vector search, context assembly, and scoring.
   - Store-specific adapters exist for Neo4j, Memgraph, and pgvector.
   - The retrieval layer is designed as a hybrid system rather than a single index: graph structure provides precision, vectors provide semantic recall, and context/scoring merge them into answerable evidence.

10. **Search service**
   - `services/search` is a separate document search and indexing service.
   - It is used for corpus/document lookup rather than graph reasoning.
   - In the overall flow, it supplies source documents and indexed artifacts that the agent later turns into graph-grounded briefings.

11. **Ontology and domain assets**
   - The repo includes packaged ontologies for finance, legal, politics, and science.
   - These are loaded through ontology/router/loader components and used to guide extraction and relation normalization.
   - This means the system is domain-aware: extraction and interpretation are constrained by curated ontology packages, not just generic NLP.

12. **MCP and tool surface**
   - `graphknows/mcp` exposes the knowledge core through an MCP server and tools for corpus, recall, STM, LTM, taxonomy, and admin actions.
   - This makes the knowledge system usable as a tool backend for agents and other clients without coupling them directly to the internal Python modules.

13. **Configuration, infrastructure, and external dependencies**
   - The system expects Docker-managed infrastructure plus bare-metal app services.
   - Key external services are Neo4j, Postgres/pgvector, Memgraph, and OpenRouter for LLM/embedding calls.
   - The repo’s health, preflight, and compose setup are part of the operational architecture because service startup depends on validated environment variables and reachable databases.

14. **Quality and boundaries**
   - The tests are organized by subsystem: adapters, retrieval, memory, ontology, ingestion, MCP, agent, and search.
   - This mirrors the architecture and suggests strong module boundaries.
   - The repository is not a monolith; it is a layered system with explicit seams between orchestration, retrieval, memory, storage adapters, and presentation.

Main entry points: [`graphknows/retrieval/retriever.py`](https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/retriever.py) (Retrieval Core), [`graphknows/graphgen/extraction/relations.py`](https://github.com/tamasmakos/graphknows/blob/main/graphknows/graphgen/extraction/relations.py) (Graph Generation), [`services/agent/graph/workflow.py`](https://github.com/tamasmakos/graphknows/blob/main/services/agent/graph/workflow.py) (Agent Graph), [`graphknows/mcp/_app.py`](https://github.com/tamasmakos/graphknows/blob/main/graphknows/mcp/_app.py) (MCP Surface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_apps["Applications"]
  node_web_chat["Web Chat<br/>frontend<br/>[page.tsx]"]
  node_web_socket["WS Hook<br/>client bridge"]
  node_agent_api["Agent API<br/>orchestrator<br/>[router.py]"]
  node_agent_graph{{"Agent Graph<br/>langgraph workflow<br/>[workflow.py]"}}
  node_agent_events["Event Stream<br/>streaming events<br/>[stream_events.py]"]
  node_source_router["Source Router<br/>tool routing<br/>[source_router.py]"]
  node_search_service["Search Service<br/>document search<br/>[archive_engine.py]"]
end

subgraph group_core["Knowledge Core"]
  node_retrieval_core{{"Retrieval Core<br/>hybrid retrieval<br/>[retriever.py]"}}
  node_context_scoring["Context & Score<br/>ranking<br/>[context.py]"]
  node_memory_stm["Short-Term Memory<br/>session memory<br/>[service.py]"]
  node_memory_ltm["Long-Term Memory<br/>durable memory<br/>[writer.py]"]
  node_memory_models["Memory Models<br/>domain models<br/>[ingest.py]"]
  node_ingest_parse["Ingestion Parse<br/>parsers<br/>[registry.py]"]
  node_graphgen{{"Graph Generation<br/>extraction pipeline<br/>[relations.py]"}}
  node_ontology_router["Ontology Router<br/>domain semantics<br/>[router.py]"]
  node_mcp_surface["MCP Surface<br/>tool server<br/>[_app.py]"]
end

subgraph group_infra["Adapters and Stores"]
  node_neo4j_store[("Neo4j Graph<br/>durable graph store<br/>[neo4j.py]")]
  node_pgvector_store[("Vector Store<br/>embedding store<br/>[pgvector.py]")]
  node_memgraph_store[("Session Graph<br/>ephemeral graph store<br/>[memgraph.py]")]
  node_openrouter_llm["LLM Backend<br/>llm/embedder<br/>[openai_llm.py]"]
end

subgraph group_assets["Domain Assets"]
  node_ontology_assets["Ontology Packs<br/>curated assets<br/>[registry.json]"]
end

node_web_chat -->|"sends messages"| node_web_socket
node_web_socket -->|"forwards"| node_agent_api
node_agent_api -->|"runs workflow"| node_agent_graph
node_agent_graph -->|"streams"| node_agent_events
node_agent_graph -->|"selects source"| node_source_router
node_source_router -->|"queries"| node_search_service
node_source_router -->|"retrieves"| node_retrieval_core
node_retrieval_core -->|"assembles"| node_context_scoring
node_retrieval_core -->|"traverses"| node_neo4j_store
node_retrieval_core -->|"vector search"| node_pgvector_store
node_retrieval_core -->|"session overlay"| node_memgraph_store
node_memory_stm -->|"uses"| node_memory_models
node_memory_stm -->|"stores session"| node_memgraph_store
node_memory_ltm -->|"uses"| node_memory_models
node_memory_ltm -->|"writes durable"| node_neo4j_store
node_ingest_parse -->|"feeds"| node_graphgen
node_graphgen -->|"guided by"| node_ontology_router
node_graphgen -->|"persists graph"| node_neo4j_store
node_graphgen -->|"persists embeddings"| node_pgvector_store
node_ontology_router -->|"loads"| node_ontology_assets
node_mcp_surface -->|"exposes STM"| node_memory_stm
node_mcp_surface -->|"exposes LTM"| node_memory_ltm
node_mcp_surface -->|"exposes recall"| node_retrieval_core
node_openrouter_llm -->|"generates"| node_agent_graph
node_openrouter_llm -->|"extracts"| node_graphgen
node_openrouter_llm -->|"ranks/embeds"| node_retrieval_core

click node_web_chat "https://github.com/tamasmakos/graphknows/blob/main/apps/web/src/app/chat/page.tsx"
click node_web_socket "https://github.com/tamasmakos/graphknows/blob/main/apps/web/src/hooks/useAgentWebSocket.ts"
click node_agent_api "https://github.com/tamasmakos/graphknows/blob/main/services/agent/router.py"
click node_agent_graph "https://github.com/tamasmakos/graphknows/blob/main/services/agent/graph/workflow.py"
click node_agent_events "https://github.com/tamasmakos/graphknows/blob/main/services/agent/graph/stream_events.py"
click node_source_router "https://github.com/tamasmakos/graphknows/blob/main/services/agent/tools/source_router.py"
click node_search_service "https://github.com/tamasmakos/graphknows/blob/main/services/search/archive_engine.py"
click node_retrieval_core "https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/retriever.py"
click node_context_scoring "https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/context.py"
click node_memory_stm "https://github.com/tamasmakos/graphknows/blob/main/graphknows/memory/stm/service.py"
click node_memory_ltm "https://github.com/tamasmakos/graphknows/blob/main/graphknows/memory/ltm/writer.py"
click node_memory_models "https://github.com/tamasmakos/graphknows/blob/main/graphknows/memory/models/ingest.py"
click node_ingest_parse "https://github.com/tamasmakos/graphknows/blob/main/graphknows/ingestion/parsers/registry.py"
click node_graphgen "https://github.com/tamasmakos/graphknows/blob/main/graphknows/graphgen/extraction/relations.py"
click node_ontology_router "https://github.com/tamasmakos/graphknows/blob/main/graphknows/ontology/router.py"
click node_mcp_surface "https://github.com/tamasmakos/graphknows/blob/main/graphknows/mcp/_app.py"
click node_neo4j_store "https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/stores/neo4j.py"
click node_pgvector_store "https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/stores/pgvector.py"
click node_memgraph_store "https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/stores/memgraph.py"
click node_openrouter_llm "https://github.com/tamasmakos/graphknows/blob/main/graphknows/adapters/openai_llm.py"
click node_ontology_assets "https://github.com/tamasmakos/graphknows/blob/main/packages/ontologies/registry.json"

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_web_chat,node_web_socket,node_agent_api,node_agent_graph,node_agent_events,node_source_router,node_search_service toneBlue
class node_retrieval_core,node_context_scoring,node_memory_stm,node_memory_ltm,node_memory_models,node_ingest_parse,node_graphgen,node_ontology_router,node_mcp_surface toneAmber
class node_neo4j_store,node_pgvector_store,node_memgraph_store,node_openrouter_llm toneMint
class node_ontology_assets toneRose
```

## Components

### Applications

User-facing and service entrypoints

- [Web Chat](https://github.com/tamasmakos/graphknows/blob/main/apps/web/src/app/chat/page.tsx): frontend · Browser chat UI and websocket client · `apps/web/src/app/chat/page.tsx`
- [WS Hook](https://github.com/tamasmakos/graphknows/blob/main/apps/web/src/hooks/useAgentWebSocket.ts): client bridge · WebSocket transport to the agent service · `apps/web/src/hooks/useAgentWebSocket.ts`
- [Agent API](https://github.com/tamasmakos/graphknows/blob/main/services/agent/router.py): orchestrator · Session lifecycle, routing, and streaming API · `services/agent/router.py`
- [Agent Graph](https://github.com/tamasmakos/graphknows/blob/main/services/agent/graph/workflow.py): langgraph workflow · Planning and execution state machine for briefings · `services/agent/graph/workflow.py`
- [Event Stream](https://github.com/tamasmakos/graphknows/blob/main/services/agent/graph/stream_events.py): streaming events · Thinking, tool, graph, and completion event emission · `services/agent/graph/stream_events.py`
- [Source Router](https://github.com/tamasmakos/graphknows/blob/main/services/agent/tools/source_router.py): tool routing · Routes agent requests to search and knowledge sources · `services/agent/tools/source_router.py`
- [Search Service](https://github.com/tamasmakos/graphknows/blob/main/services/search/archive_engine.py): document search · Corpus indexing and document retrieval surface · `services/search/archive_engine.py`

### Knowledge Core

Shared graph, memory, retrieval, and ontology subsystems

- [Retrieval Core](https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/retriever.py): hybrid retrieval · Combines graph traversal, vector search, context assembly, and scoring · `graphknows/retrieval/retriever.py`
- [Context & Score](https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/context.py): ranking · Evidence assembly and hybrid ranking · `graphknows/retrieval/context.py`
- [Short-Term Memory](https://github.com/tamasmakos/graphknows/blob/main/graphknows/memory/stm/service.py): session memory · Dialog state, recall, ingest, and summarization for sessions · `graphknows/memory/stm/service.py`
- [Long-Term Memory](https://github.com/tamasmakos/graphknows/blob/main/graphknows/memory/ltm/writer.py): durable memory · Promotion, enrichment, and persistent memory writing · `graphknows/memory/ltm/writer.py`
- [Memory Models](https://github.com/tamasmakos/graphknows/blob/main/graphknows/memory/models/ingest.py): domain models · Ingest, recall, promotion, evidence, and session models · `graphknows/memory/models/ingest.py`
- [Ingestion Parse](https://github.com/tamasmakos/graphknows/blob/main/graphknows/ingestion/parsers/registry.py): parsers · Text, code, and docx parsing into normalized inputs · `graphknows/ingestion/parsers/registry.py`
- [Graph Generation](https://github.com/tamasmakos/graphknows/blob/main/graphknows/graphgen/extraction/relations.py): extraction pipeline · Chunking, canonicalization, coreference, and relation extraction · `graphknows/graphgen/extraction/relations.py`
- [Ontology Router](https://github.com/tamasmakos/graphknows/blob/main/graphknows/ontology/router.py): domain semantics · Loads curated ontologies and guides extraction · `graphknows/ontology/router.py`
- [MCP Surface](https://github.com/tamasmakos/graphknows/blob/main/graphknows/mcp/_app.py): tool server · Exposes corpus, recall, STM, LTM, taxonomy, and admin tools · `graphknows/mcp/_app.py`

### Adapters and Stores

Concrete backend clients and persistence boundaries

- [Neo4j Graph](https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/stores/neo4j.py): durable graph store · Persistent knowledge graph for documents, chunks, entities, and relations · `graphknows/retrieval/stores/neo4j.py`
- [Vector Store](https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/stores/pgvector.py): embedding store · Embedding-backed semantic retrieval over Postgres/pgvector · `graphknows/retrieval/stores/pgvector.py`
- [Session Graph](https://github.com/tamasmakos/graphknows/blob/main/graphknows/retrieval/stores/memgraph.py): ephemeral graph store · Short-lived per-session graph overlay for conversational reasoning · `graphknows/retrieval/stores/memgraph.py`
- [LLM Backend](https://github.com/tamasmakos/graphknows/blob/main/graphknows/adapters/openai_llm.py): llm/embedder · Model calls for generation and embedding through adapter ports · `graphknows/adapters/openai_llm.py`

### Domain Assets

Packaged ontologies and configuration data

- [Ontology Packs](https://github.com/tamasmakos/graphknows/blob/main/packages/ontologies/registry.json): curated assets · Finance, legal, politics, and science ontology packages · `packages/ontologies/registry.json`

## Connections

- Web Chat → WS Hook: sends messages · Chat UI uses websocket transport
- WS Hook → Agent API: forwards · Client bridge delivers user turns to agent service
- Agent API → Agent Graph: runs workflow · API hands off orchestration to LangGraph state machine
- Agent Graph → Event Stream: streams · Workflow emits progressive execution events
- Agent Graph → Source Router: selects source · Routes tasks toward search or knowledge retrieval
- Source Router → Search Service: queries · Document/source lookup path
- Source Router → Retrieval Core: retrieves · Knowledge-grounded retrieval path
- Retrieval Core → Context & Score: assembles · Builds answerable context and ranks evidence
- Retrieval Core → Neo4j Graph: traverses · Graph retrieval over durable knowledge graph
- Retrieval Core → Vector Store: vector search · Semantic recall via embeddings
- Retrieval Core → Session Graph: session overlay · Consults temporary session graph state
- Short-Term Memory → Memory Models: uses · STM reads and writes shared memory data structures
- Short-Term Memory → Session Graph: stores session · Session-level state and recall
- Long-Term Memory → Memory Models: uses · LTM promotion and enrichment data contracts
- Long-Term Memory → Neo4j Graph: writes durable · Promotes enriched memories into persistent graph
- Ingestion Parse → Graph Generation: feeds · Parsed inputs enter graph generation pipeline
- Graph Generation → Ontology Router: guided by · Ontology-constrained extraction and normalization
- Graph Generation → Neo4j Graph: persists graph · Writes extracted entities and relations
- Graph Generation → Vector Store: persists embeddings · Stores chunk and document embeddings
- Ontology Router → Ontology Packs: loads · Domain packages and registry are consulted at runtime
- MCP Surface → Short-Term Memory: exposes STM · Tool interface for short-term memory operations
- MCP Surface → Long-Term Memory: exposes LTM · Tool interface for long-term memory operations
- MCP Surface → Retrieval Core: exposes recall · Tool access to retrieval and corpus functions
- LLM Backend → Agent Graph: generates · Language model calls used by orchestration
- LLM Backend → Graph Generation: extracts · LLM assists extraction and normalization
- LLM Backend → Retrieval Core: ranks/embeds · Model-backed scoring and embeddings support
