#23The Orchestrator feedback loopMedium

The Orchestrator feedback loop

Background

The Orchestrator owns the retry loop: plan → execute → synthesize → critique. On FAIL with gaps, it re-executes only the gaps, accumulates findings, and re-critiques — up to max_retries extra attempts.

Problem statement

Implement orchestrate(query, plan, execute, synthesize, critique, max_retries=2).

Input

  • query — the research question.
  • plan — callable query -> list[str] of sub-questions.
  • execute — callable list[str] -> dict[str, str] (findings for those sub-questions).
  • synthesize — callable (query, all_findings) -> str (the answer).
  • critique — callable (query, all_findings, answer) -> (passed: bool, gaps: list[str]).
  • max_retries — extra attempts after the first.

Output

Returns (answer, passed, attempts). Per attempt (max_retries + 1 total):

  1. run execute(sub_questions) and merge into all_findings,
  2. answer = synthesize(query, all_findings),
  3. passed, gaps = critique(query, all_findings, answer),
  4. if passed or gaps is empty → return (answer, passed, attempt + 1),
  5. else set sub_questions = gaps and loop.

If the loop ends, return (answer, passed, max_retries + 1).

Examples

Example 1 — fail then pass on a targeted retry

plan      -> ["a", "b", "c"]
execute   -> {sq: f"F-{sq}" for sq in sub_questions}
critique  -> attempt 1: (False, ["c"]); attempt 2: (True, [])
orchestrate(...)  ->  (answer, True, 2)   # only ["c"] re-executed on attempt 2

Constraints

  • First attempt executes all planned sub-questions; retries execute only gaps.
  • Accumulate findings across attempts (don't discard prior results).
  • Stop and return when passed or no gaps, or after max_retries + 1 attempts.
  • attempts is the number of attempts run.

Notes

  • Re-searching only the gaps is the cost win: the Critic named exactly what failed, so the retry is small.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: passes on the first attempt
  • Sample: retries only the gap sub-question
  • Reference: findings accumulate across attempts