Agents in production: ReAct loops, timeouts, and human-in-the-loop
Agent demos loop forever. Production agents need budgets, idempotent tools, escalation paths, and explicit stop conditions.
Framed from public engineering talks, blogs, and OSS patterns. Not confidential internals or invented quotes.
The demo vs the pager
A ReAct agent that “keeps trying” looks smart in a blog GIF and catastrophic in prod: runaway tool calls, duplicate side effects, and spend spikes. Public patterns from agent platforms and coding harnesses emphasize budgets, verification, and human gates.
Core loop with guardrails
flowchart TD
Start[User goal] --> Plan[Think / plan]
Plan --> Act[Tool call]
Act --> Obs[Observe result]
Obs --> Check{Budget / stop?}
Check -->|continue| Plan
Check -->|need human| HITL[Human approval]
Check -->|done / fail| End[Final response]
HITL --> Act
Hard budgets (ship all four):
- Max steps / tool calls per run
- Wall-clock timeout
- Token / $ ceiling
- Idempotency keys on mutating tools
Tool design for agents
Tools are the real API surface:
- Narrow, documented side effects
- Structured errors (
retryable,fatal,needs_auth) - Sandboxing for code execution
- Authz checked on every call with user/tenant identity
sequenceDiagram
participant A as Agent
participant G as Gateway
participant T as Tool
participant H as Human
A->>G: tool_call(delete_resource)
G->>G: policy check
G->>H: approve?
H-->>G: approve
G->>T: execute + idempotency-key
T-->>A: result
Planning vs reactive
| Style | Strength | Weakness |
|---|---|---|
| Pure ReAct | Flexible | Wander / loops |
| Plan-then-execute | Auditable | Brittle plans |
| Workflow graph (LangGraph-style) | Explicit states | More upfront design |
| Swarm / multi-agent | Specialization | Coordination tax |
Prefer explicit graphs for business-critical flows; keep free-form ReAct for exploratory internal tools with tight budgets.
Human-in-the-loop (HITL)
Insert HITL when:
- Irreversible or high-cost actions
- Low model confidence / policy uncertainty
- Legal or brand risk
Make HITL a first-class state with SLA — not a Slack DM hope.
Failure modes
- Infinite clarification loops with the user
- Partial writes without compensating transactions
- Prompt injection via tool outputs (treat as untrusted text)
- Hidden fan-out: one user click → N agents → M tools
Eval trajectories
Unit-test tools; integration-test trajectories (golden paths + adversarial). Score: task success, step count, policy violations, human escalations.
What to ship for v1
- ReAct (or graph) with max-step and timeout
- Idempotent mutating tools
- HITL for a defined action class
- Trace every thought/tool/result (redacted)
- Kill switch per agent type
Design interview punchline
“An agent is a state machine with an LLM as a fuzzy transition function — productionize the state machine first.”