# Migrating from LangChain (/docs/migrating-from-langchain)



LangChain and `@pleach/core` overlap structurally — both treat
agent execution as composable primitives with explicit control
flow — but they make different load-bearing decisions. LangChain
optimizes for *integration breadth*: hundreds of pre-built
loaders, retrievers, agents, and tools. `@pleach/core` optimizes
for *structural guarantees*: a 4-stage lattice, family-locked
routing, append-only audit, replay from the record.

This guide walks the migration when you've decided the structural
guarantees are what you need. If your LangChain code is mostly
using the integration catalog (loaders, vector stores, retrievers),
keep using LangChain for that — the migration target is the
agent + chain surface, not the data layer.

<Callout title="Two shapes: recognized provider vs bespoke infra">
  This guide keeps a *recognized* provider — `@pleach/langchain`
  ships `LangChainProvider` (your LangChain chat model as a Pleach
  provider) and `fromLangChainTools` (your tools as Pleach tools), so
  both drop straight in:

  ```typescript
  import { LangChainProvider, fromLangChainTools } from "@pleach/langchain";

  const provider = new LangChainProvider(chatModel); // your BaseChatModel
  const tools = fromLangChainTools([retrieveDocs, searchDb]);
  ```

  If your
  provider or store is *bespoke* (an in-house gateway, a custom
  Postgres schema), the same graft still works, but you implement
  the adapter interface yourself. See
  [Adoption paths → Pick core-brownfield when](/docs/adoption-paths)
  and the bundled `examples/brownfield-adapter/`.
</Callout>

## When to migrate, when not to [#when-to-migrate-when-not-to]

<Callout type="info" title="Stay on LangChain or LangGraph if">
  * The integration catalog (document loaders, vector stores,
    retrievers) is the value.
  * LangGraph's per-node control flow already covers your shape and
    you don't need the lattice / audit / family-lock guarantees.
  * You're shipping a RAG pipeline first, an agent second.
</Callout>

Migrate to `@pleach/core` when:

* You need per-call audit rows, joinable to sessions and turns.
* You need to replay a recorded turn from its record, without
  calling the model, for eval or regression testing.
* You need family-locked provider routing (no silent cross-family
  widening when a provider fails).

You can run both — keep LangChain for the retrieval side, point
the runtime at it through a `defineTool` wrapper, and let the
runtime own the agent execution.

## The mental-model shift [#the-mental-model-shift]

| LangChain                                         | `@pleach/core`                                 | What's different                                                                                                                           |
| ------------------------------------------------- | ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `Runnable`                                        | (no direct equivalent)                         | The substrate doesn't model individual steps as composable runnables. The graph topology is declarative; the per-call surface is the seam. |
| Chains (`RunnableSequence`, `RunnableParallel`)   | Stage lattice + channels                       | Topology is constrained to the 4 stages; concurrency is a channel concern, not a chain primitive.                                          |
| LangGraph                                         | The substrate's graph + channels               | Same family of primitives; the lattice + call-class typing are the additions.                                                              |
| Agents (ReAct, OpenAI Functions)                  | `tool-loop` stage + seams                      | The agent loop is built in. You provide tools; the runtime drives the loop.                                                                |
| Callbacks (`callbacks`, `BaseCallbackHandler`)    | Stream events + audit ledger                   | Audit is structural, not optional. Events are typed.                                                                                       |
| Memory (`BaseMemory`, `ConversationBufferMemory`) | `SessionState.messages` + `@pleach/core/store` | Memory is session state; cross-session memory is a separate subpath.                                                                       |
| Document loaders / vector stores                  | (out of scope)                                 | The substrate doesn't ship retrieval. Keep LangChain or another retrieval library for that.                                                |
| LangSmith tracing                                 | Audit ledger + custom adapters                 | The ledger is the trace store. Plug an OTel adapter for LangSmith parity.                                                                  |

