Skip to main content

Workflow Conservation Engine — analyze Dify workflows for cost efficiency and detect waste

Project description

Guardian — Workflow Conservation Engine

Your workflow costs $12/day. Two nodes account for 78% of tokens. They both call GPT-4 for tasks GPT-4o-mini handles.

Guardian is a Python module that analyzes Dify workflows for cost efficiency and detects waste. It doesn't change your workflows — it tells you what to change and why.

What It Does

Budget tracking — Set hard limits on tokens per run, cost per day, and node count. Know immediately when a workflow crosses a threshold.

DAG analysis — Parse any Dify workflow JSON and surface:

  • Redundant LLM calls (same model, same upstream, same job)
  • Dead branches (conditional paths that never execute)

Per-node profiling — Track tokens in/out, latency, and cost for every node across runs. Spot degradation before it hurts.

Waste detection — Find the expensive stuff nobody notices:

  • Overprompted nodes — 4,200 tokens in, 180 out. You're paying for context the model ignores.
  • Idle nodes — Running every time, contributing 0.1% of value.
  • Model mismatch — Using GPT-4 for classification that GPT-4o-mini handles in 12ms.

Reports — Markdown summaries you can paste into Slack, Notion, or a PR comment.

Install

pip install dify-workflow-guardian

Quick Start

from dify_workflow_guardian.budget import WorkflowBudget
from dify_workflow_guardian.analyzer import WorkflowDAG
from dify_workflow_guardian.profiler import Profiler, NodeSample
from dify_workflow_guardian.detector import WasteDetector
from dify_workflow_guardian.report import render_report

# 1. Load a workflow
dag = WorkflowDAG.from_dict(workflow_json)
print(f"{len(dag.llm_nodes())} LLM nodes, {len(dag.redundant_llm_calls())} redundant")

# 2. Profile some runs
profiler = Profiler()
profiler.record(NodeSample(
    node_id="summarizer",
    input_tokens=4200,
    output_tokens=180,
    latency_ms=820.0,
    cost_usd=0.015,
))

# 3. Detect waste
detector = WasteDetector(profiler)
findings = detector.detect()
for f in findings:
    print(f"[{f.severity}] {f.message}")
    print(f"  → {f.suggestion}")

# 4. Generate report
budget = WorkflowBudget()
report = render_report(budget=budget, dag=dag, profiler=profiler, findings=findings)
print(report)

Example Output

# Conservation Report — Customer Support Pipeline

## Budget Summary
- Max tokens / run: 500,000
- Today's spend: $12.43

## DAG Analysis
- Total nodes: 14
- LLM nodes: 4
- Redundant LLM calls: 1
  - `Draft Email` ↔ `Draft Email 2` (same model & upstream)

## Waste Findings

### 🔴 Overprompted — Summarizer
Node 'summarizer' receives 4,200 tokens avg but outputs 180 (ratio 23.3×).

> **Suggestion:** Consider extractive pre-filtering, summarization, or
> reducing the prompt template size.

### 🔴 Expensive Model — classify, summarize
Two nodes account for 78% of tokens. If they use GPT-4, consider
downgrading for simple tasks.

> **Suggestion:** Classification and extraction run fine on gpt-4o-mini.

Module Structure

File Purpose
budget.py WorkflowBudget — token/cost/node limits and daily tracking
analyzer.py WorkflowDAG — parse workflow JSON, find redundancies and dead branches
profiler.py Profiler, NodeProfile, NodeSample — per-node stats and trends
detector.py WasteDetector, WasteFinding — surface actionable waste
report.py render_report() — Markdown conservation reports
tests/test_guardian.py Full pytest suite

Tests

python -m pytest tests/ -v

Philosophy

Guardian doesn't optimize your workflows. It tells you where the money goes and what to do about it. The fixes are yours to make — but at least you'll know where to look.

Built for SuperInstance.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dify_workflow_guardian-0.1.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dify_workflow_guardian-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file dify_workflow_guardian-0.1.0.tar.gz.

File metadata

  • Download URL: dify_workflow_guardian-0.1.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for dify_workflow_guardian-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2df08694860f7d3de198f991f27b0cb0fc879b77acd3a9db5f55a54eaae09e0f
MD5 a1ff57d8beae35cbc20950a866c15feb
BLAKE2b-256 7809bf0a4311a6b978284f2c998c2e8591bd8a4d52761ef7a6d3f5199934e53f

See more details on using hashes here.

File details

Details for the file dify_workflow_guardian-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dify_workflow_guardian-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c188576e62f057b5405055010751f2b6e433aad25c14207c6cab491aad09510f
MD5 18cb0e55c02802079a00f143801dea77
BLAKE2b-256 c7ea751b156161a797721c01db326367dfc4f0f39c0b2792eae94d10b8c97de5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page