Mahal Systems

Overview › 7Orchestration › d.Shaping the loop › ii.

Inversion of control

Control flow General

Give the agent the goal, the tools and the acceptance checks and let it choose the sequence of steps, instead of coding a fixed workflow; the goal, permissions, budget and evaluation stay outside the agent. It pays when the path depends on what the agent finds and the model is strong: a GPT-5.5 optimizer that composed its own process beat two prescribed pipelines in 12 of 14 comparisons at a median 34.3% of the target-interaction budget, but a medium model did better with the pipeline. Where the procedure is known and the constraints are hard, or the model is small, a coded workflow wins: 35.56% against 10.40% for same-model ReAct on TravelPlanner, and 45.3% against 6.7% for a 4B tool-calling agent on WebArena-Verified at 98.6% lower cost. Agent-decided steps also cost more per task: 2.7x to 3.6x the input tokens in one RAG comparison.

Tier 4 because it changes who owns the control flow.

When it applies

  • In the code: Minimal orchestration code; the agent decides the sequence.
  • In traces: Variety of action sequences across tasks of the same type: one sequence recurring across most tasks suggests code could own the path; widely varying sequences on a known procedure suggest constraints are being missed.
  • In eval results: Success, constraint violations, LLM calls, tokens and latency per task for the agent loop against a fixed workflow, per model tier.
  • With a judge: Rate whether autonomy was used well.
  • With probes: Tasks with a known procedure and hard constraints, and tasks whose next step depends on what is found.

How

  1. Start from two baselines on the same model, tools and task set: the fixed workflow you have or would write (code picks the steps, the model fills bounded nodes) and an agent loop (7.d.iii) that gets the goal, the same tools and the same acceptance checks and picks its own steps.
  2. Keep the ends outside the agent in both designs: the goal, permissions, budget caps and the acceptance check or evaluator, so only the route is delegated.
  3. Sort tasks by whether the path is known. Where the steps and constraints are stable (a runbook, a recurring task family, hard business rules), code the path and enforce the constraints in code: on TravelPlanner a coded workflow passed 35.56% against 10.40% for same-model ReAct, and writing the same rules into the baselines' prompts left them 12.7 to 20.2 points behind (37.20 against 17.00 to 24.50). Where the next step depends on what is found, let the agent choose: it rewrote retrieval queries better than a fixed rewrite step (NDCG@10 55.6 against 52.8).
  4. Delegate the route only to a model that can carry it: a GPT-5.5 optimizer that composed its own process beat two prescribed pipelines in 12 of 14 comparisons, but with a medium model the pipeline won by 3.50 to 9.68 points and a 4B model produced no valid action; on WebArena-Verified, code-owned control held 44.7 to 45.3% while a tool-calling agent scored 30.0% on a 120B model and 12.7% and 6.7% on 20B and 4B models.
  5. Mix within one task: keep fixed steps the agent does worse (a fixed reranker scored 49.5 NDCG@10 against 43.9 when the agent re-retrieved on its own), and enforce preconditions on irreversible actions in code (10.b.i); for a task family you run often, move recurring decisions out of the loop into code and keep the model for the semantic steps (76.0 to 91.8% fewer LLM calls in one study).
  6. Measure paired runs on the deployed model and one cheaper tier: success, constraint violations, LLM calls, tokens and latency per task, and spread over at least three runs; agent-orchestrated RAG used 2.7x to 3.6x the input tokens and 1.4x to 1.5x the time of a fixed pipeline. Re-run the comparison when the model changes.

How to measure it

  • experiment (tier 4): paired runs of the agent loop against a fixed workflow on the same model, tools, task set, budget and acceptance checks, on the deployed model and one cheaper tier: success, constraint violations, LLM calls, tokens and latency per task, and spread over at least three runs; report tasks with a known procedure and tasks whose path depends on findings separately
  • monitor: distinct action sequences per task type, steps and cost per task, constraint and policy violations caught by checks, cap-hit rate, and sequences that recur often enough to move into code

To compare the change against your agent's current version, with the same cases and enough repeats to see past run-to-run noise, use aot-evals.

