跪拜 Guibai
← All articles
Agent · Artificial Intelligence · LangChain

Don't Rewrite Dify: Sink Heavy Logic into LangGraph with a Hybrid Architecture

By Sailing ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams running Dify in production hit the same wall when complex loops and approvals outgrow the canvas; a full rewrite to LangGraph risks losing the self-serve operations workflow. A gradual hybrid gives developers code-level control over only the parts that need it, without sacrificing the low-code maintenance that keeps operations independent.

Summary

When a Dify workflow grows beyond a dozen branches, changing one branch forces a full regression and neither operations nor developers want to touch it. The common instinct is to rewrite everything in LangGraph for code-level control, but a full rewrite risks destroying the low-code self-service value that Dify provides. The safer path is a layered hybrid: keep Dify as the front door for simple, frequently-tuned prompt flows, and extract the heavy logic that the platform can't handle into standalone LangGraph services.

The article identifies four concrete migration targets: branch-heavy and loop-heavy workflows move to LangGraph's StateGraph; the knowledge base becomes an independent retrieval service with multi-path recall and reranking; scattered tool nodes consolidate into a unified Tool Runtime with auth, rate limiting, and auditing; and human approval moves to LangGraph's interrupt/resume mechanism with persisted thread_id state. For each, the key is not moving everything, but only the parts that exceed Dify's safe maintenance threshold. The suggested order starts with shared infrastructure (tool runtime and retrieval service), then an approval pilot, then gradual workflow migration with rollback paths.

The hybrid architecture carries real costs: extra HTTP hops, aligned tracing, independent deployment and monitoring for LangGraph, and a unified permission model. The author warns against layering for its own sake and lists three conditions where a hybrid makes no sense: if flows are almost entirely complex logic, if latency requirements reject extra network calls, or if the team is too small to maintain two systems. The final advice is to start with one high-risk but clearly bounded approval scenario and get interrupt/resume working before expanding.

Takeaways
— Dify and LangGraph operate at different layers: Dify handles delivery and operations, LangGraph handles code orchestration with state machines, loops, and interrupts.
— A hybrid architecture keeps Dify for simple Q&A and prompt tuning, while complex workflows, retrieval, tool management, and approvals sink into LangGraph services.
— Approval flows with rejection-and-edit should use LangGraph's interrupt/resume with a persisted thread_id, not Dify's native human node.
— The knowledge base should be extracted into an independent retrieval service shared by both Dify and LangGraph, not duplicated per platform.
— All tool calls should pass through a unified Tool Runtime that centralizes validation, auth, timeout, rate limiting, and audit logs.
— Migrate in three steps: first shared infrastructure (Tool Runtime and retrieval service), then one approval pilot, then complex workflows gradually with rollback paths.
— A hybrid architecture requires unified traceId + thread_id for end-to-end traceability, plus separate monitoring and scaling for LangGraph services.
— Avoid hybrid if the flow is almost all complex logic, if latency requirements rule out extra HTTP hops, or if the team is too small to run two systems.
Conclusions

The real value of Dify is not its visual editor but the operational autonomy it gives non-developers; any migration plan that turns prompt changes into code releases regresses that autonomy.

The line between what stays in Dify and what moves to LangGraph is set by maintenance safety: once a branch change on the canvas risks breaking the whole flow, it's time to move that flow to code.

Human approval is the highest-leverage migration target because it delivers a clear business capability (pausable, resumable, auditable high-risk operations) with a relatively bounded pilot scope.

A hybrid architecture's success is measured by observability and rollback capability, not by how cleanly the code is organized; without aligned tracing, debugging across two systems becomes the real bottleneck.

Concepts & terms
interrupt / resume (LangGraph)
A LangGraph mechanism that pauses a graph execution at a node, persists the state under a thread_id, and later resumes exactly from that point using the same thread_id. Used for human-in-the-loop approvals.
StateGraph
LangGraph's state-centric graph model where nodes and conditional edges define a workflow; the state carries all data through the execution, enabling loops, branching, and unit testing.
Tool Runtime
A unified service layer that centralizes authentication, rate limiting, logging, timeout, and audit for all tool invocations, shared by both Dify and LangGraph to keep behavior consistent.
thread_id
A unique identifier for a LangGraph run that allows resuming a paused execution with the same state; essential for approval workflows that span multiple requests.
From the discussion
Featured comments
用户573468752399 1 likes

After we sank the heavy logic into LangGraph, our bill actually went up a notch. Three reasons: first, the same flow calls the model once on each side, so the same prompt gets paid for twice; second, both sides have their own retries, and failures stack into three or four calls; third, keys are scattered across several platforms, so at month's end we can't match which flow spent how much. Later we unified into a single gateway layer: both sides go through the same key, tag by flow, retries happen only once at the gateway, and usage is exported by business line, only then did the accounts balance. Several domestic model providers offer one key that calls all models, enterprise channel pricing, and itemized exports that can be broken down by flow; anyone doing hybrid architecture can have a chat. Details via private message.

Sailing

Cost indeed tends to rise, especially when both sides call once each and retries stack up. Unified gateway and flow tagging, that direction is fine. But the sinking mentioned in the article is mainly not to save money; it's that Dify is insufficient in complex scenarios and needs engineering to fill the gap. The gateway solves usage control; whether a flow should be sunk should still be chosen by scenario, not an either/or.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