Everything-as-a-Plugin Architecture in Practice: Designing a TCM Classical Formula Consultation Workflow

    This article explains how to build a Traditional Chinese Medicine (TCM) classical formula consultation flow using an "everything-as-a-plugin" architecture. It covers the three-layer decoupling of engine, plugins, and tools; how to turn "ask enough before concluding" into engine-level gates rather than prompt suggestions; how structured option cards reduce input noise; why formula matching, literature verification, and prescription generation should be three independent gates; and how a defense-response tool plus audit logs preserve traceability. It also outlines the safety and compliance boundaries required for such a system, making the approach reusable for other high-risk, evidence-heavy, process-driven agent applications.

    1. Background: Why Consultation and Prescription Fit a Plugin-Based Flow

    As intelligent coding tools and agent frameworks evolve quickly, “everything-as-a-plugin” has become a common architectural direction: treat model capabilities as replaceable reasoning cores, and push business capabilities down into independent, composable, and testable plugin units. The direct benefit is separation of concerns—prompts express intent, plugins constrain behavior, and the engine handles orchestration and gating.

    A TCM classical formula consultation is a good scenario for validating this architecture because it has three typical characteristics:

    1. Strong process dependency: The sequence from information intake, symptom refinement, and pattern differentiation to formula matching, literature verification, and final prescribing cannot be skipped arbitrarily.
    2. Strong evidence requirements: Conclusions need traceable textual sources and literature support, not just the model’s “intuition.”
    3. Strong risk exposure: Because it involves medication safety, a conservative “no conclusion when information is insufficient” strategy must be preserved at every step.

    Therefore, turning consultation into a plugin is not about wrapping a prompt into a feature. It is about writing business rules into an executable flow, so that “whether a formula can be prescribed” is decided by gates rather than by the model improvising.

    2. Overall Architecture: Decoupling Engine, Plugins, and Tools

    A practical plugin-based design usually has three layers:

    • Engine layer: Handles session orchestration, the stage state machine, confidence gates, and retry/fallback policies. It decides “what to ask next and whether the flow can advance.”
    • Plugin layer: Each business capability is an independent plugin, such as intake, symptom refinement, differentiation and candidates, evidence verification, prescription generation, and challenge response. Plugins are not directly coupled; they communicate through standard input/output contracts.
    • Tool layer: Atomic capabilities that plugins can call, such as structured option rendering, built-in classical text retrieval, external literature retrieval, dosage conversion, and safety checks.

    A representative stage state machine can be expressed as:

    1. intake -> symptoms -> symptoms2 -> signs -> differentiate -> evidence -> prescribe
    2. ^ |
    3. | v
    4. alternatives verdict != supported

    When verdict != supported, the flow is not allowed to enter prescribe. Instead, it falls back to differentiate, forming a closed loop.

    3. Key Design 1: Encode “Ask Clearly First” as a Gate, Not a Prompt

    The original principle is “better to ask more than to conclude early.” In a plugin-based implementation, this should be fixed as an engine-level gate rather than enforced only through prompt persuasion. Practical checks include:

    • Insufficient primary symptom coverage: key symptom groups have not reached the minimum count;
    • The highest match score is below a threshold (for example, 60%);
    • The gap between the top choice and the runner-up is too small (for example, below 8%, tightened to 4% in high-match scenarios).

    If any condition is met, the plugin does not return formula options. It returns the next round of differentiation questions and limits automatic follow-up rounds (for example, at most 3) to avoid infinite loops.

    1. if (primaryHit < MIN_HIT) or (topScore < 0.60) or (topGap < 0.08):
    2. return next_differential_questions(max_round=3)
    3. else:
    4. return candidate_formulas_with_match_and_summary()

    The value of this design is that it exposes uncertainty explicitly to the flow, instead of letting the model force a conclusion when information is insufficient.

    4. Key Design 2: Option-Based Interaction Reduces Input Noise

    The patient-facing side should not present long free-text questionnaires. It should use structured option cards, for three reasons:

    1. Reduced expression bias: Patients interpret terms such as “pulse” and “tongue” differently, and options unify the semantics.
    2. Easier rule evaluation: Multi-select, single-select, range, and numeric combinations are naturally suited to gate calculations.
    3. Smoother interaction: Option-based input reduces typing effort and improves completion rates.

    A typical staged questioning design looks like this:

    • intake: form of address, age, sex, chief complaint (multi-select plus free text), disease duration, and special conditions (such as pregnancy, postpartum, or breastfeeding).
    • symptoms / symptoms2 / signs: symptom multi-select, tongue appearance, pulse, and range or numeric values for temperature, blood pressure, and pulse rate.
    • differentiate: first show a “key differentiation points” multi-select, then a “candidate formulas” single-select, with match scores and pattern-formula summaries.

    Pseudocode:

    1. stage intake:
    2. ask cards: [form_of_address, age, sex, chief_complaint(multi+free), duration, special_conditions]
    3. stage symptoms:
    4. ask cards: symptom multi-select(covering multiple common discomfort categories), temperature range/value, blood_pressure range/value, pulse_rate range/value
    5. stage signs:
    6. ask cards: tongue_appearance(multi), pulse(multi)

    5. Key Design 3: Three Independent Gates for Formula Matching, Literature Verification, and Prescribing

    The original design sets “formula matching, literature verification, and prescribing” as three independent gates, which is central to reliability. A plugin-based implementation should ensure:

    • The matching plugin only returns candidates and confidence, and is not responsible for the final conclusion;
    • The evidence plugin performs dual verification: built-in classical text retrieval plus real-time external literature retrieval, returning structured evidence and identifiers;
    • The prescription plugin rechecks the evidence conclusion before prescribing, refuses directly if it is not supported, and returns alternative paths.
    1. differentiate -> candidates[]
    2. evidence(candidates) -> verdict ∈ {supported, unsupported, partial}
    3. if verdict == supported:
    4. prescribe(recheck=true)
    5. else:
    6. return alternatives -> differentiate

    This “recheck” is not duplicated work. It prevents upstream state contamination or context drift, and is a typical defensive design.

    6. Key Design 4: Principled Challenge Response and Traceability

    When challenged, the system needs to both hold its ground and admit mistakes. A “challenge response” tool can be designed as follows:

    • If evidence is sufficient: output the original textual basis and literature identifier, and explain why the position is maintained;
    • If evidence is insufficient: revise the candidates on the spot or return to re-asking.
    1. def respond_to_challenge(claim):
    2. ev = retrieve_evidence(claim)
    3. if ev.strong:
    4. return "hold", ev.original_text, ev.identifier
    5. else:
    6. return "revise", revise_candidates(), back_to(differentiate)

    At the same time, the full flow should keep an audit log: input summaries for each stage, gate decisions, evidence returns, and the final decision. This makes review easier and supports step-by-step explanation in teaching scenarios.

    7. Safety and Compliance Boundaries (Must Be Stated Explicitly)

    Such a system can only serve as a learning and health-information assistance tool. Its output cannot replace in-person diagnosis or a licensed physician’s prescription. Engineering should build in:

    • Conservative dosage conversion (for example, 1 liang counted as 3 grams);
    • Mandatory warnings for formulas containing toxic medicinal ingredients that they “must be used under a physician’s guidance”;
    • Additional confirmation and downgrade strategies for high-risk groups (such as pregnancy, postpartum, breastfeeding, children, older adults, and abnormal liver or kidney function).

    8. Conclusion: The Essence of Plugin-Based Design Is “Constrained Intelligence”

    “Everything-as-a-plugin” is not about breaking features into fragments. It is about locking uncertainty inside the cage of a process. With engine-level gates, option-based interaction, three independent checkpoints, and challenge-response traceability, the system can provide assistance under the premise of being “careful, detailed, and traceable.” For developers, this method transfers to building agents in any high-risk, evidence-heavy, process-driven domain.