<Steps>
  <Step>
    ## Keep your retrieval; wrap it as a tool [#keep-your-retrieval-wrap-it-as-a-tool]

    The most common LangChain investment is in document loaders +
    retrievers. Don't migrate that — wrap it.

    ```typescript
    // Before — LangChain
    import { ChatPromptTemplate } from "@langchain/core/prompts";
    import { RunnableSequence } from "@langchain/core/runnables";

    const chain = RunnableSequence.from([
      { context: retriever, question: (input) => input.question },
      promptTemplate,
      llm,
    ]);

    // After — wrap the retriever as a defineTool
    import { defineTool } from "@pleach/core";

    export const retrieveDocs = defineTool({
      name: "retrieve_docs",
      description: "Retrieve relevant documents for a query.",
      inputSchema: z.object({
        query: z.string().min(1),
        k:     z.number().int().min(1).max(20).default(5),
      }),
      async execute(input, ctx) {
        const docs = await retriever.invoke(input.query, {
          configurable: { k: input.k },
          signal:       ctx.signal,
        });
        return { docs: docs.map((d) => ({
          pageContent: d.pageContent,
          metadata:    d.metadata,
        })) };
      },
    });
    ```

    The retriever's full configuration (vector store, embedding
    model, reranker, MMR settings) stays inside the tool. The
    runtime just sees a typed tool with a Zod schema.
  </Step>

  <Step>
    ## Replace the chain with a SessionRuntime [#replace-the-chain-with-a-sessionruntime]

    ```typescript
    // Before — agent with chain
    import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";

    const agent = await createOpenAIFunctionsAgent({
      llm, tools: [retrieveDocs, searchDb],
      prompt: agentPromptTemplate,
    });
    const executor = new AgentExecutor({ agent, tools: [...] });

    const result = await executor.invoke({ input: "What is X?" });

    // After
    import { SessionRuntime, AiSdkProvider } from "@pleach/core";

    const runtime = new SessionRuntime({
      provider: new AiSdkProvider({ model: openai("gpt-4o") }),
      storage,
      userId,
    });

    const session = await runtime.createSession({
      tools: { enabled: ["retrieve_docs", "search_db"] },
    });

    for await (const event of runtime.executeMessage(session.id, "What is X?")) {
      // stream events
    }
    ```

    The agent loop is built into the `tool-loop` stage. You don't
    write a `createXAgent` call — the runtime drives the
    LLM-decision ↔ tool-execution cycle until the plan resolves.
  </Step>

  <Step>
    ## Convert callbacks to stream events + plugins [#convert-callbacks-to-stream-events--plugins]

    LangChain's callback handlers are an event subscription surface;
    the runtime's equivalent is the stream + plugin contract.

    A handler sees one model run per `invoke`, not one per attempt.
    `@langchain/openai` builds its OpenAI client with `maxRetries: 0` and
    retries inside its own call wrapper, so every retry stays inside that
    one run (checked against 1.8.4). `@pleach/core` writes a call row per
    attempt instead.

    ```typescript
    // Before — BaseCallbackHandler
    class MyHandler extends BaseCallbackHandler {
      name = "my-handler";

      async handleLLMStart(llm, prompts) {
        metrics.increment("llm.starts");
      }

      async handleLLMEnd(output) {
        metrics.timing("llm.tokens", output.llmOutput?.tokenUsage?.totalTokens);
      }

      async handleToolStart(tool, input) {
        metrics.increment("tool.starts", { name: tool.name });
      }
    }

    // After — tool starts arrive as `tool.started` StreamEvents on the
    // executeMessage() iterable (they are stream events, not emitter events)
    for await (const event of runtime.executeMessage(sessionId, input)) {
      if (event.type === "tool.started") {
        metrics.increment("tool.starts", { name: event.toolCall.name });
      }
    }

    // The audit ledger is the per-LLM-call surface. Wrap your ledger
    // adapter to also emit metrics:
    class MetricsAwareLedger implements ProviderDecisionLedger {
      constructor(private primary: ProviderDecisionLedger) {}
      async recordCall(call: AuditableCall) {
        metrics.increment("llm.calls", { model: call.call.model });
        metrics.timing("llm.latency", call.outcome.latencyMs);
        return this.primary.recordCall(call);
      }
    }
    ```

    The audit ledger is the per-LLM-call observability surface —
    every call writes a row with `model`, `family`, `tokenCost`,
    `latencyMs`. That's what LangChain's `handleLLMEnd` gave you,
    made structural.

    While a LangGraph app still runs beside the runtime, record its
    callbacks into the same event log with `HarnessEventLogCallback`
    from `@pleach/langchain/to-langchain`. Pass it in the run's
    `callbacks`; each model and tool callback becomes a `@pleach/core`
    event row (`llm.turn`, `tool.started`, `tool.completed`,
    `tool.failed`). See
    [Recording a LangGraph run](/docs/langchain#recording-a-langgraph-run).

    ```typescript
    import { HarnessEventLogCallback } from "@pleach/langchain/to-langchain";

    const callback = new HarnessEventLogCallback(sink, sessionId);
    await graph.invoke(input, { callbacks: [callback] });
    ```
  </Step>

  <Step>
    ## Convert memory to session state [#convert-memory-to-session-state]

    LangChain's `ConversationBufferMemory` (and variants) maps to
    `SessionState.messages` directly. The runtime owns the
    conversation history; you don't construct a memory class.

    ```typescript
    // Before
    const memory = new ConversationBufferMemory({ returnMessages: true });
    await memory.saveContext({ input: userInput }, { output: assistantOutput });
    const { history } = await memory.loadMemoryVariables({});

    // After
    // The runtime persists messages automatically via the storage adapter.
    // Read history via:
    const session = await runtime.sessions.find(sessionId);
    const history = session?.state.messages ?? [];
    ```

    For cross-session memory (LangChain's `VectorStoreRetrieverMemory`,
    `ConversationSummaryMemory` with persistence), use the
    `@pleach/core/store` cross-session memory primitives or wrap an
    external store as a tool.
  </Step>

  <Step>
    ## Convert LangGraph nodes to the lattice [#convert-langgraph-nodes-to-the-lattice]

    LangGraph users will find the closest mental match in the
    substrate's graph + channels. The migration shape:

    | LangGraph                    | `@pleach/core`                                                                |
    | ---------------------------- | ----------------------------------------------------------------------------- |
    | `StateGraph`                 | The compiled graph (declarative; lattice-constrained)                         |
    | Node                         | A graph node belonging to one of 4 stages                                     |
    | Edge                         | Constrained to the 4-stage lattice transitions                                |
    | `Annotation.Root` / channels | Same idea — `LastValue`, `Topic`, `BinaryOperatorAggregate`, etc.             |
    | `interrupt()`                | `HumanInterrupt` envelope (LangGraph-compatible shape)                        |
    | Conditional edges            | Channel-driven scheduling — a node fires when its subscribed channel advances |

    The `HumanInterrupt` shape is intentionally LangGraph-compatible
    — external tooling (LangGraph Inspector, dashboards) interops
    without translation. See [Interrupts](/docs/interrupts).

    What changes: the lattice constrains where nodes live. A node
    that "doesn't fit" any of the 4 stages is a signal that it's two
    nodes (often a planner that should be in `anchor-plan` plus a
    quality scorer that should be in `post-turn`). The substrate's
    graph topology is more opinionated than LangGraph's
    free-form state machine.
  </Step>

  <Step>
    ## Tool ecosystem [#tool-ecosystem]

    The substrate doesn't ship LangChain's tool catalog. Three paths
    for the tools you depended on:

    1. **Convert it with `fromLangChainTool`** (or `fromLangChainTools`
       for a list) from `@pleach/langchain`. The tool's name,
       description, schema and implementation carry over.
    2. **Rewrite using `@pleach/tools`.** The sibling SKU ships
       filesystem / HTTP / shell / structured-parse primitives with
       Zod schemas and consistent error handling. Use for common
       tools.
    3. **Build native `defineTool` implementations.** For
       domain-specific tools, the explicit Zod schema + named
       batching strategy is worth the rewrite.
  </Step>
</Steps>

<a id="what-you-gain-after-the-migration" />

## Already on LangChain or LangGraph? [#already-on-langchain-or-langgraph]

**N** is native, **E** is an easy side module beside the host, **H**
is hard in combination with the rest. LangGraph is native for
checkpoints, forking at the model-call boundary and `interrupt()`.
Pleach's column is what the runtime path does today; the four interactions that get hard
together are on [the landscape page](/docs/landscape#if-you-already-run-a-runtime).

| Capability                    | LangChain                                   | LangGraph (`@langchain/langgraph@1.4.18`)     | Pleach today                                                                                                                                                                                                                                                                                                                                                                                                                       |
| ----------------------------- | ------------------------------------------- | --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Row per attempt               | H: callbacks see one call per invocation    | H: three retry layers; callbacks see one call | On the built-in providers, every request the runtime sends writes its own row, retries included, for sub-agents too; a provider that doesn't declare per-attempt rows keeps one row per call. The provider SDK's own retries are turned off so none hide inside it. An attempt cut off by an abort still closes its row.                                                                                                           |
| Chained row                   | H                                           | H: sink and checkpointer commit separately    | Call rows are hash-chained by default, in Postgres and in the quickstart store. The verifier names the first row that doesn't match. It catches an edit, a backfill or a deletion; it doesn't stop one, and an unkeyed chain can be rewritten end to end by anyone with write access.                                                                                                                                              |
| Typed keys + sub-agent rollup | H: callbacks run with async context cleared | H: callbacks run with async context cleared   | the root turn id and sub-agent depth on every row                                                                                                                                                                                                                                                                                                                                                                                  |
| Cost per turn and tenant      | E–H                                         | E–H                                           | `modelCall.costUsd` on each per-call row (`costOmitted` says why when it is absent). Calls that write the prompt cache are priced only when the call reports how many writes were 5-minute and how many 1-hour and there is a rate for each, and marked unpriced otherwise.                                                                                                                                                        |
| Tenant on row, span and HTTP  | E                                           | E                                             | The tenant id on every row and span. Providers stamp it on outbound HTTP as `x-tenant-id` by default; `tenantHeader: false` turns that off and `tenantHeader: "<name>"` renames it.                                                                                                                                                                                                                                                |
| Scrub before write            | H                                           | H: the checkpointer stores plaintext          | A default PII redactor runs on the call ledger before the row is hashed. Opt out explicitly with `piiRedactorNoop()`. Event-log scrubbers run on the Postgres and SQLite writers by default; opt out of SQLite scrubbing with `createSqliteDurableStore(path, { redaction: "off" })` (the sidecar uses `PLEACH_REDACT=0`).                                                                                                         |
| Replay without re-firing      | H                                           | H: `interrupt()` resume re-runs the node      | Replay re-serves a turn's recorded responses instead of calling the model. `replayRecordedTurn` (from `@pleach/replay/recorded`) replays a recorded turn against its original record and names the first request that drifted; steps the runtime doesn't fingerprint yet (decision, synthesize) are marked blind, so a turn on a stock runtime is never reported `matched`.                                                        |
| Fork and rewind               | via LangGraph                               | E at the model-call boundary                  | A fork's first row commits to the parent row it branched from; `verifyBranchPoint` checks it. A rollback leaves a tombstone row.                                                                                                                                                                                                                                                                                                   |
| Erasure with a chain          | H                                           | H: three stores                               | Crypto-shred erasure keeps every event-log row hash verifying. Call-ledger rows are sealed per subject by default wherever a key store is configured; shredding the subject's key keeps every call-row hash verifying and makes the sealed fields unreadable. User ids and the row's wrapper columns aren't erased, and the quickstart's default store has no key store, so its rows stay plaintext unless you pass `subjectKeys`. |
| Human-in-the-loop             | via LangGraph                               | N: `interrupt()`                              | N                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| Checkpoints and crash resume  | via LangGraph                               | N                                             | N                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| Family lock                   | E: per-chain provider choice                | E                                             | A session stays in one model family. When a model fails, the cascade retries within that family and never silently widens, including for models outside the routing matrix. The old cross-family fallback is opt-in.                                                                                                                                                                                                               |
| OTel threading                | H: no native OTel                           | H: no native OTel                             | parent-threaded spans                                                                                                                                                                                                                                                                                                                                                                                                              |

## What gets hard on LangGraph together [#what-gets-hard-on-langgraph-together]

* **Callbacks lose the turn.** LangChain callbacks run with async
  context cleared, so a tenant or turn id held in async-local
  storage isn't there when the callback writes its row.
* **Retries hide in layers.** A single model call can make up to 21
  attempts across three retry layers, and a callback sees one call.
* **Two stores, two commits.** The checkpointer and your audit sink
  commit separately, so a crash between them leaves a checkpoint
  with no row, or a row with no checkpoint.

## What you keep paying [#what-you-keep-paying]

<Callout type="info" title="Costs that don't go away">
  * LangChain's integration breadth is gone. You bring the loaders
    and retrievers; the substrate doesn't ship them.
  * The lattice is opinionated. Code that fits the LangGraph
    free-form state-machine model has to be re-shaped to fit the 4
    stages.
  * The runtime adds a storage dependency. Mock mode works for
    dev; production wants a real database with the schema bundle
    applied.
</Callout>

## Common migration pitfalls [#common-migration-pitfalls]

| Symptom                                   | Likely cause                                                                                                                                                                                  |
| ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Agent loop runs once and exits            | `maxSteps` not passed on `AiSdkProvider`; default is 1                                                                                                                                        |
| Tool fires but the LLM ignores the result | Tool name doesn't match what the model emits — Zod-validated names are stricter than LangChain's tolerance                                                                                    |
| LangGraph node doesn't fit any stage      | Almost always 2 nodes — split into a planner (`anchor-plan`) and a scorer (`post-turn`)                                                                                                       |
| Callback handler doesn't fire             | LangChain callbacks have no analogue for stream-level events — use `runtime.subscribeToStream(mode, callback)` for those; `runtime.on` subscribes to runtime lifecycle events, not the stream |
| Memory class missing                      | Memory IS session state; no separate class needed                                                                                                                                             |

## Where to go next [#where-to-go-next]

<Cards>
  <Card title="Providers" href="/docs/providers" description="`AiSdkProvider` config — same `streamText` you're already using." />

  <Card title="Tools" href="/docs/tools" description="`defineTool` — wrapping LangChain tools as Zod-validated definitions." />

  <Card title="Architecture" href="/docs/architecture" description="The 4-stage lattice and call classes that constrain where nodes live." />

  <Card title="Interrupts" href="/docs/interrupts" description="The LangGraph-compatible `HumanInterrupt` envelope." />
</Cards>
