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:
ReAct enforces:
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:
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
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
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