ReAct, For Real: Building Deterministic Tool-Using Agents with LangGraph
ReAct, For Real: Building Deterministic Tool-Using Agents with LangGraph
(feat. a Safe AST Calculator)
A practical guide to wiring a reason + act loop that’s auditable, stable, and observable. We use LangGraph for orchestration and a hardened AST calculator for math, no evals, no vibes.
Why this pattern
- 
Determinism over vibes. The calculator is pure Python AST (whitelisted ops), so results are exact and testable. 
- 
Controlled loop. First model step is forced to call the tool; the second cannot call tools and must answer. No infinite ping-pong. 
- 
Observability. Streamed console output shows precisely when the LLM runs vs. when the tool runs, with token usage. 
Architecture at a glance
Key primitives:
- 
MessagesState: appends messages, preserving the OpenAI tool-calling protocol.
- 
ToolNode: executes declared tools.
- 
tools_condition: routes to tools only when the assistant produced tool calls.
- 
One-tool-call policy: first LLM is bound to the calculator; the finisher LLM is tool-free. 
The safe calculator (snippet)
Goal: evaluate math deterministically, with a tiny NL preprocessor.
Tool wrapper:
Why this is safe: Only numeric literals and whitelisted operators/functions are allowed; no names, attrs, or imports can ever execute.
LangGraph wiring (snippet)
Bind two LLM phases: forced first (must call calculator) and free finish (no tools bound).
Why it ends cleanly: After tools run, the graph goes back to the agent once to produce the final answer; tools_condition routes to END if no tool calls are present.
Observability: know exactly when the LLM is used
Add a tiny callback to print LLM start/end and token usage; gate with DEBUG_AGENT=1.
Optional: DEBUG_CALC=1 prints NL → normalized math expression for the tool.
Running it
Sample (trimmed) output:
Design choices: pros & cons
Pros
- 
Deterministic math; no LLM hallucinations for arithmetic. 
- 
Single tool call guarantees predictable latency and flow. 
- 
Great DX: streaming, token accounting, and NL conveniences ( % of,√, word operators).
Cons
- 
Narrow by design ( sqrt, basic ops). Extend via whitelist if you needlog/exp/pi/e.
- 
Requires an LLM key for orchestration (the math itself is local). 
- 
NL preprocessor is conservative to keep parsing predictable. 
Code & assets
- 
Source: 
Snippet appendix
System rule (keeps the agent honest)
Smoke test idea

%20applied%20to%20Transformer%20models%20in%20machine%20learning.%20The%20image%20shows%20a%20neural%20networ.webp)

Comments
Post a Comment