Expected impact

  • Quality: mixed, large: an agent-decided route wins with a strong model where the path depends on findings: 12 of 14 comparisons against prescribed pipelines with a GPT-5.5 optimizer (Xue, Aug 2026); query rewriting NDCG@10 55.6 against 52.8 for a fixed step (Ferrazzi, Jan 2026). A coded workflow wins on known procedures and with smaller models: 35.56% against 10.40% for ReAct on TravelPlanner (Qiu, Aug 2025); a medium optimizer did 3.50 to 9.68 points better with the pipeline (Xue, Aug 2026); 45.3% against 6.7% for a 4B tool-calling agent on WebArena-Verified (Li, Sep 2026)
  • Cost: degrades, medium: more model calls per task: code-owned control cut LLM calls by 76.0 to 91.8% and deployed cost by 74.4 to 98.6% against a tool-calling agent, offline training excluded (Li, Sep 2026); agent-orchestrated RAG used 2.7x to 3.6x the input tokens of a fixed pipeline (Ferrazzi, Jan 2026). Not always: an agent-composed optimizer used a median 34.3% of a fixed pipeline's target-interaction budget (Xue, Aug 2026)
  • Performance: degrades, medium: agent-orchestrated RAG took 1.4x to 1.5x the end-to-end time (Ferrazzi, Jan 2026); a tool-calling agent took longer per task than a code-owned harness in all six settings (Li, Sep 2026); 14.0 steps against 10.2 for a coded workflow (Qiu, Aug 2025)
  • Security: degrades, small (unmeasured): The agent picks each next action, so text in tool output can steer the sequence; contain input (10.c.i) or fix the plan before reading it (6.c.ii).
  • Governance: degrades, medium: rules stated only in the prompt get broken: 835 commonsense-constraint violations for ReAct against 11 for a coded workflow on 1,000 tasks (Qiu, Aug 2025); the agent's route varied more than a staged pipeline's (larger edits and more churn in all 8 paired runs), though final pass/fail agreement was at least 0.78 in 7 of 8 (Xue, Aug 2026)
  • Brand: none (unmeasured)

Caveats

Each study is small or tests the authors' own method: the optimizer comparison is one run per cell, the RAG comparison caps a single-tool agent at three turns, the code-harness results have confidence-interval half-widths of 7 to 13 points on 50 tasks and exclude the offline optimization cost, and the coded-workflow paper encodes the benchmark scorer's constraints and has a cross-model table whose caption and figures disagree. The winner depends on the model and the task, so a result on one model tier does not transfer: prescribed structure helped a medium model and was required for a weak one. The coded workflow's advantage narrowed on open-ended ScienceWorld tasks that need discovery, where both designs failed. The older evidence here (a pattern-library listing and a definition) is unmeasured.

Evidence

