๐ŸŸข Technician Track
Tutorial 2 of 10
๐ŸŸข TECHNICIAN TRACK โ€ข BEGINNER

Tutorial #2: The ReAct Pattern for PLC Code Analysis

Transparent, Auditable AI Reasoning

โœ… CORE MISSION OF THIS TUTORIAL

By the end of this tutorial, the reader will be able to:

  • โœ… Apply the ReAct (Reason + Act) pattern to PLC code analysis
  • โœ… Interpret LLM reasoning through explicit step-by-step thought processes
  • โœ… Maintain strict advisory-only boundaries
  • โœ… Use deterministic reasoning scaffolds for industrial code reviews
  • โœ… Prepare for safe tool-using agents (Tutorial #3)

This tutorial establishes the foundational reasoning layer required for all future AgenticControl tutorials.

โš ๏ธ

โš ๏ธ SAFETY BOUNDARY REMINDER

This tutorial performs analysis only.

It must never be connected to:

  • Live PLCs
  • Production deployment pipelines
  • Safety-rated controllers
  • Motion or power systems

> All outputs are advisory-only and always require explicit human approval before any real-world action.

๐ŸŒ VENDOR-AGNOSTIC ENGINEERING NOTE

This tutorial uses:

  • โ–ธ Generic IEC 61131-3 Structured Text (ST)
  • โ–ธ TwinCAT, Siemens TIA Portal, CODESYS
  • โ–ธ Allen-Bradley ST
  • โ–ธ Any IEC-based runtime

No vendor APIs or libraries are required.

1๏ธโƒฃ CONCEPT OVERVIEW โ€” WHAT IS REACT?

ReAct = "Reason + Act."
In industrial engineering, it is the first pattern that forces AI to behave like an auditor, not an oracle.

Instead of:

Input โ†’ Black-box output

ReAct enforces:

Input โ†’ Step-by-step reasoning โ†’ Conclusion

Why this matters for PLCs:

  • โ–ธ PLC logic is deterministic
  • โ–ธ Industrial systems require auditability
  • โ–ธ LLMs require scaffolding to avoid hallucinations
  • โ–ธ Reasoning must be human-verifiable

ReAct gives us transparent cognition, not hidden decisions.

2๏ธโƒฃ REFERENCE ARCHITECTURE

graph LR
    A[PLC Code] --> B[ReAct Prompt]
    B --> C[LLM Reasoning]
    C --> D[LLM Conclusion]
    D --> E[Human Reviewer]

    style A fill:#1a1a1e,stroke:#04d9ff,stroke-width:2px,color:#fff
    style B fill:#1a1a1e,stroke:#8b5cf6,stroke-width:2px,color:#fff
    style C fill:#1a1a1e,stroke:#8b5cf6,stroke-width:2px,color:#fff
    style D fill:#1a1a1e,stroke:#6366f1,stroke-width:2px,color:#fff
    style E fill:#1a1a1e,stroke:#00ff7f,stroke-width:2px,color:#00ff7f

Boundaries

  • โ–ธ The LLM never writes PLC code
  • โ–ธ The LLM never evaluates live signals
  • โ–ธ The human makes the final judgment

This is the Operator Track safety posture.

3๏ธโƒฃ CLEAN EDUCATIONAL EXAMPLE

We use a simple start/stop logic example:

PASCAL
VAR
    StartButton     : BOOL;
    StopButton      : BOOL;
    MotorRunning    : BOOL;
END_VAR

IF StartButton AND NOT MotorRunning THEN
    MotorRunning := TRUE;
END_IF;

IF StopButton THEN
    MotorRunning := FALSE;
END_IF;

Goal: Use ReAct to analyze this code deterministically.

4๏ธโƒฃ PRACTICAL EXPERIMENTS

You now run two safe, advisory-only experiments using ReAct.

๐Ÿงช Experiment 1: Baseline ReAct Interpretation

Objective

Validate that the LLM performs deterministic, step-by-step reasoning on PLC logic.

Python Code

Python
from openai import OpenAI

client = OpenAI()

plc_code = """
VAR
    StartButton : BOOL;
    StopButton : BOOL;
    MotorRunning : BOOL;
END_VAR

IF StartButton AND NOT MotorRunning THEN
    MotorRunning := TRUE;
END_IF;

IF StopButton THEN
    MotorRunning := FALSE;
END_IF;
"""

prompt = f"""
You are an industrial PLC code analysis assistant.

Rules:
- Use the ReAct pattern.
- First write REASONING.
- Then write CONCLUSION.
- Do not propose code changes.
- Do not infer behavior outside this block.

Analyze the following code:
{plc_code}
"""

result = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": prompt}
    ]
)

print(result.choices[0].message.content)

Expected Output

REASONING:
- Motor starts only when StartButton = TRUE and MotorRunning = FALSE.
- Motor stops whenever StopButton = TRUE.
- Stop has priority since it is evaluated second.

CONCLUSION:
The logic implements a basic start/stop latch without sealing circuits.

Interpretation

  • โ–ธ โœ… Read-only
  • โ–ธ โœ… Deterministic
  • โ–ธ โœ… Human-review mandatory
  • โ–ธ Cost: ~$0.02 | Runtime: <1 second

๐Ÿงช Experiment 2: Controlled Variation (Order Change)

Objective

Show how ReAct detects behavioral changes when code order changes.

Python Code

Python
plc_code_variant = """
IF StopButton THEN
    MotorRunning := FALSE;
END_IF;

IF StartButton AND NOT MotorRunning THEN
    MotorRunning := TRUE;
END_IF;
"""

prompt2 = f"""
Apply the ReAct pattern.

Analyze differences introduced by reordering:
{plc_code_variant}
"""

result2 = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": prompt2}
    ]
)

print(result2.choices[0].message.content)

Expected Output

REASONING:
- Now start logic executes *after* stop logic.
- If both buttons are pressed, StartButton overrides StopButton.
- This is a behavioral change from the previous version.

CONCLUSION:
Reversing the order reverses priority.

Interpretation

  • โ–ธ โœ… Read-only
  • โ–ธ โœ… Advisory-only
  • โ–ธ โœ… Highlights unintended logic changes
  • โ–ธ Cost: ~$0.01 | Runtime: <1 second

๐Ÿ”’ EXPLICIT OPERATIONAL PROHIBITIONS

  • โŒ Writing to a PLC
  • โŒ Modifying production logic
  • โŒ Bypassing interlocks
  • โŒ Performing safety decisions
  • โŒ Connecting directly to live equipment

โœ… KEY TAKEAWAYS

  • โœ… ReAct forces LLMs to show their reasoning, reducing risk
  • โœ… Step-order changes in PLC logic can be detected reliably
  • โœ… Deterministic patterns are safer than natural-language responses
  • โœ… This method prepares you for safe tool-using agents

๐Ÿ”œ NEXT TUTORIAL

#3 โ€” Autonomous Agents vs Rule-Based Systems

Understand the fundamental differences between autonomous agents and traditional rule-based systems in industrial automation.

๐Ÿงญ ENGINEERING POSTURE

This tutorial enforced:

  • โ–ธ Determinism over creativity
  • โ–ธ Advisory intelligence over autonomy
  • โ–ธ Human authority over machine output
  • โ–ธ Transparency over opaque reasoning

โœ… END OF TUTORIAL #2