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.
Metadata
Release files for dify-workflow-guardian 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dify_workflow_guardian-0.1.0.tar.gz | 10.9 kB | Without provenance | Details for dify_workflow_guardian-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dify_workflow_guardian-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for dify_workflow_guardian-0.1.0-py3-none-any.whl |
Total release size: 20.5 kB
Release files / dify_workflow_guardian-0.1.0.tar.gz
| Download URL | dify_workflow_guardian-0.1.0.tar.gz |
|---|---|
| Size | 10.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2df08694860f7d3de198f991f27b0cb0fc879b77acd3a9db5f55a54eaae09e0f
|
|
BLAKE2b-256 checksum How to use checksums |
7809bf0a4311a6b978284f2c998c2e8591bd8a4d52761ef7a6d3f5199934e53f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Release files / dify_workflow_guardian-0.1.0-py3-none-any.whl
| Download URL | dify_workflow_guardian-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c188576e62f057b5405055010751f2b6e433aad25c14207c6cab491aad09510f
|
|
BLAKE2b-256 checksum How to use checksums |
c7ea751b156161a797721c01db326367dfc4f0f39c0b2792eae94d10b8c97de5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Security for dify-workflow-guardian 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.