Skip to content
AI & Agents··7 min read

Designing Deterministic Multi-Agent Execution Loops for Production

AI AgentsArchitectureTypeScriptObservabilitySystems Design

The Demo Trap in Agent Systems

Most prototype AI agent systems look astonishing in controlled demonstrations: a prompt is submitted, the model reasons through a few steps, calls an API, and returns a tidy answer.

In production, however, raw agent loops frequently degrade due to:

  1. Context Window Pollution: Long conversation histories accumulate irrelevant tool outputs, causing model reasoning drift.
  2. Malformed Tool Payloads: Models producing subtle schema errors (e.g. string integers instead of numbers, missing required object keys).
  3. Runaway Loops: Agents getting stuck repeating identical failed queries until the API rate limit or credit card is exhausted.
  4. Non-Deterministic Side Effects: An unconstrained model performing destructive mutations without verification or transaction safety.

To build agentic systems that enterprise clients can trust with real workflows, we enforce three architectural invariants.

+-----------------------------------------------------------------------------------+
|                     PRODUCTION AGENT EXECUTION TOPOLOGY                           |
|                                                                                   |
|  [ User Request / Event ]                                                         |
|             │                                                                     |
|             ▼                                                                     |
|  +───────────────────────────────────────────────+                                |
|  │  Intent Classifier & Subagent Decomposer       │ (Validates task feasibility)  |
|  +───────────────────────┬───────────────────────+                                |
|                          │ Task Token Budget: $0.25                               |
|                          ▼                                                        |
|  +───────────────────────────────────────────────+                                |
|  │  Discrete State Transition Step               │                                |
|  │                                               │                                |
|  │   Model Prompt ──► Strict JSON Schema Parse   │                                |
|  │                          │                    │                                |
|  │            Valid? ───────┴────── Invalid?     │                                |
|  │              │                      │         │                                |
|  │              ▼                      ▼         │                                |
|  │      Execute Tool Call       Self-Correction  │ (Max 2 retry attempts)         │
|  │              │               Feedback Prompt  │                                |
|  │              ▼                                │                                |
|  │     Append Step Checkpoint                    │                                |
|  +───────────────────────┬───────────────────────+                                |
|                          │                                                        |
|             Is Task Complete OR Budget Hit?                                       |
|             ├── YES ──► Write Output & Finalize Trace                             |
|             └── NO  ──► Next Discrete Step                                        |
+-----------------------------------------------------------------------------------+

Invariant 1: State Machines Over Free-Form Loops

Never build an agent loop as a raw while (true) query loop. Instead, model the workflow as an explicit Finite State Machine (FSM):

  • Every state has allowed transitions (e.g., PARSING -> SEARCHING -> VERIFYING -> FINALIZING).
  • State transitions are recorded to an append-only database (SQLite/D1) before executing the next action.
  • If the execution environment crashes, the agent can resume from the exact state checkpoint rather than restarting from zero.

Invariant 2: Type-Safe Schema Enforcement at the Boundary

LLM tool calling should never pass untyped JSON directly to execution handlers. All arguments must pass through runtime validation schemas (e.g. Zod):

// Example: Strict schema contract
export const DatabaseQuerySchema = z.object({
  table: z.enum(['users', 'invoices', 'audit_logs']),
  filterField: z.string().max(50),
  filterValue: z.union([z.string(), z.number()]),
  limit: z.number().int().min(1).max(100).default(20),
});

If validation fails, the structured error is returned to the model with an automated hint:

“Validation Error: ‘limit’ must be an integer <= 100. You provided 500. Please fix your tool invocation.”


Invariant 3: Hard Step and Token Circuit Breakers

Every autonomous workflow must carry hard governance bounds:

  • Maximum Step Limit: Usually 8–12 discrete steps per subtask.
  • Cost Ceiling: Pre-allocated dollar budget per job (e.g. $0.30 max).
  • Escalation Trigger: If the step limit is reached without task completion, the system transitions to REQUIRES_HUMAN_REVIEW with full debug traces rather than failing silently.

Conclusion

Agentic automation is powerful, but only when built on the bedrock of traditional systems engineering: bounded state, typed validation, telemetry, and graceful error handling.

Send us the engineering problem.

We help teams design resilient systems, resolve database bottlenecks, and build reliable automation loops.