How to Reshape an Agent Execution Architecture in Production
When agent tasks grow beyond a single tool call, a fixed orchestration shell becomes the bottleneck for planning, retries, checkpoints, and side effects. This article explains how to separate access and policy from the execution loop, which minimum runtime capabilities that loop needs, and how to migrate the main loop in stages without treating the platform as the source of task semantics.
Many teams start an agent rollout with an off-the-shelf orchestration shell. The shell owns the session entry point, tool registration, permissions, and simple multi-step calls. A specialized agent is only one callable component. That “orchestration first, execution second” start is reasonable. The shell already handles integration, authentication, and basic observability, so specialized capability can be validated as a tool before anyone builds a custom runtime.
The problem usually appears after tasks get longer. Once a goal needs multi-turn planning, mid-course plan changes, intermediate results passed across tools, a different path after failure, and checkpoints that must be kept, the outer orchestrator is no longer just an entry point. It becomes the bottleneck for execution semantics. Call count, context assembly, timeouts, and retries are all decided by the shell. The specialized agent can only “answer” inside a single call and cannot own the task lifecycle. At that point, responsibilities should be redrawn: the orchestration layer returns to access and policy, and the execution layer owns the main loop.
Do not mix the two roles
An orchestration hub is a good place for work that is stable and reused across tasks: identity and tenancy, entry protocols, quotas, audit, human-confirmation policy, and exposing external systems as controlled tools. It answers “who may call what, inside which boundary.”
An execution loop is a good place for work that changes with the task: understand the goal, break it into steps, choose tools, read returns, decide whether to continue or stop, write checkpoints, and narrow the scope on failure before retrying. It answers “what this task should do next.”
Making the executor a component early on outsources the second responsibility to the first system. Shell flows are often a fixed graph: plan once, call once, summarize once. Real tasks are frequently conditional: revise the query when retrieval is insufficient, confirm before a write, and after a tool timeout decide whether to retry, degrade, or ask a person. If those branches are written into a generic shell flow, they are either too coarse or they become special cases that are hard to maintain.
Four signals indicate that it is time to switch. First, a large amount of logic only compensates for the shell: scripts patch state, retries, and summaries before and after calls. Second, context for the same task is truncated repeatedly, so the executor cannot see the basis for its own previous step. Third, failure means rerunning the whole job; there is no way to continue from a checkpoint. Fourth, the permission model only says “this tool may be called,” not “this step is allowed to produce a side effect.” If two of these appear, the main loop should move back inside the execution architecture.
Minimum capabilities of the execution loop
A mature execution architecture is not another conversation wrapper. It is a bounded task runtime. The smallest closed loop is: goal, state, plan, action, observation, and stop conditions.
The goal must be checkable. Keep the user’s original wording as the requirement, and also write completion criteria, such as “produce a reviewable change note” or “run a read-only query and include citations.” Without completion criteria, the loop only stops at a turn limit.
State must be explicit. At minimum, distinguish pending, running, waiting for a human, completed, failed and recoverable, and failed and not recoverable. State belongs to the task record, not to one model output. After a process restart, a node switch, or human intervention, work can continue instead of guessing progress from chat history.
The plan must be revisable, but it should not be thrown out on every step. In practice, keep a short list: current assumptions, the next step, and paths already ruled out. Each turn may change only the part affected by a new observation, which avoids plan oscillation on long tasks.
Actions must go through a tool gateway. The model must not touch external systems directly. The gateway applies parameter checks, timeouts, idempotency keys, return truncation, and error classification in one place. Mark reads and writes separately. Writes require policy approval by default and, when needed, escalate to human confirmation.
Observations must be compressible. Structure the tool return first, then write it into task memory: keep identifiers, time, the conclusion, and pointers to evidence, and summarize the body against a budget. Set the context budget in advance—for example, fixed shares for system constraints, the current plan, recent observations, and long-term facts. When the limit is exceeded, drop original text, not conclusions.
Stop conditions must consider success, failure, and cost together. Check success against the completion criteria. Separate retryable errors from logical dead ends. Cost includes step count, latency, and external call count. When a threshold is reached, emit “what is already done, why it is blocked, and the suggested next step,” rather than ending silently.
The main loop below is only control flow. It is not tied to any product interface:
task = load_or_create(goal, success_criteria)while task.state in {PENDING, RUNNING, WAITING_HUMAN}:checkpoint(task)if needs_human_approval(task.next_action):task.state = WAITING_HUMANbreakplan = revise_plan(task.goal, task.memory, task.last_observation)action = select_action(plan, allowed_tools(task.policy))result = tool_gateway.invoke(action, timeout, idempotency_key)task.memory.add(compact(result))task.state = evaluate(task.success_criteria, result, budget)emit_report(task)
In this loop, the model revises the plan and selects the action, the gateway executes and constrains it, and the task record provides continuity. Once those three are separate, replacing the model or a tool does not overturn the task semantics.
Context, memory, and tool boundaries
After the executor becomes stronger, the most common failure is not “it cannot call tools.” It is context pollution. Stuffing the entire history back into the model makes early wrong assumptions reappear. Cutting history too hard makes the loop repeat paths that already failed.
Three memory layers are enough. Working memory keeps only the current plan, the last few observations, and open items, and it updates every turn. Episodic memory stores key decisions and their reasons at checkpoints for later review, and it does not enter the prompt by default. Factual memory stores only checked, stable information—environment conventions, interface constraints, confirmed identifiers—and it keeps a source. Do not promote a model guess into a fact without evidence.
Tool descriptions should be short and decidable: purpose, read-only or writable, required parameters, what failure means, and which return fields are trustworthy. Do not paste an entire upstream document into the tool description. A failed parameter check should return an actionable error, such as a missing identifier or a type mismatch, not a generic call failure.
For actions with side effects, the gateway generates an idempotency key and records “submitted, result unknown” as its own state. After a timeout, check whether the action took effect before retrying. Do not write again blindly while the state is unknown. Read-only queries may use limited retries by error type, and they should record the parameters already tried so the loop cannot spin.
Permissions should not be only a per-tool switch. A more stable form is policy: subject, resource scope, operation type, data sensitivity, and whether a human must confirm. The executor may propose an action, but it cannot relax policy on its own. After the switch, the orchestration shell is still useful. It should remain the policy enforcement point and the audit point, not the planner of every step.
Migration: run in parallel, then hand over the main loop
Do not replace the entry point, memory, tools, and permissions in one change. Hand them over from lower risk to higher risk.
First, keep the original shell as the only entry point, but add a task record inside it. Every component call writes the goal, an input summary, an output summary, and an error class. The user path does not change. This stage only fills in observability, so you can tell whether failures come from planning, from tools, or from the shell truncating context.
Second, move the loop for multi-step tasks out of the shell’s flow graph and into the executor. The shell only starts the task and queries status. Migrate read-only scenarios first. Completion criteria, a step limit, and checkpoints land in this step. Compare human-remediation count per completed task, repeat-call rate, and the share of recoverable failures. Do not chase a vague claim of being “more intelligent.”
Third, add confirmation and idempotency for writes. A confirmation request should show the object that will change, the scope, and how to roll back—not only the model’s natural-language plan. If the user rejects it, the state returns to revisable rather than failing outright.
Fourth, the shell keeps sessions, authentication, quotas, audit, and connections to external channels. The executor owns the plan and the loop. Pass a clear task identifier between them. Do not treat the shell’s temporary session as the only source of truth.
Design rollback in advance. If the new execution loop fails, the entry point can still fall back to the old “single tool call” path and mark the task as degraded. The degraded path guarantees only limited capability. It should not pretend to be equivalent to the new architecture.
If the team hosts that runtime on a cloud container platform with logs, metrics, and alerting, carry the task identifier through logs, metrics, and traces so one task can be inspected for step count, tool latency, and human wait time. A cloud environment such as Baidu AI Cloud can provide general capabilities such as compute, storage, and observability. Task semantics, permission policy, and completion criteria still have to be defined by the application. Do not assume the platform understands whether the business outcome is right or wrong.
Risk and the definition of done
An autonomous main loop does not mean fewer constraints. Steps, concurrency, writes, and external calls all need budgets. Instructions inside prompts and tool results must not override policy, or external content can induce the executor to widen its permissions. Irreversible operations should require human confirmation by default. Conclusions returned to the caller should distinguish “verified” from “inferred by the model.”
Not every request belongs in a long loop. Single-turn, read-only, stateless questions are better on a lightweight path. The execution loop is valuable for state and recovery across steps. Using it for all casual chat only adds latency and cost.
The switch is complete not when some component is turned off, but when responsibilities are clear: entry and policy stay in the outer layer; plan, observation, checkpoints, and stop conditions stay in the execution layer; tool side effects are checked and audited inside the gateway. After that, further work improves the reliability of the task runtime instead of stacking more special cases onto the shell.