Evidence comes from: Planning, Computer use, Web research, Retrieval, Data analysis, Reasoning.

  1. Lists this practice in its agent-optimization pattern library (Control Flow group)Not measured

    Names the practice as a known agent optimization; reports no measured effect for it.

    Scale AI, "VeRO: A Harness for Agents to Optimize Agents" (ICML 2026) · Feb 2026 · paper Table 6; pattern library file agentic-patterns-catalog.md

  2. Defines an agent as the model choosing which tools to run, in a loop, toward a goalNot measured

    "An LLM agent runs tools in a loop to achieve a goal."

    Simon Willison, "I think “agent” may finally have a widely enough agreed upon definition to be useful jargon now" · Sep 2025Older evidence · Opening

  3. Learn recurring control as harness code from failure traces (starting from a scaffold with no controller), keeping LLM calls for semantic steps, and compare it with a tool-calling (ReAct-style) agent and other inference-time agents on the same models, tools and call budgetAgainst

    Against the tool-calling agent, LLM calls fell 76.0-91.8% and deployed-agent cost 74.4-98.6%, with the highest mean success in five of six settings (0.7 points behind in the sixth). WebArena-Verified success 45.3 / 44.7 / 45.3% against 30.0 / 12.7 / 6.7% for tool-calling on gpt-oss-120b / gpt-oss-20b / Qwen3.5-4B; BrowseComp-Plus 49.3 / 39.3 / 29.3% against 40.0 / 40.0 / 12.0%. The tool-calling agent took longer per task in all six settings. 95% confidence-interval half-widths on success are 7 to 13 points.

    Li et al., "Grow the Harness, Not the Context: From Strategy-Free Scaffolds to Reusable Specialist Agents" · Sep 2026 · Abstract; §4.2; Table 1; App. A.3, A.5

  4. Let an optimizer model compose its own skill-improvement process (OEO) under the same objective, data, operations, budget and sealed evaluator as two prescribed pipelines (SkillOpt, GEPA), then vary optimizer capability

    With GPT-5.5 as optimizer, OEO beat SkillOpt in 7 of 8 settings and tied 1, and beat GEPA in 5 of 6 (GEPA's one lead 0.21 points), using a median 34.3% of SkillOpt's target-interaction token budget; optimizer tokens slightly exceeded SkillOpt's budget in 3 settings. With a medium optimizer (Qwen3.5-27B) SkillOpt led by 9.68 points on LiveMath and 3.50 on SearchQA; a weak optimizer (Qwen3.5-4B) produced no executable action under the open interface, while the pipeline still ran. OEO made larger edits and had more revision churn in all 8 paired runs, but pass/fail agreement with SkillOpt's result was at least 0.78 in 7 of 8.

    Xue, Yang, "Rethinking Self-Evolving Agents: Do We Still Need Prescribed Optimization Pipelines?" · Aug 2026 · Abstract; §3.1 Table 2; §3.3 Table 4; §4; App. A.1

  5. Compare a fixed enhanced RAG pipeline (semantic router, HyDE rewriting, reranker) with an agent that decides whether and how to retrieve, rewrite and re-retrieveMixed

    Query rewriting NDCG@10 55.6 for the agent against 52.8 for the fixed pipeline on average (51.7 against 43.9 on NQ). Intent routing F1 98.8 and 99.8 against 95.7 and 96.6 on two narrow domains, but 64.6 against 87.9 on FEVER. Document refinement 43.9 against 49.5 for a fixed reranker; the agent re-retrieved in 10% of cases. The agent used 2.7x and 3.6x the input tokens, 1.7x and 1.8x the output tokens and 1.5x and 1.4x the time (FiQA, CQADupStack-EN, averaged over five models). Answer quality rose with model size at similar rates for both designs.

    Ferrazzi, Cvjeticanin, Piraccini, Giannuzzi, "Is Agentic RAG worth it? An experimental comparison of RAG approaches" · Jan 2026 · Tables 3, 4, 5 and 9; §3.5; §4

  6. Code an expert procedure as a workflow run by a deterministic engine, calling the model only for bounded sub-tasks, against agents that choose their own steps on the same modelAgainst

    TravelPlanner test, 1,000 tasks, Claude-Sonnet-4: final pass 35.56% against 10.40% for ReAct and 18.00% for the ATLAS multi-agent framework; commonsense-constraint violations 11 against 835 (ReAct) and 275 (ATLAS); 10.2 steps against 14.0 for ReAct. With the same constraints written into their prompts (100-task subset, mean constraint-satisfaction score): ReAct 17.00, CodeAct 19.00, ATLAS 24.50, workflow 37.20. Final pass with GPT-5.2: 34.10 against 13.82 for ReAct; Gemini-2.5-Pro: 40.00 against 19.50 (ReAct) and 35.00 (ATLAS). ScienceWorld (30 tasks): 56.7% against 43.3% for ReAct; ALFWorld (134): 97.0% against 91.0%, with more steps (16.8 against 14.0). The authors report the advantage narrows on tasks that need discovery.

    Qiu et al., "Blueprint First, Model Second: A Framework for Deterministic LLM Workflow" · Aug 2025Older evidence · Table I; Table II; Table IV; Table V; §IV-F

Related

In Claude Code

This optimization is a skill in aot-kit, our open-source Claude Code plugin: /aot-kit:inversion-of-control. Its library entry holds every source. Have new evidence, or a correction? Suggest a change.

Before you go...

Build better agents, weekly

Our latest research, performance benchmarks, optimization strategies, and updates around AOT development.

One email a week, nothing else. Unsubscribe anytime.