Docs / Core Concepts / Architecture & Purity

Architecture & Purity

The 3-tier monorepo architecture, effect polymorphism, and zero-unsafePerformIO guarantee.

Monorepo Architecture

langchain-hs is split into three independent layers to guarantee modularity and zero unnecessary runtime dependencies:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       langchain-hs                          β”‚
β”‚  Providers (Ollama, OpenAI, Gemini) Β· MCP Client Β· RAG     β”‚
β”‚  Chains Β· Vector Stores Β· Agents Β· OpenTelemetry Observabilityβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚ depends on
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    langchain-hs-graph                       β”‚
β”‚  StateGraph s m Β· StateReducer s Β· Checkpointers Β· HITL     β”‚
β”‚  TimeTravel Β· Parallel Execution Β· Graphviz DOT Export      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚ depends on
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    langchain-hs-core                        β”‚
β”‚  Zero-Dependency Pure GADT ASTs (RunnableTree)              β”‚
β”‚  ChatModel Β· ContentBlock Β· Tool m Β· StreamEvent Lifecycle  β”‚
β”‚  Zero HTTP / Network / Database Dependencies                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1. Zero unsafePerformIO Guarantee

In many imperative AI frameworks, state mutation, network I/O, and tool executions are side-effecting operations scattered throughout code. In langchain-hs:

  • Construction is Pure: Creating a RunnableTree, StateGraph, or Chain produces a pure data structure AST without executing any network or disk operations.
  • Execution is Explicit: All effects are tracked in the monad m (e.g.Β IO, LangchainT m, or custom monad stacks).
  • Zero Hidden Global State: State is explicitly passed or held in STM TVar cells with ACID properties.

2. Algebraic Laws & QuickCheck Properties

Components in langchain-hs are verified against standard algebraic laws:

1. StateReducer Monoid Associativity

State reducers must satisfy associativity so that concurrent parallel branches can be merged in any order without race conditions:

βˆ€a, b, c.Β (aβ‹„b)β‹„c ≑ aβ‹„(bβ‹„c)

2. Runnable Identity & Composition Laws

Pure runnable pipelines satisfy Category / Arrow laws:

interpret (id|β€Šβ‰«f) ≑ interpret f interpret (f|β€Šβ‰«id) ≑ interpret f


3. Effect Polymorphism

All models and tools are polymorphic over their execution monad m:

-- Effect-polymorphic ChatModel
class (MonadIO m, MonadError LangchainError m) => ChatModel model m where
  invoke :: model -> [Message] -> m Message
  streamModel :: model -> [Message] -> (StreamEvent -> m ()) -> m ()

This allows you to run pipelines inside unit test harnesses with Identity or State monads, or in production web servers inside custom ReaderT AppEnv (ExceptT AppError IO).

ESC